Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ifjake

macrumors 6502a
Original poster
Jan 19, 2004
562
1
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:
Code:
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.
 
  • Like
Reactions: Jughead

Llywelyn

macrumors member
Oct 27, 2003
48
0
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:
Code:
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

macrumors 6502
Dec 6, 2006
277
1
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

macrumors 6502
Sep 15, 2006
264
2
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

macrumors 6502
May 12, 2003
468
0
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

macrumors 6502a
Original poster
Jan 19, 2004
562
1
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.

Code:
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

macrumors 6502
Sep 15, 2006
264
2
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

macrumors 6502
May 12, 2003
468
0
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.

Code:
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

macrumors 6502a
Original poster
Jan 19, 2004
562
1
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!)
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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

macrumors 6502a
Original poster
Jan 19, 2004
562
1
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

macrumors 6502
Sep 15, 2006
264
2
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

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
You could try changing the bundle compile and run plist

from
Code:
{ 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 <<EOF
#    tell application "Terminal"
#       activate
#       do script "cd '$TM_DIRECTORY'; java '${TM_FILENAME%.java}'"
#    end tell
# EOF

to
Code:
#{ 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 <<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
 

ifjake

macrumors 6502a
Original poster
Jan 19, 2004
562
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.