Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: A simple mistake I can't fix

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.
    Last edited by jps; September 5th, 2012 at 02:27 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    while (userInput != s1)

    The program runs fine if there isn't a while loop but I need it

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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"

  5. The Following User Says Thank You to jps For This Useful Post:

    xdx (September 5th, 2012)

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: A simple mistake I can't fix

    Glad you got it working

Similar Threads

  1. Please, find my mistake
    By Daler in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2012, 11:17 PM
  2. where located my mistake
    By erdy_rezki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2012, 10:31 AM
  3. Replies: 12
    Last Post: May 8th, 2012, 05:22 PM
  4. I am making a silly mistake but I cant find where! PLEASE HELP!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2012, 01:44 AM
  5. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 04:12 PM