Hey friends.Need a help from you again.

I'm trying to study about streams and I wrote small code for get input from command line and print it in the screen,but It prints weird characters like Japanese :O . Can anyone explain what I did wrong...

import java.io.*;
public class ReadBytes {

  
    public static void main(String[] args) {
        
        try{
          //  FileInputStream file =new FileInputStream(System.in);
            BufferedInputStream buff = new BufferedInputStream(System.in);
            DataInputStream data = new DataInputStream(buff);
            

            
            int count=0;

            try{

            while(true){
                char input =data.readChar();
                System.out.print(input+" ");
                count++;
              }
            }
            catch(EOFException eof){
                buff.close();

            }
            
            System.out.println("\nBytes read:  " +count);
            
        }catch(IOException e){
        System.out.println("Error -- "+e.toString());
        }



    }

}
//output

baby C
扡 批 ⁃

You need to do some more research on the classes you are using.
For example what does the API doc for the DataInputStream class say it is used for?
The data that is read is translated into an internal format, not into Strings.

What happened when you used the BufferedInputStream?

How about trying the BufferedReader class or the Scanner class?

Do a Search on this forum for lots of code samples.

I STRONGLY suggest using the scanner for this, as it is much easier to use, though it is slightly more limited than other things (in my opinion).

These are declared by using:

Scanner scanner=new Scanner(System.in);

You may get input from the user by using:

scanner.nextInt();
scanner.nextLine();

You should goto: http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html for more information about the Scanner Class

Member Avatar for masterofpuppets

Hi,

As jackmaverick1 said, the Scanner is simpler to use, depending on what you want to do - if you just need to enter commands, use scanner.nextLine() and then parse the command. As for the DataInputStream, I think you should use it for reading bytes and file transfer.

You need to do some more research on the classes you are using.
For example what does the API doc for the DataInputStream class say it is used for?
The data that is read is translated into an internal format, not into Strings.

What happened when you used the BufferedInputStream?

How about trying the BufferedReader class or the Scanner class?

Do a Search on this forum for lots of code samples.

thank you for reply..Actually I was going to study how DataInputStream class is work. But of course I translate the input into byte code. isn't it. I skipped that unknowingly.. Is there any way to translate back this byte code to String ??

I STRONGLY suggest using the scanner for this, as it is much easier to use, though it is slightly more limited than other things (in my opinion).

These are declared by using:

Scanner scanner=new Scanner(System.in);

You may get input from the user by using:

scanner.nextInt();
scanner.nextLine();

You should goto: http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html for more information about the Scanner Class

thank you very much.. I got the point

Hi,

As jackmaverick1 said, the Scanner is simpler to use, depending on what you want to do - if you just need to enter commands, use scanner.nextLine() and then parse the command. As for the DataInputStream, I think you should use it for reading bytes and file transfer.

thanks.. I got it..

DataInputStream is used to read files/streams written by the DataOutputStream class.

DataInputStream is used to read files/streams written by the DataOutputStream class.

so is it can't be used for read user inputs ??

it can't be used for read user inputs

Mostly No. It is difficult to enter the correct format for the data from the keyboard.
There are other better ways as mentioned above.

commented: from baby_C +3

Mostly No. It is difficult to enter the correct format for the data from the keyboard.
There are other better ways as mentioned above.

ok thank you.. I think this will help me to improve.. thanks again..I'll mark this as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.