0

Program reads from a txt file of sorted strings and uses sequential, iterative binary and recursive binary to store in array then search array for location and how many iterations it took to find the word. Having an error when I try to compare a word in array to the word the user imputs. Not sure why. 2) Hoping someone can explain the differences between iterative binary and recursive binary. 3) Why is it necessary to do this...

SearchString si = new SearchString();

Program is below...

import ...

public class SearchString
{
public static final String TO_STOP = "-1";
public static final int NOT_FOUND = -1;
public static final int MAX_SIZE = 15000;

  public static int count1;
  public static int count2;
  public static int count3;


  public int sequentialSearch(String[] wordsArray, String word2Search)
  {
    int low = 0;
    int high = wordsArray.length - 1;
    for (int i = low; i <= high; i++)
    {
        count1++;
        if (wordsArray[i] == word2Search)
            return i;
    }
    return NOT_FOUND;
  } // end of sequentialSearch()

  public int binarySearch(String[] wordsArray, String word2Search)
  {
    int low = 0;
    int high = wordsArray.length - 1;
    while (low <= high)
    {
        int mid = (low + high)/2;
        count2++;
        if (wordsArray[mid] > word2Search){
            high = mid - 1;
        } else if (wordsArray[mid] < word2Search){
            low = mid + 1;
        } else
            return mid;
    }
    return NOT_FOUND;
  } /


  public int binarySearch2(String[] wordsArray, int low, int high, String word2Search){
    if (low > high)
        return NOT_FOUND;
    int mid = (low + high)/2;
    count3++;
    if (wordsArray[mid] > word2Search){
        return binarySearch2(wordsArray, low, mid-1, word2Search);
    } else if (wordsArray[mid] < word2Search){
        return binarySearch2(wordsArray, mid+1, high, word2Search);
    } else
        return mid;
  } 



public static void main(String[] args) throws IOException
{
    Scanner keyboard = new Scanner(System.in);

    boolean wantToContinue = true;

    Scanner stringsFile = new Scanner (new File("sortedStrings.txt"));//.useDelimiter(",\\s*"); 
    List<String> words = new ArrayList<String>();           

    String token1 = "";                                     

  (stringsFile.hasNext())                           
    {     token1 = stringsFile.next();
          words.add(token1);
    }
    stringsFile.close();                                    
    String[] wordsArray = words.toArray(new String[0]);     

    System.out.println(Arrays.toString(wordsArray));

    SearchString si = new SearchString();

    do {
        System.out.print("Type a word to search or -1 to stop: ");
        String word2Search = keyboard.nextLine();
        if (word2Search.equals(TO_STOP)){
            wantToContinue = false;
        } else {
            count1 = count2 = count3 = 0;
            int  index;

            index = si.sequentialSearch(wordsArray, word2Search);
            if (index == NOT_FOUND)
                System.out.println("sequentialSearch()      : " + word2Search + " is not found (comparison=" + count1 + ").");
            else
                System.out.println("sequentialSearch()      : " + word2Search + " is found in [" + index + "] (comparison=" + count1 + ").");

            index = si.binarySearch(wordsArray, word2Search);
            if (index == NOT_FOUND)
                System.out.println("iterative binarySearch(): " + word2Search + " is not found (comparison=" + count2 + ").");
            else
                System.out.println("iterative binarySearch(): " + word2Search + " is found in [" + index + "] (comparison=" + count2 + ").");

            index = si.binarySearch2(wordsArray, 0, wordsArray.length-1, word2Search);
            if (index == NOT_FOUND)
                System.out.println("recursive binarySearch(): " + word2Search + " is not found (comparison=" + count3 + ").");
            else
                System.out.println("recursive binarySearch(): " + word2Search + " is found in [" + index + "] (comparison=" + count3 + ").");
        }
    } while (wantToContinue);

}

}

5
  • 1
    If you have a "txt file of sorted strings," why do you need to sort it? Commented Sep 18, 2016 at 20:22
  • 1
    What, and where, is the error? What data was being processed that caused it? Commented Sep 18, 2016 at 20:23
  • In binarySearch method and binarySearch2 method. when I compare words.Array[mid] > word2Search Sort is inaccurate, edited question-- The operator > is undefined for the argument type(s) java.lang.String, java.lang.String Commented Sep 18, 2016 at 20:33
  • There are no such things as "sequential, iterative binary and recursive binary". The terms are not even grammatically correct: they are a bunch of adjectives. You are missing a noun. Commented Sep 18, 2016 at 20:37
  • @MikeNakis Sequential Searching. Recursive Binary Searching and Iterative Binary Seraching. Those terms are incorrect? Can you point me towards a resource where I can learn more? Commented Sep 18, 2016 at 20:46

2 Answers 2

1

You cannot compare String with == (or > and <). You need to use String.compareTo

Sign up to request clarification or add additional context in comments.

Comments

0

How to compare Strings (or any other type of Comparable object):

  • a > b becomes a.compareTo(b) > 0
  • a < b becomes a.compareTo(b) < 0
  • a == b becomes a.equals(b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.