Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I read char,int,double, string from a keyboad ( Terminal ).

Posted on 1998-07-22
7
Medium Priority
?
255 Views
Last Modified: 2010-03-30
Hi :

How do I read Integer, Double, Char and String data type cleanly from the Keyboard Terminal. I have a program which requires user input and I dont want to use Applet for testing. I can do this in a crude way but I want a clean way to handle this.

What I have done to handle this is to read each line and then parse it using StringTokenizer to read doubles. But readLine in DataInputStream is also a depreciated. So I really dont want to use this. Is there a clean method to do this??

Also How do I handle if I have two double on one line seperated by space or two double on two seperate lines??

I have a following sample program but it does not work.
import java.io.*;
public class IOTest
{
  public static void main(String args[])
  {
    DataInputStream dis = new DataInputStream ( System.in );
    double d = dis.readDouble() ;
     System.out.println ( "Double d = " +d1  ) ;
  } catch ( IOException io )
  {
      System.out.println ( "readDouble Exception: "+io.toString() ) ;
  }
 
  }
}
0
Comment
Question by:sshah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:sshah
ID: 1227549
Edited text of question
0
 

Author Comment

by:sshah
ID: 1227550
Adjusted points to 100
0
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 1227551
Here is the note from the JDK 1.1 documentation:

Note: readLine() is deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form

DataInputStream d = new DataInputStream(in);
with
BufferedReader d = new BufferedReader(new InputStreamReader(in));

This then will allow the use of an "approved" kind of readLine.


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:gadio
ID: 1227552
sshah, use InputStreamREader to read from the stdin, and then feed it to a StreamTokenizer to break the input to sperate numbers.
0
 

Author Comment

by:sshah
ID: 1227553
Good Info. But Is there a better way to read compared to reading it as a string and then converting that string to double?? Can I directly read it as a double?? Can I use any of the methods of the of the interface readBoolean, readChar, readFloat or readDouble??. Or any other way to go around rather then reading first string and then converting to appropriate type??.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1227554
No. The readDouble stuff is a portable way of retreiving data from a file. The expected format is some kind of binary (produced by a corresponding write type call of DataOutputStream). When acquiring information from the console you are inherently dealing with character data.

0
 

Author Comment

by:sshah
ID: 1227555
Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question