Reputation: 127
When I run this code, the lines after the while loop are never executed. I've done testing inside the loop itself, and as far as i can tell the loop itself is completing, the method just never moves on to the following line.
I am aware there are multiple similar topics, but most seem to reference proper string comparisons and infinite loops.
Example input for this would be:
Maria 1 2 3 4
Output should be:
Maria's GPA is 2.50.
Any help would be appreciated.
public static void printGPA(){
Scanner console = new Scanner(System.in);
String studentName = "";
int counter = 0;
int gpa = 0;
System.out.print("Enter a student record: ");
while (console.hasNext()){
if (console.hasNextInt()){
gpa += console.nextInt();
counter += 1;
} else {
studentName = console.next();
}
}
System.out.print(studentName + "'s GPA is ");
System.out.printf("%.2f.", ((double)gpa / (double)counter));
}
Upvotes: 2
Views: 542
Reputation: 2210
The loop is going infinite and keeps waiting. Add some condition that will break the loop ->
while (console.hasNext()){
if (console.hasNextInt()){
int num = console.nextInt();
if ( num == -99){
break;
}
gpa += num;
counter += 1;
} else {
studentName = console.next();
}
}
Then enter -> "Maria 1 2 3 4 -99"
Or you can add the logic to break the loop after counter reaches 4.
while (console.hasNext()) {
if (console.hasNextInt()) {
gpa += console.nextInt();
counter += 1;
if (counter == 4) {
break;
}
} else {
studentName = console.next();
}
}
Upvotes: 0
Reputation: 565
(Addition to Xabster's answer) Since the scanner stream will keep on looking forever and doesn't know when you stop, another solution is to introduce an exit word, such as "done". That means it will keep on taking in numbers until you say done.
public static void printGPA(){
Scanner console = new Scanner(System.in);
String studentName = "";
int counter = 0;
int gpa = 0;
System.out.print("Enter a student record: ");
String input = "";
while (true){
input=console.next();
if (input.equals("done"))
break;
try
{
gpa += Integer.parseInt(input);
counter += 1;
}
catch (NumberFormatException e)
{
studentName = input;
}
}
System.out.print(studentName + "'s GPA is ");
System.out.printf("%.2f.", ((double)gpa / (double)counter));
}
Input:
Maria 1 2 3 4 done
Upvotes: 0
Reputation: 32
I think your problem is that you need another scanner. The first scanner is going to grab everything in the input stream. Here's an idea, scan the entire line of input and throw it into a string. Then scan that string with another scanner. Here is my proposed solution:
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
String studentName = "";
int counter = 0;
double gpa = 0;
System.out.print("Enter a student record: ");
String myInput = console.nextLine();
Scanner scan2 = new Scanner(myInput);
while (scan2.hasNext()){
if (scan2.hasNextInt()){
gpa += scan2.nextInt();
counter += 1;
}
else {
studentName = scan2.next();
}
}
System.out.print(studentName + "'s GPA is ");
System.out.printf("%.2f.", (double)(gpa / counter));
}
This should work, it worked for me although I needed to change the data type of gpa to double to get the proper calculation. I know just reposting code may not seem helpful, but I felt it was an easier to show you rather than try and explain it. Hope this helps!!!!
Upvotes: 1
Reputation: 3720
while (console.hasNext()){
is a blocking call that waits for input. If the stream is not terminated it is assumed that there is more. System.in reads from your keyboard and that stream should never be closed and therefor the "hasNext()" call will wait indefinitely.
The fix is to do this:
Scanner sc = new Scanner(System.in);
System.out.print("Enter a student record: ");
String str = sc.nextLine();
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
String token = st.nextToken();
// try to parse the token as an integer with try-catch Integer.parseInt()
try {
int num = Integer.parseInt(token);
gpa += num;
counter++;
} catch (NumberFormatException e) {
// if it fails, assume it's the name of the student
studentName = token;
}
}
// We only read a single line and we're not asking how much more there is.
Upvotes: 1