Results 1 to 4 of 4
  1. #1
    RomiOrchids is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default "Console.Readline();" of C# in Java?

    Hi there. I want to do a method which allows me to read a line of data input and returns a String.
    In C# I would simply do the following:

    static string EnterData() {
    return Console.ReadLine();
    }

    but in Java it just keeps returning an error. One of the things I tried was this:

    static String EnterData(){
    java.io.DataInput in = new java.io.DataInputStream(System.in);
    String Answer=in.readLine();
    return Answer;
    }

    Which returns: "Unhandled exception type IOException" over the "in.readLine();"
    I read I could add something like "throw IOException". If I do that, I get the "Unhandled exception type IOException" everytime I try to use the method EnterData.

    How do I fix this problem? Thanks in advance.

    (Yup, my english sucks ._.).

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: "Console.Readline();" of C# in Java?

    The method is called readLine (Java is case sensitive).
    The method is non-static. If you do not know what that means you need to do some revision.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: "Console.Readline();" of C# in Java?

    BTW the most common way to get user input these days is to use the Scanner class.

  4. #4
    matejm1994 is offline Member
    Join Date
    Dec 2010
    Posts
    14
    Rep Power
    0

    Default Re: "Console.Readline();" of C# in Java?

    Scanner in = new Scanner(System.in);
    String a = in.nextLine();

    This sample is for reading Strings, if you want to read integers, you would write int a=in.nextInt();
    That is a Scanner. And this is the simplest way to read from console.

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. How To Get Java To "Die" [Console App]
    By EdenC in forum New To Java
    Replies: 10
    Last Post: 08-24-2011, 07:15 PM
  4. Java Console's "Install Shortcuts" grayed Out
    By a_gunslinger in forum New To Java
    Replies: 0
    Last Post: 03-01-2011, 04:21 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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