1.11M Members

help with console input

 
0
 

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
扡 批 ⁃
 
0
 

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.

 
0
 

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

 
0
 

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.

 
0
 

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 ??

 
0
 

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

 
0
 

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..

 
0
 

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

 
0
 

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

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

 
1
 

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.

 
0
 

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 :)

Question Answered as of 3 Years Ago by NormR1, masterofpuppets and jackmaverick1
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: