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.


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

			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, learning, and sharing knowledge.