• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

input from keyboard

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic