Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Code working in Eclipse but not in Sublime Text
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chromium




PostPosted: Thu Mar 13, 2014 1:07 pm   Post subject: 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);       
        }

}


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)


Anyone know why?
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Thu Mar 13, 2014 1:14 pm   Post subject: RE:Code working in Eclipse but not in Sublime Text

String[] args not String args[] I believe
chromium




PostPosted: Thu Mar 13, 2014 3:03 pm   Post subject: Re: RE:Code working in Eclipse but not in Sublime Text

Raknarg @ Thu Mar 13, 2014 1:14 pm wrote:
String[] args not String args[] I believe


Made no difference. Same error.
Zren




PostPosted: Thu Mar 13, 2014 4:02 pm   Post subject: 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:

Javascript:

{
    "cmd": ["javac", "$file"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java",
   
    "variants": [
        {
            "name": "Run",
            "cmd": ["start", "java", "$file_base_name"],
            "shell": true
        }
    ]   
}


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




PostPosted: Thu Mar 13, 2014 9:58 pm   Post subject: RE:Code working in Eclipse but not in Sublime Text

Oh I didn't realize Sublime Text could compile code at all.
chromium




PostPosted: Thu Mar 13, 2014 11:21 pm   Post subject: Re: Code working in Eclipse but not in Sublime Text

Zren @ Thu Mar 13, 2014 4:02 pm wrote:
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:

Javascript:

{
    "cmd": ["javac", "$file"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.java",
   
    "variants": [
        {
            "name": "Run",
            "cmd": ["start", "java", "$file_base_name"],
            "shell": true
        }
    ]   
}


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
        }
    ]   
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: