1,105,592 Community Members

help with console input

Member Avatar
baby_c
Junior Poster
116 posts since May 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
扡 批 ⁃
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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.

Member Avatar
jackmaverick1
Posting Whiz in Training
228 posts since May 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
masterofpuppets
Posting Whiz in Training
272 posts since Jul 2009
Reputation Points: 5 [?]
Q&As Helped to Solve: 74 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
baby_c
Junior Poster
116 posts since May 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 ??

Member Avatar
baby_c
Junior Poster
116 posts since May 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
baby_c
Junior Poster
116 posts since May 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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..

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
baby_c
Junior Poster
116 posts since May 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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.

Member Avatar
baby_c
Junior Poster
116 posts since May 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 2 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: