Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

input from keyboard  RSS feed

 
Alan H
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++, we can input any type values (int, double, string,...) from keyboard easily. In java, System.out.read() only reads one character from keyboard and returns the ASCI value in integer. We have to convert the integer to character.
Is there any easier way in jave to read int (or string,....) from keyboard?
In Core Jave 2, the author wrote a console class to do this job. Why didn't Sun's engineer to fix this problem?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason this was not included is that real-world Java applications hardly ever need this sort of functionality. The only Java applications which I have ever seen which take input from the keyboard using System.in are teaching excercises copied from courses in other languages.
That's not an absolute justification for leaving this sort of thing out, of course, but it's pretty powerful if the aim is to keep the size of the core Java classes down. Java has some pretty powerful tools for building this sort of thing, too (look at StreamTokenizer, BufferedReader.readLine() and NumberFormat.parse() for example).
Do you actually need to do this? If you do, I guess you can use the classes from Core Java 2 which you mentioned, or have a go at writing something more specific to your requirements.
 
Rajesh Tatooskar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pl try this:
-----------
import java.io.*;
public class Inp
{
public static void main(String args[]) throws IOException
{
BufferedReader keyboardInput;
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
String newLine;
newLine = keyboardInput.readLine();
System.out.println("You Typed: "+newLine);
}
}
-----------
Originally posted by Alan H:
In C++, we can input any type values (int, double, string,...) from keyboard easily. In java, System.out.read() only reads one character from keyboard and returns the ASCI value in integer. We have to convert the integer to character.
Is there any easier way in jave to read int (or string,....) from keyboard?
In Core Jave 2, the author wrote a console class to do this job. Why didn't Sun's engineer to fix this problem?

 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Rajesh!
I was searching for something like this. I was doing it in a big code snipppet earlier and now if we need any int, char, float, double...etc primitives we can do it in the way that I have added with ur code.I m showing a sample to take input of int only.
code:

Hope all the ranchers will be helped and think taking keyboard input of primitives in Java is not too much harder than in C/C++.
Thnx again Rajesh.

------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 13, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!