-3

I used the following code segment to get 10 inputs from console in java and tried to print them for example in *******12 format. In run-time, I entered 12,13,14,15 as inputs and then the program terminated. Now there are 3 questions:

  1. Why this code gets only 5 inputs instead 10?
  2. Why this code prints for example 49,52,10 for input 14?
  3. What's the solution?

Code:

public static void main(String[] args) {
    for (int i = 0 ; i <10 ;i++){
        try {
            int j= System.in.read();
            System.out.println("**********"+j);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

and the output was:

12
**********49
**********50
**********10 13
**********49
**********51
**********10 14
**********49
**********52
**********10 15
**********49

BUILD SUCCESSFUL (total time: 14 seconds)

Roman C
  • 49,761
  • 33
  • 66
  • 176
zari
  • 1,709
  • 1
  • 12
  • 18
  • What does `read()` do? You're using it, so I'll assume you've looked it up. – Sotirios Delimanolis Feb 27 '14 at 22:20
  • You might want to read this http://stackoverflow.com/questions/15446689/what-is-the-use-of-system-in-read-in-java – piacente.cristian Feb 27 '14 at 22:23
  • "Why this code gets only 5 inputs instead 10?". Because you entered 10 bytes, which is what in.read() reads. – Ted Bigham Feb 27 '14 at 22:25
  • -1 for not reading the javadoc – Franz Ebner Feb 27 '14 at 22:27
  • Dear Franz I read the javadoc and I didn't find the answers. It's better to help me instead of giving -1. -1 does not solve my problem. – zari Feb 27 '14 at 22:30
  • -1 isn't there to help you solve this. It's there to tell you that the community thinks you didn't put or show enough effort before asking the question. The javadoc says `Reads the next byte of data from the input stream.` You can look up what a byte and what an input stream are. – Sotirios Delimanolis Feb 27 '14 at 22:32
  • So I discovered that you don't want to solve a problem. You want to argue because you have lot of time!!! and I don't have. Please let others solve the problem and help me. bye – zari Feb 27 '14 at 22:37
  • 1
    As members of the community, we try to improve the quality of StackOverflow. Some members have judged that your question is poor and have downvoted it. That's all. – Sotirios Delimanolis Feb 27 '14 at 22:40
  • The members must regards others. Maybe at that time I concentrated on one aspect of the problems and now you and others helped me to focus on the other aspects. So I tried to solve the problems and they solved! – zari Feb 27 '14 at 22:44
  • I'm happy you got your answer. – Sotirios Delimanolis Feb 27 '14 at 22:46

5 Answers5

7

49, 52, and 10 are the ASCII character codes for the characters you typed, 14Enter.

You may continue to use System.in.read(), and process each character as it arrives. You would do something like the following:

  • collect the characters that are typed
  • look for digits typed followed by Enter
  • convert the ASCII codes of the digits to a decimal number

This is, of course, exactly what Scanner.nextInt() does for you.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
3

You would want to use a Scanner and the nextInt() method. Try this:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    for (int i = 0; i < 10; i++) {
        int j = s.nextInt();
        System.out.println("**********" + j);
    }
}

Note that read() is a method of the InputStream class. You usually don't want to access an InputStream directly.

nrubin29
  • 1,522
  • 5
  • 25
  • 53
  • Yeah I know that class Scaner is another solution. But I need a solution using System.out.read(). So what's the problem in above code? – zari Feb 27 '14 at 22:23
  • If you mean `System.in.read()`, there's not reason to. A Scanner is an easy and safe way to access an `InputStream`. – nrubin29 Feb 27 '14 at 22:25
  • Meaning use it in inheritance because it's abstract class? – zari Feb 27 '14 at 22:33
1

In case you are running this outside of an IDE

String input = System.console().readLine();
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
1

When you write one character and then press enter, it translates into 3 bytes: character_code + \r + \n. In your case it takes 3 iterations for i variable.

For 2 characters you type it translates into: character_code + character_code + \r + \n. It takes 4 iterations of i.

Btw, I have another output for the same code:

12           
**********49 
**********50 
**********13 
**********10 
13           
**********49 
**********51 
**********13 
**********10 
14           
**********49 
**********52 
senyor
  • 332
  • 4
  • 9
0

This should also help incase you want to use the bufferedReader

public static void main(String[] args) throws NumberFormatException, IOException {
    BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

    for(int i = 0; i<10; i++){
        int inp = Integer.parseInt(stdin.readLine());
        System.out.println("----------- "+inp);
    }
1baga
  • 340
  • 4
  • 16