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.


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();
int i = sc.nextInt(); // etc etc - see API doc for details of all options

Edited 5 Years Ago by JamesCherrill: n/a

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 :: " );

			str = br.readLine();
		catch( IOException exp )
			System.err.println( exp.getMessage() );
		System.out.println( str );
This question has already been answered. Start a new discussion instead.