Best way to take text input from console

Discussion in 'Java' started by Angus, Nov 16, 2006.

  1. Angus

    Angus Guest

    Hello

    I am fairly new to Java and want to start with simple console programs (ie
    text based - not GUI).

    I can output easily using System.out.println - but been searching web as to
    how to get input from console and finding all sorts of different ways. What
    is the elegant way? Or most accepted/standard/easiest way to do it?

    I thought I should be able to use something like System.in.read ? No?

    Angus
     
    Angus, Nov 16, 2006
    #1
    1. Advertising

  2. Angus

    Guest

    Try this:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    public class Sysin {
    public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new
    InputStreamReader(System.in));
    System.out.println("Enter you name: ");
    String name = br.readLine();
    System.out.println("Hello " + name);
    }
    }

    Regards,
    Martin.

    Angus wrote:
    > Hello
    >
    > I am fairly new to Java and want to start with simple console programs (ie
    > text based - not GUI).
    >
    > I can output easily using System.out.println - but been searching web as to
    > how to get input from console and finding all sorts of different ways. What
    > is the elegant way? Or most accepted/standard/easiest way to do it?
    >
    > I thought I should be able to use something like System.in.read ? No?
    >
    > Angus
     
    , Nov 16, 2006
    #2
    1. Advertising

  3. Angus

    Rhino Guest

    "Angus" <> wrote in message
    news:ejicnr$2ff$1$...
    > Hello
    >
    > I am fairly new to Java and want to start with simple console programs (ie
    > text based - not GUI).
    >
    > I can output easily using System.out.println - but been searching web as
    > to
    > how to get input from console and finding all sorts of different ways.
    > What
    > is the elegant way? Or most accepted/standard/easiest way to do it?
    >
    > I thought I should be able to use something like System.in.read ? No?
    >

    As you've noticed, there are several ways that work and are in common usage.

    One approach that seems quite popular for those on Java 1.5 or later is to
    use the Scanner class. I know a guy who is just learning Java at college and
    they use Scanner for all their console I/O.

    --
    Rhino
     
    Rhino, Nov 16, 2006
    #3
  4. Angus

    Guest

    One more thing...

    In Java 5 one can make a static import:
    import static java.lang.System.out;

    allowing one to write:
    out.println("Enter you name: ");

    /Martin Lansler

    wrote:
    > Try this:
    >
    > import java.io.BufferedReader;
    > import java.io.InputStreamReader;
    >
    > public class Sysin {
    > public static void main(String[] args) throws Exception {
    > BufferedReader br = new BufferedReader(new
    > InputStreamReader(System.in));
    > System.out.println("Enter you name: ");
    > String name = br.readLine();
    > System.out.println("Hello " + name);
    > }
    > }
    >
    > Regards,
    > Martin.
    >
    > Angus wrote:
    > > Hello
    > >
    > > I am fairly new to Java and want to start with simple console programs (ie
    > > text based - not GUI).
    > >
    > > I can output easily using System.out.println - but been searching web as to
    > > how to get input from console and finding all sorts of different ways. What
    > > is the elegant way? Or most accepted/standard/easiest way to do it?
    > >
    > > I thought I should be able to use something like System.in.read ? No?
    > >
    > > Angus
     
    , Nov 16, 2006
    #4
  5. Angus

    Taria Guest


    > One approach that seems quite popular for those on Java 1.5 or later is to
    > use the Scanner class. I know a guy who is just learning Java at college and
    > they use Scanner for all their console I/O.
    >
    > --
    > Rhino


    I'm just learning java this semester and Scanner is pretty quick and
    easy to use. The way to implement this into your program is to do the
    following things:

    1) type this line at the top of your program before any variables,
    classes, main body is declared:
    import java.util.Scanner;

    2) You can use the API manual that will tell you all the methods that
    are connected with Scanner but here are examples how you can use it to
    take in input from a user.

    Scanner input = new Scanner(System.in);
    System.out.print("Enter your sentence: ");
    String sentence = input.nextLine();

    If you want to read in just a word and not a series of words, then
    change "nextLine" to "next". To input an integer then use for example:

    System.out.print("\nEnter the choice, followed by 2
    integers: ");
    String choice = input.next();

    // evaluate choice then input 2 integers, expected


    if (choice.equalsIgnoreCase("q")) {
    quit = true;
    System.out.println ("Adios mi amigo.");
    }
    else
    {
    int x = input.nextInt();
    int y = input.nextInt();
    // ... morec ode here
    }
     
    Taria, Nov 17, 2006
    #5
  6. Angus

    salomonsk8r2003

    Joined:
    Apr 11, 2012
    Messages:
    1
    Why not do this?

    I am brand new to Java (and programming in general) so go easy on me.

    Is there a reason why noby is recommending the Console object? Instead of:

    BufferedReader variableName = new BufferedReader(new InputStreamReader(System.in));

    Why not use this:

    Console variableName = System.console();


    ? It is much shorter and even has the same methods such as .readLine() and another cool one called .readPassword() that lets the user type in the command line without it echoing back.
    I learned about it from the oracle java tutorial. Is it something I shouldnt be using?
     
    salomonsk8r2003, Apr 11, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Replies:
    2
    Views:
    996
    Egor Bolonev
    Jun 30, 2003
  2. Sean W
    Replies:
    2
    Views:
    2,297
    Oliver Wong
    Aug 31, 2006
  3. arnuld
    Replies:
    173
    Views:
    3,287
    Tim Rentsch
    Oct 9, 2008
  4. Tim Mcd
    Replies:
    2
    Views:
    96
    Tim Mcd
    Feb 3, 2009
  5. Deiva Rajasingam
    Replies:
    3
    Views:
    215
    Deiva Rajasingam
    Jul 29, 2009
Loading...

Share This Page