Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to execute Java code in SciTE

Tags:

java

scite

I have written a sample code:

import java.util.Scanner;

 public class abcd {
    public static void main(String[] args) {
        System.out.print("please enter a: ");
        Scanner a = new Scanner(System.in);
        String b = a.next();
        System.out.println(b);
    }
}

I am able to compile and execute this code via Ubuntu terminal. In SciTe, it compiles fine, but when I run it, I am faced with this error:

please enter a: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at abcd.main(abcd.java:8)

Any Suggestions?

EDIT: When I execute a file in terminal, I do: 'java abcd' Scite does: 'java -cp .abcd'. How are the two commands different and why isn't java -cp working?

like image 959
mayank budhwani Avatar asked Sep 18 '26 04:09

mayank budhwani


1 Answers

It appears that there is a bug/improper implementation in the handling of standard input in SciTE on Linux/Unix.

The description of the bug and a workaround are in this PDF document: A Problem with SciTE Go Command on Linux

Note: this is not official documentation, but it seems to match your problem.

According to that document, when running a Java program through the "Go" command on SciTE, input is supposed to come from the output pane. However, on Linux this does not work properly, and it's as if you are reading from an empty stream.

When you are reading from an empty stream, Scanner sees the end-of-file marker when it attempts to read a value using next(), nextInt() etc. And it throws a NoSuchElementException as there is no input element in the stream.

Your options to work around this problem:

  • Try the method mentioned in the aforesaid document, to use "Go" in a Linux terminal instead of the output pane.
  • Run the program in a terminal and avoud the "Go" command altogether.
  • Use a different IDE which doesn't have this problem.
like image 96
RealSkeptic Avatar answered Sep 20 '26 17:09

RealSkeptic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!