import java.util.Scanner not working with TextMate?

Discussion in 'Mac Programming' started by aztastic, Nov 23, 2009.

  1. aztastic macrumors newbie

    Joined:
    Nov 23, 2009
    Location:
    Sweden
    #1
    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:

    I have no idea what this means, or what I can do about it. I checked my Java version in the Terminal, it's:

    Very grateful for any replies regarding this!

    Edit: I am running a MacBook Pro, OS X 10.5.7 if that helps.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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.
     
  3. aztastic thread starter macrumors newbie

    Joined:
    Nov 23, 2009
    Location:
    Sweden
    #4
    These instructions seem to have fixed it! Thanks a lot! :D

    Yeah, it both compiles and runs. I suppose this can make errors a bit convoluted.. at least to me :p
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     

Share This Page