What is the mistake in the following code?
while ((char t==(char) System.in.read())!='0')
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You can not declare a new variable in a
whileloop.You’d have to declare the variable before and outside of the loop, so perhaps something like this:
In this sense,
forloop is unique: you can in fact declare a new local variable whose scope is limited to that loop: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
Scannerto read numbers from standard input, terminating at 0. It then prints the sum of those numbers. It handles invalid input gracefully usinghasNextInt()instead ofInteger.parseInt/NumberFormatException.Here’s an example session: