import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d= scan.nextDouble();
String a= scan.next();
// Write your code here.
System.out.println("String: " + a);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Asked
Active
Viewed 56 times
0

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194

notauser123
- 7
- 4
-
2So what exactly is happening? What are you typing, and what's the result? – Jon Skeet Aug 01 '19 at 13:42
-
1(I ran your code entering "10 10.5 foo" and then hitting return, and it was fine...) – Jon Skeet Aug 01 '19 at 13:44
-
Please clarify the problem, I [cannot reproduce it](https://ideone.com/Xppp3L). Make sure your code is properly saved and compiled. Make sure the console is large enough to see all the text. – TiiJ7 Aug 01 '19 at 13:56
-
1@Butiri Dan That happens when you use `scan.nextLine()` which consumes a line terminator, not `scan.next()` which does not. This code works completely fine running it in all new lines or running it all in one line. – Nexevis Aug 01 '19 at 14:00
1 Answers
0
nextDouble() does not consume the new line itself so the next token returned will be an empty string. Thus, you need to follow it with a scan.nextLine().
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Insert an int: ");
int i = scan.nextInt();
System.out.print("Insert a double: ");
double d = scan.nextDouble();
scan.nextLine();
System.out.print("Insert a string: ");
String a = scan.nextLine();
System.out.println("Inserted string: " + a);
System.out.println("Inserted double: " + d);
System.out.println("Inserted int: " + i);
}
and the output is:
Insert an int: 45
Insert a double: 7.7452
Insert a string: Hello World
Inserted string: Hello World
Inserted double: 7.7452
Inserted int: 45

bart
- 1,003
- 11
- 24