-2

By way of an example I have a txt file that contains a list of dogs, one on each line of the file and then "**" on the last line. I then have the following code to load this into an ArrayList to use in a JComboBox.

This way I can simply add another line in the text box to add another dog.

My example code is as follows

public class test{
static String temp;

public static void main(String[] args) {
List<String> picklistDogs = new ArrayList<String>();
File picklistFile = new File (filePath);
try {
    BufferedReader loadPickList = new BufferedReader (new FileReader(picklistFile));
    while(true) {
        temp = loadPickList.readLine();
        if (temp != "**") {
            picklistDogs.add(temp);
        } else {
            break;
        }
    }
    loadPickList.close();
}
catch(FileNotFoundException e){
    System.out.println("file not found");
}
catch(IOException e){
    System.out.println("file io error");
}
} // END of main
} // END of class test

My problem seams to be that the if statement never triggers the break to exit the while loop.

Any solutions would be appreciated.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
m ainsworth
  • 49
  • 1
  • 6

2 Answers2

1

Use !temp.equals("**") .Try below code

if (!temp.equals("**")) {
                picklistDogs.add(temp);
            } else {
                break;
            }
A W
  • 1,041
  • 11
  • 18
0

In order to compare strings better to use java.lang.String#equals

Oleh Linnyk
  • 59
  • 1
  • 13