-1

I am a new programmer in java and I'm trying to use the binarySearch to find a specific state name in an array that captures 3 state names. In this case I chose TEXAS. Everything else in the code is right up until the binarySearch where I get a compiler error of "cannot find symbol TEXAS". Can somebody show me what i'm doing wrong?

import java.util.Scanner;
import java.util.Arrays;

public class OrozcoBLE64 {
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {    
        String[] stateNameArray = new String[3];
        String[] stateNameArrayCopy = new String[stateNameArray.length];

        for (int counter = 0; counter < 3; counter++) {

            System.out.printf("Enter the name for state %d:\n ", counter + 1);
            stateNameArray[counter] = input.nextLine().toUpperCase();

            System.arraycopy(stateNameArray, 0, stateNameArrayCopy, 0, stateNameArray.length);
        } // end of for loop

        boolean copyEquals = Arrays.equals(stateNameArray, stateNameArrayCopy);
        System.out.printf("stateNameArray %s stateNameArrayCopy\n\n",
                (copyEquals ? "equals" : "does not equal")); //displays if arrays are equal after copy.

        Arrays.sort(stateNameArray);
        System.out.printf("The statesNameArray has been sorted\n");

        boolean sortEquals = Arrays.equals(stateNameArray, stateNameArrayCopy);
        System.out.printf("stateNameArray %s stateNameArrayCopy\n\n",
                (sortEquals ? "equals" : "does not equal"));

        String state = Arrays.binarySearch(stateNameArray, TEXAS);

        if (state == TEXAS) {
            System.out.printf("Found TEXAS at element %d in statesNameArray\n", state);
        }
    } // end of main method
} // end of class
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
borozco94
  • 23
  • 3
  • Shouldn't `TEXAS` be in quotes? Like, `"TEXAS"`? – jfdoming Jul 27 '15 at 00:48
  • 2
    What is `TEXAS` ? where is it defined? can you come up with a smaller code snippet that demonstrate the *whole* thing in a way it's easy to reproduce ? – Nir Alfasi Jul 27 '15 at 00:48
  • After you correct problem with `TEXAS` here you will find answer about why `if(state==TEXAS)` doesn't work (since `state` is String): http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Pshemo Jul 27 '15 at 00:49
  • TEXAS is going to be an element in the array statesNameArray. It will be captured from the scanner. – borozco94 Jul 27 '15 at 00:51
  • 2
    @borozco94 since we can't *see* what input your user has entered, please create an example with hardcoded values that reproduces your issue and which we can easily copy into our IDEs, compile & run. As long as you're not providing us important details - we can't really help you. – Nir Alfasi Jul 27 '15 at 00:54
  • @borozco94: What is the type of `TEXAS` ? Moreover, which `binarySearch ( ... )` method of `Arrays` class returns a `String`. I couldn't see anyone, since binary search returns the index at which the element is present, so how come you getting one `String` value from `binarySearch ( ... )` – nIcE cOw Jul 27 '15 at 00:56
  • Answer for your current question is that `TEXAS` is treated as variable, but it hasn't been declared in this scope, so it is not local variable of this method, and it is also not static field of `OrozcoBLE64` class. If you think that this code should work provide more details about why you think so. – Pshemo Jul 27 '15 at 00:57

1 Answers1

2

You should be using the String API equals() not '==' for string comparison.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45