Mac import java.util.Scanner not working with TextMate?

aztastic

macrumors newbie
Original poster
Nov 23, 2009
2
1
Sweden
Hello everyone!
I've just started a programming class at Uni, and we're dealing with Java.
Consequently I got TextMate to work with, as I've heard many good things about it - but I stumbled upon a problem.
One of the practice assignments we were supposed to do was related to the Scanner. I have the following code:

Code:
import java.util.Scanner;
class testtwo
{
      public static void main(String[] args)
      {
         Scanner askname = new Scanner(System.in);
            System.out.println("What's your name?");
            String answer = askname.nextLine();
            System.out.println("Hello " + answer + "!");
      }
}
I have cross-referenced this with my course mates, and the code seems to work for them. They're all running various versions of Windows. However, I get the following error:

Annoying error said:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1471) at testtwo.main(testtwo.java:13)
I have no idea what this means, or what I can do about it. I checked my Java version in the Terminal, it's:

Terminal said:
java version "1.5.0_19"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_19-b02-304)
Java HotSpot(TM) Client VM (build 1.5.0_19-137, mixed mode, sharing)
Very grateful for any replies regarding this!

Edit: I am running a MacBook Pro, OS X 10.5.7 if that helps.
 
  • Like
Reactions: Jughead

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
Are you just running from the commandline with
java testwo
?

I'm guessing that if you are running it elsewhere (through an IDE, etc.) there may not be anything "hooked up" to System.in to read from, so the nextLine() fails.

-Lee

EDIT:
sammich obviously has more insight here. I had no idea textmate would compile/run code for you, i somehow (the name, perhaps...) was led to believe it was "just" a text editor. For now i would use textmate for just text editing, then compile and run your code from terminal using javac and java.
 

aztastic

macrumors newbie
Original poster
Nov 23, 2009
2
1
Sweden
These instructions seem to have fixed it! Thanks a lot! :D

lee1210 said:
Are you just running from the commandline with
java testwo
?

I'm guessing that if you are running it elsewhere (through an IDE, etc.) there may not be anything "hooked up" to System.in to read from, so the nextLine() fails.

-Lee

EDIT:
sammich obviously has more insight here. I had no idea textmate would compile/run code for you, i somehow (the name, perhaps...) was led to believe it was "just" a text editor. For now i would use textmate for just text editing, then compile and run your code from terminal using javac and java.
Yeah, it both compiles and runs. I suppose this can make errors a bit convoluted.. at least to me :p
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
textmate seems less involved than other IDEs, but one way or the other, i feel that when people are beginning using the command line to compile and run code is the only way to do it. You get comfortable with the tools, the errors are more straight-forward, etc. Once you have mastered those things, and have a better idea of how everything works, and you NEED the power an IDE provides, then it's reasonable to give one a whirl.

I'll reiterate, just using textmate to edit text right now. Compile with javac, run with java. Especially with programs of this scope, you don't need all of the fancy integration.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.