1

I have a list of names from a text file: MARY, PATRICIA, LINDA, BARBARA, ELIZABETH, JENNIFER, MARIA that I put into a String Array. I want to be able to search the array, but I am having problems. I'd like to have it sorted using a quicksort algorithm, and to search using the Binary search, but I'm trying to get something simple to work. If I searched "MARY" the result would be "MARY is not it"

public class NameSearch {

public static void main(String[] args) throws IOException{
    // TODO Auto-generated method stub

    FileReader woMen = new FileReader("names.txt");

    String[] womenArray;
    womenArray = new String[64];

    BufferedReader reader = new BufferedReader(woMen);
    String line = null;

        for (int j=0;j<womenArray.length;j++)
        {
            womenArray[j] = reader.readLine();
        }

    reader.close();

    int x = 0, y =0;


    System.out.println("Name?");
    Scanner keyboard = new Scanner(System.in);
    String input = keyboard.nextLine();

    for(int i=0;i<womenArray.length;i++) {
        if (input == womenArray[i]) {
            System.out.println("Found! "+input);
        }
        System.out.println(womenArray[i]+" is not it");
    }


}

}

th30d0rab1e
  • 119
  • 1
  • 11

1 Answers1

1

The mistake is in if statement. To match strings, you use .equals method like this:

System.out.println("Name?");
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();

for(int i=0;i<womenArray.length;i++) {
    if (womenArray[i].equals(input)) {
        System.out.println("Found! "+input);
    }
}

And to sort the array, you use Array's sort method:

Arrays.sort(womenArray);

Read about Arrays.sort(Object[] a) here

Also, you can use Arrays's built in binarySearch method. Read more about it here

In your code, that would look like this:

int location = Arrays.binarySearch(womenArray, input)

if location is a negative number, that means the value you are searching for was not found.

int location = Arrays.binarySearch(womenArray, input.toUpperCase());

if (location > 0){    
   System.out.println("found it at " + location + " " + womenArray[location]);
}
Faraz
  • 6,025
  • 5
  • 31
  • 88
  • Hmm... how would I use Arrays.Binary Search for this one? How would I get it to return success? – th30d0rab1e Feb 20 '16 at 02:21
  • so after sorting (Arrays.sort(....)), and after taking input from user, you don't write for loop. You just write int location = Arrays.binarySearch(womenArray, input); – Faraz Feb 20 '16 at 02:23
  • Oh cool, then I call display womenArray[location] as the result? – th30d0rab1e Feb 20 '16 at 02:25
  • one question, all the strings inside womenArray are capital letters? if yes than you will have to do 1 small change: int location = Arrays.binarySearch(womenArray, input.toUpperCase()); and then yes you do womenArray[location] to print out the woman's name. Remember that, if the location is negative (less than 0), that means the name is not found – Faraz Feb 20 '16 at 02:29
  • I edited my answer. Please check it again. If you have any question let me know – Faraz Feb 20 '16 at 02:34
  • I've been trying to find String Array examples for my own quicksort and Binary search to make my own algorithm. I cant find any good examples in my textbook either, most of them are INT arrays being sorted and searched. – th30d0rab1e Feb 20 '16 at 03:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104005/discussion-between-th30d0rab1e-and-faraz-durrani). – th30d0rab1e Feb 20 '16 at 03:00
  • @th30d0rab1e : I can try to write quicksort tomorrow. I looked up online and you were right. They all are written to sort ints. I will try to replicate it for Strings. But no promises :P Right now I am going to sleep. I will have to get some kind of confirmation from you for me to go ahead – Faraz Feb 20 '16 at 03:40
  • Wont be needed, the assignment is due tonight. But thanks anyways my man. – th30d0rab1e Feb 20 '16 at 04:04
  • @th30d0rab1e r u online? – Faraz Feb 20 '16 at 04:16
  • No Im watching a movie :D – th30d0rab1e Feb 20 '16 at 04:17
  • lawl i m trying to make quicksort right now – Faraz Feb 20 '16 at 04:21
  • quicksort is done xD – Faraz Feb 20 '16 at 04:26
  • come online on steam plz – Faraz Feb 20 '16 at 04:26
  • I cant im at a movie theater :/ lol – th30d0rab1e Feb 20 '16 at 04:27
  • okay..... I am posting the solution in the main answer. You can check it and test it and submit it if you want. – Faraz Feb 20 '16 at 04:28
  • posted quicksort algorithm. Anything else? – Faraz Feb 20 '16 at 04:32
  • I also wrote binarySearch method. But I wont post it here. I also removed quicksort. Just msg me on steam – Faraz Feb 20 '16 at 04:41