We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,985 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

help with console input

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
扡 批 ⁃
4
Contributors
10
Replies
15 Hours
Discussion Span
1 Year Ago
Last Updated
11
Views
Question
Answered
baby_c
Junior Poster
113 posts since May 2010
Reputation Points: 47
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

jackmaverick1
Posting Whiz in Training
223 posts since May 2010
Reputation Points: 6
Solved Threads: 10
Skill Endorsements: 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.

masterofpuppets
Posting Whiz in Training
272 posts since Jul 2009
Reputation Points: 20
Solved Threads: 74
Skill Endorsements: 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 ??

baby_c
Junior Poster
113 posts since May 2010
Reputation Points: 47
Solved Threads: 0
Skill Endorsements: 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

baby_c
Junior Poster
113 posts since May 2010
Reputation Points: 47
Solved Threads: 0
Skill Endorsements: 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..

baby_c
Junior Poster
113 posts since May 2010
Reputation Points: 47
Solved Threads: 0
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

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

baby_c
Junior Poster
113 posts since May 2010
Reputation Points: 47
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

baby_c
Junior Poster
113 posts since May 2010
Reputation Points: 47
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by NormR1, masterofpuppets and jackmaverick1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0981 seconds using 2.82MB