Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using Console.readInt() in JDK

Options
  • 03-05-2017 8:57pm
    #1
    Registered Users Posts: 8,322 ✭✭✭


    It's over 15 years since I did Java programming in uni, I kept all my old projects and tutorials and am currently trying to revisit them for nostalgia reasons. I'm very rusty :) I have downloaded and installed the JDK but when I try to compile/run my old programs I am getting an error related to Console.readInt() or Console.readChar() etc.

    I can compile and run programs as long as no input is required from the user.

    Here is an example of one where the user is supposed to enter their ID code:
    int code;
    
    System.out.println("Enter your ID code");
    
    code=Console.readInt();
    

    I open a command prompt, navigate to the jdk/bin directory (where I have also placed my .java files) and type java or javac etc. and get an error related to Console.

    I am using the latest JDK version with Windows 10 and have also tried a much older release with Windows 2000. Same story with both.

    Anyone have any suggestions for what embarrassing silly thing I am doing wrong?


Comments

  • Registered Users Posts: 778 ✭✭✭pillphil


    Just a bit of advice for future posts, it's best to include the actual error message, it makes it a lot easier to help.

    This one seems easy enough though.

    http://stackoverflow.com/questions/34008641/console-readint-java-programming
    This is because readInt isn't a function of Console. What you are looking for is:
    //Read input from console
    String input = System.console.readLine()
    
    //Convert string to integer
    Integer.parseInt(input)
    


    http://stackoverflow.com/questions/33521839/cannot-find-symbol-system-console-readint
    Use Scanner and Scanner.nextInt() method to take only Integer as input from the user.
    Scanner sc = new Scanner(System.in);
    int phonenumber = sc.nextInt();
    
    If the user gives an input which is not an integer, it will throw an InputMismatchException.

    I don't know if one method is preferred over the other.


  • Registered Users Posts: 8,322 ✭✭✭BrianD3


    Thanks pillphil, I think I was missing a Console class. Some "non standard" or obsolete class that doesn't come with JDK? Note that it is Console not System.console, I think that is significant.

    Or else I configured JDK incorrectly in terms of path etc. and it couldn't be found.

    In any case, I found a file Console.java on the web, compiled it, placed it in the bin folder and my programs are now working with it. Cool. This class does have readInt() readDouble() readChar() etc.

    I could have started changing code based on that stackoverflow page and used Scanner etc. But the point is the programs were running 15 years ago on my old PC and I wanted them running again unchanged on my current PC.

    Thanks for the help.


  • Registered Users Posts: 778 ✭✭✭pillphil


    It might have been a class provided by your uni? DCU by any chance?
    http://computing.dcu.ie/~jmorris/ca213/ConsoleClass.pdf


Advertisement