0

I have to write a program for my class that takes user input for homework,labs,tests,etc,puts them in a string, and there is a separate class that splits them in separate "double"numbers and contains a writeToFile method to write the student's grade into a file.

I keep getting a problem though.. When I ask the user for their name the first time, it works fine, but if the user decides to enter the do-while loop again, the "What is your name" is skipped and it goes straight to id. I know it has something to do with the last input not being a string, and the solution to that is to put a "keyboard.nextLine();" above the question, but I tried that and it just asks the user for their name before the question is even asked..

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

    public class GradeApplication
    {
        public static void main(String[] args) throws IOException 
        {
            Scanner keyboard = new Scanner(System.in);

            //Define variables
            String name;
            int id;
            String homework;
            String labs;
            String tests;
            double project;
            double discussion;
          int answer;

            do
            {
                System.out.print("\nWhat is your name? ");
                name=keyboard.nextLine();

                System.out.print("\nWhat is your student ID? ");
                id=keyboard.nextInt();

                homework = keyboard.nextLine();
                System.out.println("\nPlease enter homework grades separated by spaces:");
                homework = keyboard.nextLine();

                System.out.println("Please enter lab grades separated by spaces:");
                labs = keyboard.nextLine();

                System.out.println("Please enter test grades separated by spaces:");
                tests = keyboard.nextLine();

                System.out.println("Please enter project grade:");
                project = keyboard.nextDouble();

                System.out.println("Please enter discussion grade:");
                discussion = keyboard.nextDouble();

                System.out.println("\nResults: ");
                //Call toString method
                Student_Khazma s = new Student_Khazma(name,id,homework,labs,tests,project,discussion);
                System.out.print(s.toString()); 

     //Open file         
         PrintWriter outputFile= new PrintWriter("gradeReport.txt");
         System.out.println(s.writeToFile());
         outputFile.close();

         System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
         answer=keyboard.nextInt();

             System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
             answer=keyboard.nextInt();

                }while(answer==1);

        }
    }

1 Answers1

6

Your last call to Scanner#nextInt():

    ...
    System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
    answer=keyboard.nextInt();

} while(answer==1);

Does not consume the newline character so it is passed through to your first call of Scanner#nextLine(). As a consequence, the scanning operation will not block waiting for input (see the javadoc for details). To solve it you will need to add keyboard.nextLine();:

    ...
    System.out.print("\n\nWould you like to see your grade report again? (1 is yes, 2 is no): ");
    answer=keyboard.nextInt();
    keyboard.nextLine(); // <== line added here
} while(answer==1);

So that your first call to nextLine() will block for input (when the loop restarts).

acdcjunior
  • 132,397
  • 37
  • 331
  • 304