PDA

View Full Version : errors with scan.next()



craigtb
04-06-2007, 11:32 PM
Hi,
i have a program that takes in input from the user as a string. To get this i am using string = scan.next().

It works fine until i place a space in the string. I get an error. I dont think i have ever had this problem before but i will post the code jsut in case you need it.


import java.util.Scanner;
class Project8Driver
{
static int age, id;
static double gpa;
static String schoolName, name;
static Scanner scan = new Scanner(System.in);
static int userChoice = 1;
static int counter = 0;
static Student[] ListOfStudents = new Student[20];
final static int LIMIT = 14;
static int ap, pTime;
static String major, minor;
static boolean time;
public static void main(String[]args)
{
while (userChoice != 0)
{
menu();
userChoice = scan.nextInt();
menuSwitch(userChoice);
}

}

public static void menuSwitch(int choice)
{
int num;
switch(choice)
{
case 4:

System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.println("Please enter your expected graduation year");
int graduationYear = scan.nextInt();

ListOfStudents[counter] = new MiddleSchool(name, age, schoolName, id, gpa, graduationYear);
counter++;


break;
case 3:
System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.print("What year do you plan to graduate? ");
int gradYear = scan.nextInt();
System.out.print("How many credits do you need to aquire before you can graduate? ");
int credLeft = scan.nextInt();
System.out.print("What did you score on the SAT? ");
int SAT = scan.nextInt();
System.out.print("How many AP credits do you have? ");
int apCred = scan.nextInt();

ListOfStudents[counter] = new HighSchool(name, age, schoolName, id, gpa, gradYear, credLeft, SAT, apCred);
counter++;


break;
case 1:
int[] activity = new int[LIMIT];
int[] exercise = new int[LIMIT];
int[] project = new int[LIMIT];
System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();
System.out.print("How many AP credits do you have? ");
ap = scan.nextInt();
System.out.print("What is your current major? ");
major = scan.next();
System.out.print("What is your current minor? ");
minor = scan.next();
System.out.print("Are you a full time or part time student? (1 - full time, 2 - part time) ");
pTime = scan.nextInt();
if (pTime == 1)
time = true;
else
time = false;
System.out.print("Please enter your exam 1 grade: ");
int examOne = scan.nextInt();
System.out.print("Please enter your exam 2 grade: ");
int examTwo = scan.nextInt();
// create for loop to gather grades and put them in an array
for (int i = 1; i <= 14; i++)
{
System.out.print("Please enter Activity grade for Activity " + i +" : ");
activity[i-1] = scan.nextInt();
}
for (int j = 1; j <= 14; j++)
{
System.out.print("Please enter Exercise grade for Exercise "+j+" : ");
exercise[j-1] = scan.nextInt();
}
for (int k = 1; k <= 14; k++)
{
System.out.print("Please enter project grade for Project " + k +" : ");
project[k-1] = scan.nextInt();
}
System.out.print("What did you score on the1 final exam? ");
int finalExam = scan.nextInt();

ListOfStudents[counter] = new Undergrad(name, age, schoolName, id, gpa, ap, major, minor, time, examOne, examTwo, activity, exercise, project, finalExam);
counter++;
break;
case 2:
System.out.print("Please enter your name: ");
name = scan.next();
System.out.print("Please enter your age: ");
age = scan.nextInt();
System.out.print("Please enter your school's name: ");
schoolName = scan.next();
System.out.print("Please enter your ID: ");
id = scan.nextInt();
System.out.print("Please enter your current GPA: ");
gpa = scan.nextDouble();

;
System.out.print("What is your current major? ");
String major = scan.next();
System.out.print("What is your current minor? ");
String minor = scan.next();

System.out.print("Are you a full time or part time student? (1 - full time, 2 - part time) ");
int pTime = scan.nextInt();
if (pTime == 1)
time = true;
else
time = false;

System.out.print("What did you score on your GRE? ");
int gre = scan.nextInt();
System.out.print("Are you getting your Ph.D. or Masters? ");
String degree = scan.next();

ListOfStudents[counter] = new Graduate(name, age, schoolName, id, gpa, major, minor, time, gre, degree);
counter++;


break;
case 5:
for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null)
System.out.println(ListOfStudents[i]);
break;
default:
for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null)
System.out.println(ListOfStudents[i]);
System.out.println();
System.out.println("Goodbye");
}
}

public static void menu()
{
System.out.println("0: Quit");
System.out.println("1: College - Undergraduate");
System.out.println("2: College - Graduate");
System.out.println("3: High School");
System.out.println("4: Middle School");
System.out.println("5: Print");
System.out.print("\nEnter your choice: ");

}

}

here are the errors i am getting

Please enter your name: John Smith
Please enter your age: Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Project8Driver.menuSwitch(Project8Driver.java:55)
at Project8Driver.main(Project8Driver.java:21)
Any clue why it is doing this?