Search code examples
javajava.util.scannerstdinsystem.incollaborative-editing

Using CoderPad, why do I get a NoSuchElementException using a Scanner on System.in?


I am preparing for an interview I have in a few days. I was told that I should familiarize myself with CoderPad since that is the collaborator I will be interviewed with. However, I was in the middle of answering a question when I realized I could not read input from System.in using the Scanner class.

For example, running the following:

import java.io.*;
import java.util.*;

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */

class Solution {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();
    System.out.println(s);
  }
}

Gives me the message:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at Solution.main(Solution.java:14)

What's going on here?


Solution

  • tl;dr: "[CoderPad does not] support input from STDIN in static languages like Java"

    So I was struggling trying to figure out what was going on when I found the following (link and picture included):

    Link:

    https://twitter.com/CoderPad/status/656283541253177345

    Picture:

    enter image description here

    At least as of December 14, 2015, it seems this is a problem with how CoderPad operates their service. It makes sense, but it would've saved me some time and trouble if this was somewhere in their FAQ.

    Hope this helps out someone that will be using their service!