0

I have been going thorough some practice problems and have a question on this code. I was able to figure it out using a different method, but I don't understand why this example doesn't work.The code asks for input until the user enters the same input twice, where it should then display the duplicate input before ending the program.

I am getting:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: any>

Error on the last line with the word variable. Any ideas?

import java.util.ArrayList;
import java.util.Scanner;

public class MoreThanOnce {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        // create here the ArrayList 
        ArrayList<String> words = new ArrayList<String>();
        while (true){
            System.out.print("Type a word: ");
            String word = reader.nextLine();
            if(!words.contains(word)){
                words.add(word);
            }else{
                break;
            }


        }
        System.out.println("You gave the word " + word + " twice");

    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
xCargot
  • 1
  • 1
  • 2

3 Answers3

0

Your code does not compile. The variable "word" you want to display at the end is not in the right scope : you declare it in the while loop but try to use it outside this loop. Just change thing like this :

import java.util.ArrayList;
import java.util.Scanner;

public class MoreThanOnce {

  public static void main(String[] args) {
      Scanner reader = new Scanner(System.in);
      // create here the ArrayList
      String word; //variable declared before loop
      ArrayList<String> words = new ArrayList<String>();
      while (true){
         System.out.print("Type a word: ");
         word = reader.nextLine();
          if(!words.contains(word)){
              words.add(word);
          }else{
              break;
          }
      }
      System.out.println("You gave the word " + word + " twice");
  }

Hope it helps.

Mathias

  • This doesn't seem to work either. I am getting the same error even with your changes. – xCargot Aug 12 '15 at 05:44
  • Hi, its strange because it works fine for me with this change. Are your sure the change as been taken into account when you tried. What editor do you use ? What version of Java ? – Mathias Aboudou Aug 12 '15 at 05:51
  • Thanks for the responses. I got it working after saving it to a new file. I don't know what the issue was, but it is working after implementing your change. Thanks. – xCargot Aug 12 '15 at 05:55
  • Another question - why do you have to declare it before the while loop? Are the variables that are declared inside the while loop local to the while loop itself - so they cant be used anywhere outside the loop block? – xCargot Aug 12 '15 at 06:01
  • Yes exactly ! And this is true for every block (for, if, method...) you declare a variable in. – Mathias Aboudou Aug 12 '15 at 06:04
  • Thanks very much for your help! – xCargot Aug 12 '15 at 06:08
0

Declare "String word" variable before while loop.

import java.util.ArrayList;
import java.util.Scanner;
public class MoreThanOnce {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    // create here the ArrayList 
    String word;
    ArrayList<String> words = new ArrayList<String>();
    while (true) {
        System.out.print("Type a word: ");
        word = reader.nextLine();
        if (!words.contains(word)) {
            words.add(word);
        } else {
            break;
        }

    }
    System.out.println("You gave the word " + word + " twice");

  }
}
Rafiq
  • 740
  • 1
  • 5
  • 17
0

Are you using NetBeans ?

If yes then there is an open bug

Ankit
  • 55
  • 7