0

I am trying to make a endless loop pig Latin translator until the user enters a "q" to quit the program. I am having issues finalizing the while statement. The error that I am getting is as followed.

PigLatin.java:27: error: cannot find symbol } while (word != "q"); ^ symbol: variable word location: class PigLatin

Here is my source code:

import java.util.Scanner;

public class PigLatin {
    public static void main(String[] args) {
        System.out.println("Welcome to the pig latin convertor.");
        do {
            Scanner in = new Scanner(System.in);
            String word, pig;
            char first;

            System.out.print("enter word or press 'q' to quit: ");
            word = in.next();
            word = word.toLowerCase();
            System.out.println(word);

            first = word.charAt(0);
            if (first == 'a' ||  first == 'e' || first == 'i' || 
                first == 'o' || first == 'u')  // vowel
                pig = word + "way";      
            else
                pig = word.substring(1) + word.charAt(0) + "ay";
            System.out.println("pig-latin version: " + pig);
        } while (word != "q");
    }
}
dave
  • 11,641
  • 5
  • 47
  • 65
Unholypalidn
  • 13
  • 1
  • 3

1 Answers1

1

Your variable word has been declared in the wrong place, ie. within the do..while loop, rather than before. This is causing the compilation error.

Once you fix that, you still have a bug because String comparisons should use equals() not != (or ==).

Try something like:

String word;
do {
    ...
} while (!word.equals("q"));

You could also use word.equalsIgnoreCase("q") if you don't care whether they enter "q" or "Q".

dave
  • 11,641
  • 5
  • 47
  • 65
  • Well that fixed the program for stopping the loop now its adding ay to it (enter word or press Q to quit: q pig-latin version: qay)... maybe the program is to good :) haha i kid Thanks so much for your help this is what I was looking for. – Unholypalidn Feb 10 '16 at 06:09