
-----------------------------------
chromium
Thu Mar 13, 2014 1:07 pm

Code working in Eclipse but not in Sublime Text
-----------------------------------
Hi Im trying to get java to run properly in Sublime Text. I have the following code, which seems valid to me:

[code]import java.util.Scanner;

public class Thing {

	public static void main(String args[])
	{
	    Scanner input = new Scanner( System.in );

	    System.out.println ( "Please enter the numerator: " );
	    int num = input.nextInt();

	    System.out.println ( "Please enter the denominator: " );
	    int den = input.nextInt();

	    if (den != 0 ){
	      System.out.println ("Decimal is " + convert (num, den));
	    }
	    else 
	      System.out.println ("Cant divide by zero");       

	}

	public static double convert(int x, int y)
	{        
	        return ((double)x/y);        
	}

}
[/code]

It runs fine in Eclipse but when I try to run in Sublime Text I get this error:
[code]Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:907)
	at java.util.Scanner.next(Scanner.java:1530)
	at java.util.Scanner.nextInt(Scanner.java:2160)
	at java.util.Scanner.nextInt(Scanner.java:2119)
	at Thing.main(Thing.java:11)[/code]

Anyone know why?

-----------------------------------
Raknarg
Thu Mar 13, 2014 1:14 pm

RE:Code working in Eclipse but not in Sublime Text
-----------------------------------
String[] args not String args[] I believe

-----------------------------------
chromium
Thu Mar 13, 2014 3:03 pm

Re: RE:Code working in Eclipse but not in Sublime Text
-----------------------------------
String

Made no difference. Same error.

-----------------------------------
Zren
Thu Mar 13, 2014 4:02 pm

Re: Code working in Eclipse but not in Sublime Text
-----------------------------------
Sublime Text doesn't have a full fledged console inside it. So you can't really use System.in to enter input.

As a fix, you can change your build file to this:


{
    "cmd": 

The start command will open a new console window (Command Prompt), rather than tunneling the streams through Sublime.

Ctrl+B = Build
Ctrl+Shift+B = Run

-----------------------------------
Raknarg
Thu Mar 13, 2014 9:58 pm

RE:Code working in Eclipse but not in Sublime Text
-----------------------------------
Oh I didn't realize Sublime Text could compile code at all.

-----------------------------------
chromium
Thu Mar 13, 2014 11:21 pm

Re: Code working in Eclipse but not in Sublime Text
-----------------------------------
Sublime Text doesn't have a full fledged console inside it. So you can't really use System.in to enter input.

As a fix, you can change your build file to this:


{
    "cmd": 

The start command will open a new console window (Command Prompt), rather than tunneling the streams through Sublime.

Ctrl+B = Build
Ctrl+Shift+B = Run

Great, that works almost perfectly. Command prompt opens and runs the program, however after everything has completed (get num, get den, return quotient as double), the console just closes. Is there a way to stop this from happening? So that the console stays open after all the code has executed?

Edit: Nevermind, I figured it out. I needed to add a command to pause the console after completing:
[code]{ 
    "cmd": ["javac", "$file"], 
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)", 
    "selector": "source.java", 
    
    "variants": [ 
        { 
            "name": "Run", 
            "cmd": ["start", "cmd", "/c", "java $file_base_name & pause"], 
            "shell": true 
        } 
    ]    
} [/code]
