Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to read user input, cmd line style?

Status
Not open for further replies.

hbutt

Programmer
Apr 15, 2003
35
GB

hi, is there anyway i can read input direct from the screen. i am not using a gui just command line. in c++ i think it is something like system.io and c is something like scanf.
any help is much appreciated.
thanx
hgsb
 
you can download a keyboard class from here..


you can use the various methods of the keyboard class as follows..

Keyboard.readString()
Keyboard.readWord()
Keyboard.readChar()
Keyboard.readBoolean()
Keyboard.readInt()
Keyboard.readLong()
Keyboard.readFloat()
Keyboard.readDouble()

so for example..

String s = Keyboard.readString();
double dbl = Keyboard.readDouble();

hope this helps.
 
It is not as easy as in C or C++, but it can be done. See example below...hope this helps.

Code:
import java.io.*;

public class ConsoleReader
{
	public static void main(String[] args)
	{

		BufferedReader readConsole = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter a number: ");

		String number 	= null;
		String aNumber 	= null;
		int a = 0;
		int b = 0;
		int c = 0;
		try
		{
			number 	= readConsole.readLine();

			a = Integer.parseInt(number);



		}
		catch(IOException e)
		{
			System.out.println( e );
		}
		try
		{
			System.out.print("Enter another number: ");
			aNumber = readConsole.readLine();
			b = Integer.parseInt(aNumber);
		}
		catch(IOException e)
		{
			System.out.println( e );
		}
		c = a+b;
		System.out.println("The numbers you enter were " + a +" and " + b + ". Your total is: " + c + ".");
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top