Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:17:11+00:00 2026-05-15T06:17:11+00:00

What is the mistake in the following code? while ((char t==(char) System.in.read())!=’0′)

  • 0

What is the mistake in the following code?

 while ((char t==(char) System.in.read())!='0')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:17:12+00:00Added an answer on May 15, 2026 at 6:17 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VS2008 Code Analysis will flag a spelling mistake in an identifier using the IdentifiersShouldBeSpelledCorrectly
I have the following code at the head of a method: BigInteger foo =
I started debugging some code attempting to find my mistake. When I attempt to
I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I
I got the following output with error message while executing the program that uses
The very common beginner mistake is when you try to use a class property
A common mistake when configuring the compilation/linking/etc. settings in VC++ 2008 is to set
I seem to make this mistake every time I set up a new development
I am sure making a silly mistake but I can't figure what: In SQL
I must be dumb (and I'm sure I am making mistake but can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.