LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java - reading from stdin (https://www.linuxquestions.org/questions/programming-9/java-reading-from-stdin-276775/)

greg108 01-12-2005 12:36 AM

java - reading from stdin
 
I know how to read a String from standart input this way:
Something like:

inStr = reader. readLine(); //reader is a BufferedReader type

and then if I want just one character:

myChar = inStr.charAt(0);

and if I want double:

myDouble = Double. parseDouble(inStr);

But how can I read one character (of char type) or a double without those conversions?

csfalcon 01-12-2005 02:28 AM

Code:

DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
               
        try
        {
                in.readChar();
                in.readDouble();
        }
        catch (IOException ioe)
        {
        }


greg108 01-12-2005 02:59 PM

Thanks, but when I do this and print the character that I just entered then the output is not a character but a bunch of digits (some kind of binary stream)

I need to read a character and test it with a switch statement.

csfalcon 01-13-2005 02:34 PM

hmm, sorry about that. Seems like readDouble() reads in IEEE representation of double and readChar() read in unicode? Not sure...

But looks like you would have to read it as a string first and parse out the different components.

jlliagre 01-13-2005 03:29 PM

To read characters from a file, try:
Code:

    BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));;
    int i;
    while((i=br.read())!=-1)
    {
      char c=(char)i;
      ...;
    }



All times are GMT -5. The time now is 06:10 AM.