I was taking a coding challenge in which I was to take an integer, a double, and a string from stdin
and have this print in stdout
. The String had to be a String in which had to include more than a singular word, separated by spaces.
When I inputted the code with only a singular nextLine
method below the line with nextDouble
, it worked on the challenge website, but didn't work in my editor. Both the site and my editor were using Java 8. I tried to switch between different Java 8 packages in my editor, but it didn't make a difference. What gives?
Normally, from what I gather, you only have to put one dummy nextLine
in order to resolve the Java nextInt
method not reading newline characters created by hitting "Enter". But in this case, I have to do this twice.
import java.util.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
// Write your code here.
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
scan.nextLine(); //why do I have to include this line in order for spaces to print on my string in stdout?
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}