1

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);
    }
}
Dan
  • 3,647
  • 5
  • 20
  • 26
  • You don't have to include an extra `scan.nextLine()` if the `String` is supposed to be the next line after the `double`. The challenge must be expecting an extra blank line between the `double` and the `String`. As you haven't shown the text of the challenge, it's hard to know why the challenge would expect that. – Dawood ibn Kareem May 31 '22 at 02:57
  • It's not the coding challenge that expected the extra next line, I only had one `scan.nextLine()` under the double in the challenge, and it stated it was correct. What tripped me out is that it didn't work properly in IDEA on my local PC. – dissidenttux May 31 '22 at 03:10
  • I'd be interested to see if anyone else can reproduce this issue. I know this is an unhelpful thing to say, but this works fine for me, without the extra call. – Dawood ibn Kareem May 31 '22 at 03:15
  • You can print the Scanner input right after user enters it? import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); System.out.println("String: " + s); double d = scan.nextDouble(); System.out.println("Double: " + d); int i = scan.nextInt(); System.out.println("Int: " + i); } } – Isuru Perera May 31 '22 at 03:46
  • Which version of IntelliJ IDEA are you using? It could be a known problem described in https://youtrack.jetbrains.com/issue/IDEA-293951/Console-readLine-skips-input-in-202211 – Thomas Kläger May 31 '22 at 05:53
  • @dissidenttux this problem came up already with IntelliJ. As Thomas Klager said, it is a known bug in Intellij https://stackoverflow.com/questions/72327807/why-is-the-second-string-returned-from-readline-an-empty-string/72327845#72327845 – Dan May 31 '22 at 07:38
  • Thanks everyone! I thought there was something wrong with my local install. Weird bug. Happy to know it's not my fault, haha. – dissidenttux May 31 '22 at 22:44

0 Answers0