Solved

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

Posted on 1998-07-22
7
237 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 100 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

696 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