3

What is the mistake in the following code?

 while ((char t==(char) System.in.read())!='0')
1
  • 1
    If you ask such a question it always helps if you specify the error or misbehavior you get. Commented Jun 15, 2010 at 8:07

4 Answers 4

8

You can not declare a new variable in a while loop.

    while (boolean always = true) {
    } // DOES NOT COMPILE!!!

You'd have to declare the variable before and outside of the loop, so perhaps something like this:

    boolean always = true;
    while (always) {
        break;
    } // compiles fine!

    // always is still in scope after the loop!
    always = !always;

In this sense, for loop is unique: you can in fact declare a new local variable whose scope is limited to that loop:

    for (boolean always = true; always; ) {
        break;
    } // compiles fine!

    // always is no longer declared after the loop!
    always = !always; // DOES NOT COMPILE!

That said, looking at what you're doing, you may want to look at java.util.Scanner. I suspect that it will serve your need much better.


Example

Here's an example of using Scanner to read numbers from standard input, terminating at 0. It then prints the sum of those numbers. It handles invalid input gracefully using hasNextInt() instead of Integer.parseInt/NumberFormatException.

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter numbers (0 to end):");
    int sum = 0;
    int number;
    do {
        while (!sc.hasNextInt()) {
            System.out.println("I'm sorry, that's not a number! Try again!");
            sc.next();
        }
        number = sc.nextInt();
        sum += number;
    } while (number != 0);
    System.out.println("The sum of those numbers is " + sum);

Here's an example session:

Enter numbers (0 to end):
1
3
-1
five
I'm sorry, that's not a number! Try again!
2
0
The sum of those numbers is 5

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the use of Scanner, probably the only JDK IO class that isn't horribly painful to use
You could point out that a { is valid "anywhere" inside a method. `void method() { statements; { boolean always = true; while(always) {} } ... // always not available``
7

This is probably what you intended to write.

char t;
while ((t = (char) System.in.read()) != '0') {
    //...
}

Comments

6
 while ((char t==(char) System.in.read())!='0')
 //            ^^  should be 'char t = ...'

This loop can be rewritten more clearly as

 while (true) {
    char t = (char) System.in.read();
    if (t == '0')
      break;
    ...

2 Comments

I would not agree with you. That is a standard technique of reading streams, and I see no problems with that line. Maybe it does make it more clear by rewriting it, but anyone who ever worked with streams would easily understand the "compact" code too. The way @aioobe did it is enough.
I also dislike this way of constructing a loop, I prefer to see the condition in the loop condition, rather than somewhere within the loop body.
3

char t is a statement which declares a variable rather than an expression; it has no value to compare with the == operator. You probably also meant to use assignment rather than equality, but unlike C++ the declaration does not have a value.

The easiest way of restricting the scope of a variable in a loop is to use for instead, which specifically allows you to declare variables.

for (char t; ( t = (char) System.in.read() ) != '0'; )
   // loop body involving t

However, some company's guidelines don't allow mutating operators in boolean expressions, such as the read and the assignment, and would prefer you to separate them out onto several lines. Personally I find restricting the scope more important.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.