简体   繁体   中英

Why is my for loop printing 2 of my prompts at one time while using (scanner).nextLine();

Is there a problem in my while or for loops by chance or am I missing something? First run through works fine but on the second I get this:

Enter course NAME: class name
Enter course HOURS: 4
Enter course GRADE: 4.0
You have entered class: jeff graves, class hours: 4 and, class grade 4.0
Do you want to continue:
y
Enter course NAME: Enter course HOURS: 

It works just fine using (scanner).next(); but then I can only take one word from the user and it will throw an error from the nextInt when it rolls over.

   public class GetGrades { //open class GetGrades

    public static void main(String[] args)  {
/**creating new instance of scanner named input to read user input*/
        Scanner input = new Scanner(System.in);  // create new instance of scanner object
        boolean repeat = true;  //boolean value for while loop holding array input
        String s;  // create string to store user input value for exiting loops
/** create 3 arrays to store course name, hours, and grades*/
        String[] name = new String[20]; //type string array for class name
        int[] hours = new int[20]; // type int array for class hours
        float[] grade = new float[20]; //type float array for class grade


        outerloop:  // set break point for nested for loops exit on user input 
            while(repeat != false)  {// while loop with boolean value to let user exit array input
        for (int i=0; i<name.length; i++)  { //for loop for name array

            System.out.print("Enter course NAME: "); //prompt user for input
            name[i] = input.nextLine(); //read next line value and store in array name
            System.out.print("Enter course HOURS: "); //prompt user for input
            hours[i] = input.nextInt(); //read the next int and store in array hours
            System.out.print("Enter course GRADE: "); //prompt user for input
            grade[i] = input.nextFloat(); //read the next float value and store in array grade

        /**Print line to console summing um what the user has entered*/
            System.out.println("You have entered class: " + name[i] + ", class hours: " + hours[i] + 
                    " and, class grade " + grade[i]);

/**prompt user if wanted to enter more grades, break loop on n or N*/
            System.out.println("Do you want to continue:");
            s = input.next();
            if ( s.equals("y") || s.equals("Y"))  { //open if statement
                repeat = true;
            } else  { //close if and open else
                break outerloop;
            }  //close else statement


          }//close for loop with i as count
        }//close while

input.next() will read the next word. input.nextLine() will read up until the next time you press enter.

This means that when you write "y" and hit enter, you've input both a word "y", as well as the next enter, filling in both prompts at the same time and causing the next prompt to be written.

You can simply replace your next() with nextLine() when you ask to continue:

        System.out.println("Do you want to continue:");
        s = input.next();

becomes

        System.out.println("Do you want to continue:");
        s = input.nextLine(); 

thereby reading both the "y" and the enter. The next prompt is now free to accept new input.

When you input the grade, for example 12.3 and enter, "input.nextFloat();" will only take "12.3" but not "enter", so "enter" will be taken by the next scanner.

In my opinion,

first, change "s = input.next()" to "s = input.nextLine()", but it will take the "enter" of previous scanner "grade[i] = input.nextFloat();", so, second, put it into a while loop as a condition. like this

while((s = input.nextLine()).equals("")) {}

therefore, it won't stop til get the expect input.

try this..

System.out.print("Do you want to continue:");
while((s = input.nextLine()).equals("")) {}
if (s.equals("y") || s.equals("Y")) { // open if statement
    repeat = true;
} else { // close if and open else
    break outerloop;
} // close else statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM