0


Can anyone please explain why the following code behaving strangely:

public class UserInputTest {
    public static void main(String[] args) throws IOException {
        int n=3;
        char[] arr = new char[n];
        for (int i=0; i<n; i++) {
          System.out.println(i+1 + " character :");
          arr[i] = ((char)System.in.read());
        }

        System.out.println("You Entered : ");
        for (int i=0; i<n; i++) {
         System.out.println(arr[i]);
        }
    }
}

OUTPUT:
1 character :
u
2 character :
3 character :
You Entered :
u

I was expecting it to block three times for a user to input values. Any comments ?



Thanks,
Mohit

EMM
  • 1,702
  • 6
  • 30
  • 58
  • Ideas for exploration: 1) print out the *character code* of the "letters you entered"; what happens? 2) change `n` to 6; what happens? –  Jul 01 '12 at 04:55
  • @pst: not really a duplicate. the OP does not need to get the characters as the user types them. – Denis Tulskiy Jul 01 '12 at 08:08

3 Answers3

3

Reading character from console has above mentioned enter issue. So, try to read as string:

public static void main(String args[]){


        int n=3;
        char[] arr = new char[n];
        Scanner in = new Scanner(System.in);
        for (int i=0; i<n; i++) {
          System.out.println(i+1 + " character :");
          String s1 = in.nextLine();
          arr[i] = s1.charAt(0);
        }

        System.out.println("You Entered : ");
        for (int i=0; i<n; i++) {
         System.out.println(arr[i]);
        }
    }
Arpssss
  • 3,630
  • 5
  • 33
  • 75
  • I am aware of Scanner class. But I am interested in knowing how to set the program in my question right. – EMM Jul 01 '12 at 05:18
1

You typed a character followed by the Enter key. The second read returns the Enter.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • What is the right way to enter characters in this case? – EMM Jul 01 '12 at 04:09
  • 1
    @mohit: you already have enough hints from one of the comments: the other two characters you're reading are probably \r and \n. Just skip them. Or use Scanner. – Denis Tulskiy Jul 01 '12 at 08:10
  • 1
    @mohit That's not only the right way, it's the only way to enter characters at the console. The thing is to write your program to expect what you actually get and process it correctly. – user207421 Jul 01 '12 at 08:38
  • @EJP So it means, If I have to enter 3 chars I have to prepare myself for 6 chars , 3 real ones and 3 for Enter ? – EMM Jul 02 '12 at 06:53
  • @mohit I really think you can answer that yourself given these answers and the continued existence of your computer. – user207421 Jul 03 '12 at 09:37
1

The right way to enter individual characters is to use a GUI such as a Swing GUI. You can't enter individual characters with the standard Java console.

You can use a non-standard console for this such as JCurses but not the standard Java console.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
  • See this post if think it's not possible: http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it – Shahryar Jul 01 '12 at 04:56
  • @Shahryar Well, the Swing UI bypasses the standard console (and the associated line buffer) for input, so.. that link only vaguely applies. However, a Swing app is also hardly a console app .. –  Jul 01 '12 at 04:58
  • @Shahryar On another note, that would be a *good* question to close-as-duplicate of. –  Jul 01 '12 at 04:59
  • @Shahryar: I didn't say that it is not possible, just not possible with the standard Java console (re-read my answer), and your link confirms my answer. Also a few answers in your link uses the JCurses which is not part of the standard console. And please undo the down vote unless you can *prove* that my statement is wrong with a better link (you can't). – Hovercraft Full Of Eels Jul 01 '12 at 05:05
  • You are right, it's not possible with 'standard' java console. – Shahryar Jul 01 '12 at 05:11
  • @Hovercraft: I tried to remove it but it's not possible, my vote is locked in unless you edit your answer!!! – Shahryar Jul 01 '12 at 05:17
  • @pst: the OP didn't stipulate explicitly in the original question that they must use a console. Rather they implied that they're trying to read in individual entered characters. – Hovercraft Full Of Eels Jul 01 '12 at 05:40
  • the OP says he was expecting the program to block three times for three characters, so he has no need in gui or jcurses or whatever so far – Denis Tulskiy Jul 01 '12 at 08:06