user466534
user466534

Reputation:

Problem with loop in Java

What is the mistake in the following code?

 while ((char t==(char) System.in.read())!='0')

Upvotes: 3

Views: 344

Answers (4)

Pete Kirkham
Pete Kirkham

Reputation: 49311

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.

Upvotes: 3

polygenelubricants
polygenelubricants

Reputation: 383676

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

Upvotes: 8

aioobe
aioobe

Reputation: 420921

This is probably what you intended to write.

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

Upvotes: 7

kennytm
kennytm

Reputation: 523164

 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;
    ...

Upvotes: 6

Related Questions