A simple mistake I can't fix
String userInput;
int lowerCase = 0, upperCase = 0;
Scanner scan = new Scanner (System.in);
System.out.println("Enter a word (type exit to finish) : ");
userInput = scan.next();
while (userInput != exit){
for (int i = 0; i<userInput.length(); i++){
for (char lower = 'a'; lower <= 'z'; lower++){
if (userInput.charAt(i) == lower){
lowerCase++;
}
}
}
for (int j=0; j<userInput.length(); j++) {
for(char upper='A'; upper<='Z'; upper++) {
if (userInput.charAt(j) == upper)
{
upperCase++; }
userInput = scan.next();
}
}
}
System.out.println("Total number of lower case letters = " + lowerCase);
System.out.println("Total number of upper case letters = " + upperCase);
}
}
Error result
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
exit cannot be resolved to a variable
at Q3.main(Q3.java:19)
How do I fix this?
Re: A simple mistake I can't fix
First, please put [code=java] before your code and [/code] after your code.
Second the error message tells you what is wrong and points you to the general location of the error.
exit cannot be resolved to a variable
That means you typed the 4 letters exit somewhere (see below) and the compiler has no idea what those letters mean.
at Q3.main(Q3.java:19)
That means line 19 in your source code.
From what I see, the line is:
while (userInput != exit){
edit:
Since this syntax error is not your only problem, here is some suggested reading.
Re: A simple mistake I can't fix
Hello,
Thanks for your insight. The only problem my program has is the while loop. The program should keep on asking userInput and calculate uppercase and lowercase characters until the user types in exit. But the program shouldn't calculate the uppercase and lowercase characters of the word exit when user inputs exit. Should I assign exit to a string s1 and replace the while loop as
The program runs fine if there isn't a while loop but I need it
Re: A simple mistake I can't fix
The only reason it would process the word "exit" is if your code inside the while loop runs, correct?
That means the line of code to determine when to stop looping has not decided it is time to stop yet, correct?
Looking at the line of code responsible for making this decision:
while (userInput != s1)
...I refer you back to the suggested reading in my post#2 to see why this is happening. It has something to do with != and comparing strings.
If you can't figure it out with that page, try searching "java compare strings"
Re: A simple mistake I can't fix
Thanks a lot as I have figured out that the problem was using the != assignment, Should have used !userInput equals(exit).
Also, I do want to apologise to all the viewers, especially jps for not coding my first post.
Re: A simple mistake I can't fix
Glad you got it working :)