-2

Possible Duplicate:
Java string comparison?

Good evening. The following code gives no error, but when I try to validate the String entered it always assume that I haven't entered "YES" even when though I did use the toUpperCase() method. This must be a very basic error, but I'm unable to figure it out right now since I'm just learning the basics of this language. Here's the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main{

    public static void main(String[] args) throws IOException{

        byte counter = 1;
        boolean myCondition = false;
        String answer;
        List<String> myList = new ArrayList<String>();
        BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in));

        do{
            System.out.println("Enter the #" +counter +" person's name:");
            myList.add(objReader.readLine());
            counter++;
            System.out.println("Would you like to add another name to the list? yes/no");
            answer = objReader.readLine();
            if (answer.toUpperCase() == "YES"){
                myCondition = true;
            }

        } while (myCondition);

        System.out.println("The list of names you entered, looks like this:\n");
        for (String name: myList)
            System.out.println(name);

        System.out.println("\nPress <Enter> to randomize the list of names you entered.");
        objReader.readLine();
        Collections.shuffle(myList);

        System.out.println("Done! \nPress <Enter> to see how the randomized list looks like.\n");
        objReader.readLine();
        for(String nombre: myList)
            System.out.println(nombre);

    }

}

Thanks in advance for any help and/or tips.

Community
  • 1
  • 1
Cristian
  • 359
  • 3
  • 6
  • 12

1 Answers1

0

you should use .equals() method instead of == when comparing strings:

answer.toUpperCase().equals("YES")

You can find comfort in that this is an error most people does when starting java programming :)

John Snow
  • 5,214
  • 4
  • 37
  • 44