Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Eclipse console

  1. #1
    Join Date
    Oct 2011
    Beans
    6

    Eclipse console

    Hey guys,

    I just installed Oneiric Ocelot on my laptop and wanted to start programming in Java with Eclipse IDE.

    But there is a little problem I got.

    Code:
    public class aufgabe7 {
    
        public static void main(String[] args) {
    
            System.out.println("Please input a number: ");
            
            int zahl = Integer.parseInt(System.console().readLine());
            
        }
    
    }
    For example with this code. When I run this, I get a NullPointerException because it does not let me input anything, it just runs through and then of course readLine() returns null.

    I read that since Eclipse 3.2 or 3.3 there is something different with the intern console.

    When I run that program with the terminal and the java commands everything works fine.

  2. #2
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Eclipse console

    add this to your code and see what happens

    Code:
    Console con = System.console();
        if (con == null) {
            System.err.println("Error:console is null");
            System.exit(1);
        }

  3. #3
    Join Date
    Oct 2011
    Beans
    6

    Re: Eclipse console

    Gives me "Error: console is null"

  4. #4
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Eclipse console

    Ok. I believe that this is because the JVM you are using does not have an associated console. I don't think it is a problem specific to eclipse. There is another way to read input from the command line. try this:

    Code:
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in);
    
    System.out.println("Please input a number: ");
            
    int zahl = Integer.parseInt(reader.readLine());

  5. #5
    Join Date
    Oct 2011
    Beans
    6

    Re: Eclipse console

    I know but if possible I would like to avoid those "Readers" since we don't use those in our lectures.

    And before it worked fine as well :/

    Which means I'd rather fix that JVM problem. I didn't install/fix/repair anything java specific after I installied Oneiric so I'm quite unsure what I got here and what I need.

  6. #6
    Join Date
    May 2009
    Location
    relative
    Beans
    365
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Eclipse console

    For such input actions (like starting program with specific parameters) you should inspect Run Configurations http://help.eclipse.org/galileo/inde...figuration.htm .

  7. #7
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Eclipse console

    Quote Originally Posted by S0m3guy View Post
    I know but if possible I would like to avoid those "Readers" since we don't use those in our lectures.

    And before it worked fine as well :/

    Which means I'd rather fix that JVM problem. I didn't install/fix/repair anything java specific after I installied Oneiric so I'm quite unsure what I got here and what I need.
    Actually, I should ask before blaming the JVM, does this work when run from the command line instead of eclipse?

  8. #8
    Join Date
    Oct 2011
    Beans
    6

    Re: Eclipse console

    The original code from first post, yes.

  9. #9
    Join Date
    May 2009
    Location
    relative
    Beans
    365
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Eclipse console

    Quote Originally Posted by S0m3guy View Post
    The original code from first post, yes.
    In this case you should definitely use run configurations...

  10. #10
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: Eclipse console

    Well a few minutes of research is telling me that eclipse does not allow for use of the Console class. Perhaps somebody else here can help with a solution

    I think your best option for this assignment is to just run for the command line, but in the future, I recommend using the Reader classes anyway, because Console is a newer feature, and the Readers give you some backwards compatibility.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •