Using CoderPad, why do I get a NoSuchElementException using a Scanner on System.in?
NickName:youngrrrr Ask DateTime:2015-12-15T06:20:33

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?

Copyright Notice:Content Author:「youngrrrr」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34277694/using-coderpad-why-do-i-get-a-nosuchelementexception-using-a-scanner-on-system

More about “Using CoderPad, why do I get a NoSuchElementException using a Scanner on System.in?” related questions

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 midd...

Show Detail

System.in is not working with Scanner (NoSuchElementException)

So I'm creating a simple programme that contains arrays and such, and my programme compiles perfectly. However, when I run it and input the name of my file (flight), my programme gives me the error

Show Detail

Grabbing multiple string inputs from System.in using Scanner

My problem is related to grabbing multiple user input strings using Scanner on System.in to retrieve a username/password tuple and store the tuple in a String array. similar inquiry: Reading multi...

Show Detail

Why am i getting NoSuchElementException while using scanner

When i run my code i get a NoSuchElementException error. I want the input to be a file path that will be passed to the File class and then to BufferReader. Output: Enter directory: java.util.

Show Detail

NoSuchElementException on scanner

EDIT: I've found the exception to occour on line 24: "choice = scanner.nextInt();" But why, IDK Why do I get a "NoSuchElementException" when using my scanner in the switch to get case 4 and choosi...

Show Detail

Why cant I just create another scanner object after using scanner.close()?

After using the scanner.close() method, I cannot open another scanner. I have searched my question on google to only find people giving a solution for a workaround. I just want to understand why th...

Show Detail

noSuchElementException with Scanner

So I am just making a simple averaging program for fun, and I came across an error that I am interested in why it's happening. Here's the code: import java.util.Scanner; class Main { public static

Show Detail

Why do I keep getting the nosuchelementexception error?

I'm new to programming. I'm trying to get multiple inputs from the user by using Scanner class. I'm using net beans and trying to run and compile my code within the net beans IDE. The program runs ...

Show Detail

JShell: Scanner nextLine() NoSuchElementException when using /open

When using JShell's /open command to open a Java file and run code, user input does not work with Scanner. It will always throw a NoSuchElementException when it reaches the nextLine() method. Does ...

Show Detail

NoSuchElementException after closing system.in

[ Interrupt a thread waiting for user input and then exit the app ] I wanted to do the same exercise as mentioned in the above link. But, I ran into NoSuchElementException when I tried to use inCo...

Show Detail