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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do console input

Status
Not open for further replies.

GEAK

Instructor
Feb 1, 2001
90
US
I've been coding in C/C++ and VB for close to 20 years. I've been teaching C++ for about 8 and VB.NET for the past year. In the process of trying to learn Java and just trying to write a simple program to do console I/O. I've got two texts that I'm using as a reference and one uses their own class for doing input - I'd rather avoid specialized classes and get down to the roots of what their class is doing. The other text uses lines like this:

Code:
char Letter;
Letter = (char)System.in.read();

to perform character input but when I try compiling it I'm getting exceptions. I wrap the exception causing code in a try/catch block and the subsequent lines start throwing exceptions because "Letter might not be initialized"?!?

Doesn't java have a single line command for doing input like C/C++/VB?
Code:
[COLOR=green]/* C */[/color]
char Letter;
Letter = getch();
Code:
[COLOR=green]// C++[/color]
char Letter;
cin >> Letter;
Code:
[COLOR=green]'VB.Net[/color]
Dim Letter As Char
Letter = Convert.ToChar(Console.ReadLine())
 
simple solution:

Code:
// letter initialised to make the compiler happy
char letter = 0;
try
{
	letter = (char)System.in.read();
}
catch (EOFException welcomed)
{
	break;
}
catch (IOException ioe)
{
	System.err.println (ioe.getMessage ());
}


seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top