PDA

View Full Version : Java and TextMate and why




ifjake
Jan 16, 2007, 08:19 PM
Beginner here.

I've just started a Java course, and am considering getting TextMate to use for writing. I'm getting this stupid error for reasons I know not why. I can compile and run a little test Java app from the terminal just fine but when I try and do it from within TextMate it yells at me:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at testprogram.main(testprogram.java:6)

So this is frustrating. Works fine from the terminal. Works fine from Xcode. I'd rather use TextMate if I could. Obviously there's something fundamental that I'm not getting. Maybe this isn't the best place to post this, but it seems like you guys are pretty kind to people who are new at this stuff.



Llywelyn
Jan 17, 2007, 12:20 AM
Beginner here.

I've just started a Java course, and am considering getting TextMate to use for writing. I'm getting this stupid error for reasons I know not why. I can compile and run a little test Java app from the terminal just fine but when I try and do it from within TextMate it yells at me:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at testprogram.main(testprogram.java:6)

So this is frustrating. Works fine from the terminal. Works fine from Xcode. I'd rather use TextMate if I could. Obviously there's something fundamental that I'm not getting. Maybe this isn't the best place to post this, but it seems like you guys are pretty kind to people who are new at this stuff.

I mostly use TextMate for Ruby and Python development, and don't have it in front of me, so let me ask a few questions on how you are doing it.

- How are you running it through TextMate?
- What is the source code you are trying to run?

Also, have you tried Eclipse?

garethlewis2
Jan 17, 2007, 02:23 AM
Can you post your code. It's impossible to diagnose what is wrong from an exception output in Java unless you see the exception in the context of the code.

mufflon
Jan 17, 2007, 03:47 AM
I'm fairly certain that this issue happened a while back - as long as you don't use any kind of scanner utilities you should be fine (no I/O) - just save the java file and compile it separetly and you'll do fine - this is 100% a textmate issue.

Eclipse is great, but imo due to autocomplete and various configuration options you might not get as much "bang for your buck" as you would with something a lot more basic - so good luck with your future career in programming :)

therevolution
Jan 17, 2007, 10:36 AM
I don't know this for sure, but I believe that the TextMate console is read-only. It doesn't support keyboard input. Your code is probably assuming that the ability to get input is there.

As others have said, if you want a definitive answer, post your code.

ifjake
Jan 17, 2007, 11:50 AM
I don't know this for sure, but I believe that the TextMate console is read-only.

This is probably the case. It appears there's an option to run the class file through the Terminal when I look editing the Java bundle, but it's in this block of code and I'm not sure what to comment or uncomment.

Here's the code for good measure. Thanks for your all's help.


import java.util.Scanner;
class squareroot {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = kb.nextInt();
double r, guess = n/2.0;
for(int i = 0; i < 10; i++) {
r = n/guess;
guess = (guess + r)/2.0;
}
System.out.printf("Square root of %d equals %.2f%n", n, guess);
}
}

mufflon
Jan 17, 2007, 12:10 PM
without system.in you can't do much - no "guessing" or anything (whatever you intend to do with your application)- you can't enter any kind of data and thus you can't really debug it.

Instead do like this: open your terminal, find your way to your java document, compile and run - javac *filnename* && java filenamewithout.class
This is by far the best solution imo.

therevolution
Jan 17, 2007, 03:38 PM
This is probably the case. It appears there's an option to run the class file through the Terminal when I look editing the Java bundle, but it's in this block of code and I'm not sure what to comment or uncomment.

Here's the code for good measure. Thanks for your all's help.


import java.util.Scanner;
class squareroot {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = kb.nextInt();
double r, guess = n/2.0;
for(int i = 0; i < 10; i++) {
r = n/guess;
guess = (guess + r)/2.0;
}
System.out.printf("Square root of %d equals %.2f%n", n, guess);
}
}
Your code pretty much confirms my suspicions. If you want to get input from System.in, you'll have to do that outside of TextMate.

ifjake
Jan 18, 2007, 12:33 AM
without system.in you can't do much - no "guessing" or anything (whatever you intend to do with your application)- you can't enter any kind of data and thus you can't really debug it.

Instead do like this: open your terminal, find your way to your java document, compile and run - javac *filnename* && java filenamewithout.class
This is by far the best solution imo.

It's just a little thing that finds the square root of whatever number you put in, just for fun. And I agree, your suggestion is by far the best solution. Thanks for the help.

And thanks everyone else. I'm set for class now. (PC? We don't need no stinkin' PC!)

iW00t
Jan 18, 2007, 01:17 AM
Why not Eclipse?

There is a standard OS X port too.

garethlewis2
Jan 18, 2007, 02:34 AM
ifjake doesn't state what Mac he/she is using. On an Intel Mac Eclipse is rather nippy. On a PPC Mac (unless it a 2Ghz> G5) it runs like treacle. Using Eclipse for small programs is like using a sledge hammer to open a wallnut.

ifjake
Jan 18, 2007, 09:41 AM
Yeah, these are probably going to be small homework projects to be completed a week after given. And no intel or G5 here.

(nippy? treacle? I now know two new words. Sweet!)

mufflon
Jan 18, 2007, 10:09 AM
Why not Eclipse?

There is a standard OS X port too.

well I use eclipse as well, however when you are beginning the coding endevour it's a lot better to go "dirty" and type eveything by hand - no autocomplete, no easy mode, once you've done your homework and understand what kind of issues is generated by what you can move slowly towards more advanced editors (such as eclipse), this is how I recieved my schooling in my current university and it worked for me :)

jeremy.king
Jan 18, 2007, 11:16 AM
You could try changing the bundle compile and run plist

from

{ java -Dfile.encoding=utf-8 "${TM_FILENAME%.java}"
echo -e "\nProgram exited with status $?."; }|pre

# # if you want to run the program in Terminal.app
# osascript &lt;&lt;EOF
# tell application "Terminal"
# activate
# do script "cd '$TM_DIRECTORY'; java '${TM_FILENAME%.java}'"
# end tell
# EOF


to

#{ java -Dfile.encoding=utf-8 "${TM_FILENAME%.java}"
# echo -e "\nProgram exited with status $?."; }|pre

# # if you want to run the program in Terminal.app
osascript &lt;&lt;EOF
tell application "Terminal"
activate
do script "cd '$TM_DIRECTORY'; java '${TM_FILENAME%.java}'"
end tell
EOF


Good Luck, let me know how it works.

PS. If you want to look at other options, check out Netbean BlueJ edition (http://edu.netbeans.org/bluej/)

ifjake
Jan 18, 2007, 03:41 PM
Yeah that worked. I had tried that but failed to realize I had to comment the previous two lines. BlueJ looks interesting. I'll have to mess with that some. Thanks.