Hi, I'm very new to Java (started today, though I have 1.5 years in c++) and I've now read a little bit about Java, the thing that I can't find is how to take user input from the keyboard through a console window... How can you go about do that?

I'm looking for something like std::cin in c++. That however doesn't seem to be available in Java.

Thanks,
jt

Dani AI

Generated

As noted, System.in is the underlying input stream; many beginners reach for Scanner for convenience and parsing. 's BufferedReader example is a solid alternative for line-oriented input. The notes below clarify common pitfalls and offer a practical alternative (Console) that is handy for interactive programs.

import java.io.Console;

Console console = System.console();
if (console != null) {
    String line = console.readLine("Enter a number: ");
    try {
        int n = Integer.parseInt(line.trim());
        console.printf("Got: %d%n", n);
    } catch (NumberFormatException ex) {
        console.printf("Not a valid integer%n");
    }
} else {
    // Console is null in many IDEs; fall back to a stream-based reader there.
}

Key practical tips and gotchas:

  • Mixing token-based reads and line-based reads is a frequent source of confusion (for example, reading a number token then immediately reading a line). A robust pattern is to read whole lines and parse them (e.g., Integer.parseInt), catching NumberFormatException for invalid input.
  • Do not close System.in lightly: closing a Scanner or other wrapper that closes its underlying stream will make further reads fail for the lifetime of the JVM. Use try-with-resources only when the program is ending or when the stream truly should be closed.
  • Console offers readLine and readPassword (no-echo) and is ideal for real terminal programs, but System.console() often returns null when running inside IDEs or non-interactive environments—provide a fallback.
  • For simple scripts or learning, Scanner is fine; for high-throughput or competitive-style input, BufferedReader (possibly with StringTokenizer or manual parsing) is faster.

These points complement the examples already posted and should make console input behavior in Java easier to reason about.

Recommended Answers

All 3 Replies

Yup. System.in is the console input stream.
It's common practice to use this with the Scanner class to handle some basic parsing of the input...

Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
or...
int i = sc.nextInt(); // etc etc - see API doc for details of all options

Thank you, that worked perfectly

You can also use the BufferedReader class beside the Scanner class, check j2se tutorial for reading user input for better understanding...

import java.io.*;

public class GetUserInput
{
	public static void main( String [] args )
	{	
		String str = null;
		
		BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
		
		System.out.println( "Enter whatever :: " );

		try 
		{
			str = br.readLine();
		}
		
		catch( IOException exp )
		{
			System.err.println( exp.getMessage() );
		}
		System.out.println( str );
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.