-1

I am trying to learn Java, and I've just started. I want to run this code I found online:

import java.util.Scanner;  // needed for Scanner

/** A Java program that demonstrates console based input and output. */
public class MyConsoleIO 
{
    // Create a single shared Scanner for keyboard input
    private static Scanner scanner = new Scanner( System.in );

    // Program execution starts here
    public static void main ( String [] args )
    {
        // Prompt the user
        System.out.print( "Type some data for the program: " );


        // Read a line of text from the user.
        String input = scanner.nextLine();

        // Display the input back to the user.
        System.out.println( "input = " + input );

    } // end main method

} // end MyConsoleIO class

However I get this error:

Type some data for the program:
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at MyConsoleIO.main(MyConsoleIO.java:17)
[Finished in 0.9s with exit code 1]

I am running the code in Sublime Text 2, directly in the editor, pressing CMD+B.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sahand
  • 7,980
  • 23
  • 69
  • 137

3 Answers3

2

You can't call nextLine() directly, you need to check first if a line is available (the user has hit enter) with scanner.hasNextLine() that blocks until that line can be read.

String input ="";

if(scanner.hasNextLine()){
  input=scanner.nextLine();
}

The javadoc for Scanner describes this behaviour, the summary on top usually gives you all the information you need to use that class.

Also, if you are a beginner, the book Thinking in Java is usually recommended.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
uraimo
  • 19,081
  • 8
  • 48
  • 55
  • I added your code int the program (replacing String input = scanner.nextLine(); ) However, I got this errormessage: MyConsoleIO.java:21: error: cannot find symbol input=scanner.nextline(); ^ symbol: method nextline() location: variable scanner of type Scanner 1 error Now when I replaced the lowercase "l" in the row input=scanner.nextline(); so that it became input=scanner.nextLine();, the program just finished without me entering anything: Type some data for the program: input = [Finished in 0.9s] . So I still can't enter anything. – Sahand Mar 28 '15 at 21:13
  • That way you will not be able to use `input` outside the if (not a problem), read about *variables scope* to learn more about this if interested. – uraimo Mar 28 '15 at 21:15
  • Ok, but even if i place the row: System.out.println( "input = " + input ); inside the if-statement I can't enter anything from my keyboard, I get this message: Type some data for the program: [Finished in 0.9s] – Sahand Mar 28 '15 at 21:18
  • Java is case sensitive, nextline() and nextLine() are two different methods. – uraimo Mar 28 '15 at 21:18
  • Okay. When I use nextline(), I get the following error message: MyConsoleIO.java:21: error: cannot find symbol input=scanner.nextline(); ^ symbol: method nextline() location: variable scanner of type Scanner 1 error – Sahand Mar 28 '15 at 21:20
  • nextline() does not exist :), it's nextLine(). – uraimo Mar 28 '15 at 21:24
  • Other than this, if everything is still like in your question, plus my if, it must work, it prints your "Type some data", waits for some input+ enter key, and prints. If it still doesn't work, try editing your question adding at the end the new code, and i'll take a look at it. – uraimo Mar 28 '15 at 21:25
  • Wait. I'm not sure that sublime text actually allows you to type something inside the editor... go in that directory and type: `javac MyConsoleIO.java` and then once it has compiled your file run it with `java MyConsoleIO ` (without .java at the end) – uraimo Mar 28 '15 at 21:30
  • Thank you! P.S. I had to use .nestLine() in order for it to compile. – Sahand Mar 28 '15 at 21:42
2

I'm not sure about Sublime Text, but your program looks correct. I ran it in my NetBeans 8.0.1 and all works fine. It looks like it is a problem in how Sublime runs java programs.

Try compile and run it with standard java compiler and run tools. For further details read this: http://www.oracle.com/technetwork/java/compile-136656.html

See also: https://stackoverflow.com/a/14810204/1981450

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ar4ers
  • 740
  • 5
  • 19
  • He could be right, sublime could not allow interaction with System.in. – uraimo Mar 28 '15 at 21:31
  • 1
    I think that's problem in **how** sublime runs java programs. It looks like it somehow skips user input on `nextLine()` and method throws exception, as wrote in sources: `if (result == null) throw new NoSuchElementException("No line found");` – ar4ers Mar 28 '15 at 21:40
0
private String read(String text) {
      System.out.println(text);
      return read();
}

private String read() {
      if (!scan.hasNext())
      System.exit(0);
      return scan.next();
}

write and scan with

String string = read("Input: ");

only read with...

String string = read();