4

I am working on a simple project, I took it from a website that gives some challenge to improve my coding skill in java.

import java.util.Scanner;

public class Test1 {        
  public void test() {
    Scanner in = new Scanner(System.in);
    System.out.println("enter something :");
    String str = in.nextLine();
    StringBuilder sb = new StringBuilder(str);
    if (str.isEmpty()) {
        System.out.println("you should write something");
    }
    if(str.length()<=30){
        System.out.println("reverse : "+sb.reverse());
    }else {
        System.out.println("Error");
    }
    System.out.println("----------------------");   
  }

  public static void main(String[] args) {  
    Test1 c = new Test1 ();
    for (int i = 1; i <= 10 ; i++) {
        System.out.println("case number : " +i);
        c.test();
    }
  }
}

case number : 1
enter something : ayoub
reverse : buoya
----------------------
...loop continue ..

My code works like I want in terminal of eclipse, but when I put it into the "code editor" of the web site, this last one gives me a runtime error that says:

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540)
at Test1.test(Test1.java:10)
at Test1.main(Test1.java:31)

I tried to search on StackOverflow for some solutions but I didn't find it.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
Ayoub_Prog
  • 196
  • 2
  • 11
  • 1
    Just guessing, since you don't have quite enough information: eclipse may have connected System.in with the console window so that you can enter things there. I don't know what the '"code editor" of the website' does, but it may be expecting input from some location where you cannot type it. For the future, try to identify what you are working with specifically -- like the name of this 'code editor' you mention -- and since you have a line number in the stack trace, identify the line of code that you posted that matches that line. – arcy Feb 03 '19 at 13:43
  • 1
    As @arcy mentioned, I think it has to do with the 'code editor'. I tried [this compiler](https://www.compilejava.net/) online and seemed to have the same problem. Whereas, testing the same code worked in Netbeans. – Shahlin Ibrahim Feb 03 '19 at 13:52

1 Answers1

2

You are probably using an online java code editor/compiler which does not have an stdin input.

As stated by arcy, you are probably using an IDE with a built-in console window which allows you to pass standard inputs to your program.

The following online editor will allow you to add inputs. You should take care of the way you are using the nextLine method of Scanner:

The exception you are getting is the result of the scanner not getting any inputs, as can be seen here. I suggest you refactor your loop on the scanner using the method hasNextLine of Scanner.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • @ayoubelmohamedi Happy to help buddy. – Rann Lifshitz Feb 04 '19 at 08:15
  • Have been using IntelliJ IDEA's terminal. Project setup has several packages with main methods in it, just to see how things working with multithreading (due to laziness). Used Gradle tasks to run main methods from packages as tasks. Scanner never waited for user input. However if same is done in Main-Class, build and run as jar than works just fine. Sadly took me half a day to solve this. Still don't know why this happening though. – dobhareach Oct 31 '20 at 08:25