Why does the compiler complain "while expected" when I try to add more code?
Posted
by
user1893578
on Stack Overflow
See other posts from Stack Overflow
or by user1893578
Published on 2012-12-11T04:47:10Z
Indexed on
2012/12/11
5:04 UTC
Read the original article
Hit count: 119
java
Write a program with a word containing @ character as an input. If the word doesn't contain @, it should prompt the user for a word with @. Once a word with @ is read, it should output the word then terminate.
This is what I have done so far:
public class find {
public static void main(String[] args) {
System.out.println(" Please enter a word with @ ");
Scanner scan = new Scanner(System.in);
String bad = "@";
String word = scan.next();
do
if (!word.contains(bad))
System.out.println(" Please try again ");
else
System.out.println(" " + word);
while (!word.contains(bad));
}
}
I can get it to terminate after a word containing "@" is given as input, but if I try to add a Scanner to the line after "please try again", it says while expected
.
© Stack Overflow or respective owner