0

I am working on a program to average grades. For some reason my scanner or console is not letting me enter an answer for the third question and I am not sure why.

A lot of the code is commented out as it unneeded for now.

edit: I am not sure how to number lines of code here but the lines 23-28 I think are the problem

import java.text.DecimalFormat;
import java.util.Scanner;

public class GradeData {

    public static void main(String[] args) {

int count = 0;
double grade = 0;
//decided to use double for grade in case user's want to be specific with
//decimal points.
String user;
boolean enter;
String name;
String enterAgain;
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter your name: ");
name = userInput.nextLine();
System.out.println("Please enter a grade: ");
grade = userInput.nextDouble();

//System.out.println(user); //May not need this line
System.out.println("Would you like to continue entering grades? (y/n)");
enterAgain = userInput.nextLine();

if (enterAgain.equalsIgnoreCase("y"))
        /*|| enterAgain.equalsIgnoreCase("Yes"))*/ {

    System.out.println("Please enter another grade: "); }
/*  if (user.getScore() > highScore) {

        highScore = user.getScore();
    }
    System.out.println("High Score: "
            + DecimalFormat.format(grade));
    user.setScore(0);
}*/

else {
    enter = false;
userInput.close();
    }
}
}
Steven Penny
  • 82,115
  • 47
  • 308
  • 348
Martin
  • 5
  • 2

2 Answers2

0

Look at the link that @Fast Snail commented (Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods) - it explains your issue. To fix this problem, as shown in that post, add a userInput.nextLine() after the userInput.nextDouble(). Here would be the finished code (I removed some of the comments and variables that weren't relevant):

import java.util.Scanner; 

public class GradeData{
    public static void main(String[] args){
        double grade = 0;
        String name;
        String enterAgain;
        boolean enter = true; //to use in while loop
        Scanner userInput = new Scanner(System.in);
        System.out.println("Please enter your name: ");
        name = userInput.nextLine();
        System.out.println("Please enter a grade: ");
        grade = userInput.nextDouble(); 
        userInput.nextLine() //add this to your program
        System.out.println("Would you like to continue entering grades? (y/n)");
    enterAgain = userInput.nextLine(); 

        while(enter){
            if(enterAgain.equalsIgnoreCase("y")){
                System.out.println("Please enter another grade: ");
                /*enterAgain = userInput.nextLine() - we want user to input 
                a double grade, not a String*/
                grade = userInput.nextDouble();
            }
            else if(enterAgain.equalsIgnoreCase("n")){
                System.out.println("Ok. Don't enter more grades.");
                break; 
            }
            else{
                System.out.println("Sorry, you can only enter (y/n)");
                break; 
            }
        }
    }
}

The break; statements in the else{} statements are to stop the loop from running after the user has entered n for no (or some other random character besides y or n). For example, the loop will output "Ok. Don't enter more grades." and stop there. If we don't include the break; statement, it'll print that out infinitely.

Community
  • 1
  • 1
  • Thank you for your help, those edits worked nicely. Do you happen to know a good loop that would work with that if statement? – Martin Sep 13 '15 at 01:39
  • @Martin yes. Look at my updated code. It allows the user to keep entering grades after they entered y for yes. I was trying to make it so that they can enter n and exit the loop after they've already entered y - but that didn't work out. Still trying to figure that out. So pretty much after you enter y, you keep getting asked to enter grades without being able to stop. –  Sep 13 '15 at 03:32
0

Okay, from my experience this seems to be a fairly difficult problem to get around based off of your code. I have found one solution to getting to the if statement after the "continue entering grades?" question. set the enterAgain variable equal to userInput.next(); This will allow the program to continue its path. Though, from there, I had an issue where after you say "y or n" the program ends. Here:

System.out.println("Would you like to continue entering grades? (y/n)");
enterAgain = userInput.next();

This code will allow you to enter something but it does not display "Please enter another grade." This was the only solution I was able to come up with. Also try using a switch statement or while loop to experiment results in your program. Best of luck.

Locos
  • 1
  • 3