简体   繁体   中英

Scanner not asking input of string

In the below code:

secondI = scan.nextInt();
System.out.println("double");
secondD = scan.nextDouble();
System.out.println("string");
secondS = scan.nextLine();

After entering the double value, it is skipping the string entry part. How can I fix this?

secondI = scan.nextInt();
System.out.println("double");
secondD = scan.nextDouble();

scan.nextLine();

System.out.println("string");
secondS = scan.nextLine();

You need to have this extra nextline statement which consumes the remaining section of that line which you entered for the double value.

Use

secondS = scan.next();

instead of

secondS = scan.nextLine();

Please see the documentation for Scanner.nextLine() .

Advances this scanner past the current line and returns the input that was skipped.

Try using other methods of the Scanner class such as Scanner.next()

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