Best way to take text input from console

A

Angus

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
 
M

martin.lansler

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.
 
R

Rhino

Angus said:
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.
 
M

martin.lansler

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

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.
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
 
T

Taria

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.

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
}
 
Joined
Apr 11, 2012
Messages
1
Reaction score
0
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top