0

I am trying to create a program that will allow me to read multiple user inputs from the keyboard, for some reason if I read the double first I cant read the string next. What am I missing here ?

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);
        String item1Name, item2Name, item3Name;
        double vehicleCapacity, item1Weight, item2Weight, item3Weight;


        System.out.print("Please input the vehicles maximum weight capacity in Kilogrammes : ");
        vehicleCapacity = kbd.nextDouble();
        System.out.println(vehicleCapacity);
        System.out.print("Please input the first items name : ");
        item1Name = kbd.nextLine();
        System.out.println(item1Name);
        System.out.println("Program is complete!!");

    }
}

I input 100 then hit enter and this is the output then the program ends without allowing me to input the first items name, what am I not understanding?

Please input the vehicles maximum weight capacity in Kilogrammes : 100
100.0
Please input the first items name : 
Program is complete!!

Process finished with exit code 0
Reign
  • 289
  • 1
  • 9
  • `kbd.nextDouble()` does not handle the end-of-line token for that line, and so when you next call `kbd.nextLine()`, that EOL token *is* handled. But this token is prior to the actual next line of text, which is missed. One solution is to explicitly grab the EOL token after calling `.nextDouble()`. Please read the duplicate to see the details on why this is happening. – Hovercraft Full Of Eels May 06 '21 at 10:51

0 Answers0