Results 1 to 2 of 2
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default binarySearch is not working for String array?

    I don't know why, but my binaryStringSearch method isn't correctly printing even though it's virtually the same thing as my binaryIntSearch method, which works perfectly.

    This is the objective of the exercise:
    1) Use binary search to output the position of the following numbers from file numsearch.txt
    5, 9, 43 and 79.
    2) Read from file namesearch.txt
    Use binary search to output the position of the following names
    Dennis, Billy-Bob and Steve.
    The file contains 20 entries.


    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class exercise2 {
    	static int[] numbers;
    	static String[] words;
    	public static String[] readStringFile(String f)
    			throws FileNotFoundException{
    			List<String> words1 = new ArrayList<String>();
    			Scanner input = new Scanner(new File(f));
    			while (input.hasNext()){
    				words1.add(input.next());
    			}
    			words = new String[words1.size()];
    			words = words1.toArray(words);
    			  return words;
    			}
    	public static int[] readIntFile(String f)
    			throws FileNotFoundException{
    		ArrayList <Integer> number1 = new ArrayList<Integer>();
    		Scanner input = new Scanner(new File(f));
    		while (input.hasNext()){
    			number1.add(Integer.parseInt(input.next()));
    		}
    		numbers = new int[number1.size()];
    		for(int i = 0;i < numbers.length;i++)
    		    numbers[i] = number1.get(i);
    		  return numbers;
    		}
    	public static int binaryIntSearch(int[] numbers, int target){
    		int low = 0;
    		int high = numbers.length - 1;
    
    		while (low <= high){
    			int mid = (low + high)/2;
    			if (numbers[mid] == target){
    				return mid;
    			} else if (numbers[mid] < target){
    				low = mid + 1;
    			} else if (numbers[mid] > target){
    				high = mid - 1;
    			}	
    		}
    			return -1;
    	}
    	public static int binaryStringSearch(String[] words, String value){
    		int low = 0;
    		int high = words.length - 1;
    		System.out.println("This is the low variable:" + low);
    		System.out.println("This is the high variable:" + high);
    		while (low <= high){
    			int mid = (high + low) / 2;
    			if (words[mid].equals(value)) {
    				return mid;
    			} else if(words[mid].compareTo(value) > 0) {
    				low = mid + 1;
    			} else if (words[mid].compareTo(value) < 0){
    				high = mid - 1;
            }
        }
    		return -1;
    }   
    	public static void main(String[] args)
    		throws FileNotFoundException{
    		int[] file1 = readIntFile("numsearch.txt");
    		String[] file2 = readStringFile("namesearch.txt");
    		
    		System.out.println(binaryIntSearch(file1, 5));
    		System.out.println(binaryIntSearch(file1, 9));
    		System.out.println(binaryIntSearch(file1, 43));
    		System.out.println(binaryIntSearch(file1, 79));
    		
    		System.out.println(binaryStringSearch(file2, "Dennis"));			
    		System.out.println(binaryStringSearch(file2, "Billy-bob"));
    		System.out.println(binaryStringSearch(file2, "Steve"));
    		
    	}
    }

  2. #2
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: binarySearch is not working for String array?

    Oh my god I'm stupid. Sorry
    I just got the operators wrong. Switched around the less than and greater than operators. Sigh.

Similar Threads

  1. Replies: 2
    Last Post: 04-28-2013, 02:50 AM
  2. Replies: 1
    Last Post: 12-07-2012, 02:57 PM
  3. replaceAll(string a, string b) not working as expected
    By Bronsonboy in forum New To Java
    Replies: 3
    Last Post: 07-16-2012, 01:18 AM
  4. Having problem with Arrays.binarySearch()
    By xEnOn in forum New To Java
    Replies: 13
    Last Post: 05-10-2011, 06:04 PM
  5. SortedSet and Collections.binarySearch
    By ninoid in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •