Results 1 to 5 of 5
  1. #1
    sequential is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Binary Search / Sort - Unable To Retrieve Index

    This is hard to describe, but I am trying to get the index of the found number to print alongside other code. I have tried various ways to pull the index, even re-creating the whole array. I have this set up to ask for input, find the number or the closest number to and the index to calculate a year. I have used numerous guides and examples but none seem to function in my code. Any help is greatly appreciated.

    This is the tricky bit. I am trying to get the last part to pull the index of the number but I can't get it to function. Standard guides for searching, sorting, index, array, array list, etc have not worked.
    Java Code:
    JOptionPane.showMessageDialog(null, "Searching for house value of "  + defaultFormat.format(userNumber) + 
    						"\nClosest Value: " + defaultFormat.format(binarySearch.binarySearchClosestMatch(userNumber)) + 
    						"\nIn the year: " + 0000 /*(1989+binarySearch.binarySearchClosestMatch(numbers[i]))*/);

    Java Code:
    public class BinarySearch {
    
    	public Integer[] _numbers;
    
    	BinarySearch(List<Integer> inputList) {
    
    		Collections.sort(inputList);
    		_numbers = new Integer[inputList.size()];
    		inputList.toArray(_numbers);	
    	}
    
    	public static void main(String[] args) throws Exception {
    		String userInput = null;
    
    		NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
    
    		Integer[] numbers = {0, 25000, 31000, 33000, 80000, 81000, 33500, 
    				33600, 86000, 90000, 91000, 33900, 40000, 44400, 45600, 
    				56000, 58000, 79000, 79999, 26500, 28000, 29000, 82000,
    				83000, 84000, 85000};
    
    		System.out.println(Arrays.toString(numbers));
    		try {
    
    			userInput = JOptionPane.showInputDialog("Enter House Value: ");
    
    			int userNumber = Integer.parseInt(userInput);
    
    			BinarySearch binarySearch = new BinarySearch(new ArrayList<Integer>(Arrays.asList(numbers)));
    
    			if (binarySearch.binarySearchClosestMatch(userNumber) == 0) {
    				JOptionPane.showMessageDialog(null, "Searching for house value of "  + defaultFormat.format(userNumber) + 
    						"\nClosest Value: " + defaultFormat.format(binarySearch.binarySearchClosestMatch(userNumber)) + 
    						"\n*** House was not built yet, $25000 / year 1989 is the first value ***" 
    						/*(1989+binarySearch.binarySearchClosestMatch(numbers[i]))*/);
    			}
    			else {
    				JOptionPane.showMessageDialog(null, "Searching for house value of "  + defaultFormat.format(userNumber) + 
    						"\nClosest Value: " + defaultFormat.format(binarySearch.binarySearchClosestMatch(userNumber)) + 
    						"\nIn the year: " + 0000 /*(1989+binarySearch.binarySearchClosestMatch(numbers[i]))*/);
    			}
    		}
    
    		catch(NumberFormatException e)
    		{
    			JOptionPane.showMessageDialog(null, "Invalid Input, Must Be A Number");
    		}
    		catch(ArrayIndexOutOfBoundsException e)
    		{
    			JOptionPane.showMessageDialog(null, userInput + " Invalid Input.");
    		}
    	}
    	public Integer binarySearchClosestMatch(Integer inputMatch) throws Exception {
    
    		int start = 0;
    		int end = _numbers.length - 1;
    		while(true) {
    			int mid = start + (end - start)/2;
    			if(_numbers[mid] == inputMatch) {
    				return inputMatch;
    			}
    			if(start == end) {
    				if(start == 0) {
    					if(Math.abs(_numbers[start] - inputMatch) < Math.abs(_numbers[start + 1] - inputMatch)) {
    						return _numbers[start];
    					} else {
    						return _numbers[start + 1];
    					}
    				}
    				if(start == (_numbers.length - 1)) {
    					if(Math.abs(_numbers[start] - inputMatch) < Math.abs(_numbers[start - 1] - inputMatch)) {
    						return _numbers[start];
    					} else {
    						return _numbers[start - 1];
    					}
    				}
    			}
    			if(start > end) {
    				if(Math.abs(_numbers[start] - inputMatch) < Math.abs(_numbers[end] - inputMatch)) {
    					return _numbers[start];
    				} else {
    					return _numbers[end];
    				}
    			}
    			if(_numbers[mid] < inputMatch) {
    				start = mid + 1;
    			} else {
    				end = mid - 1;
    			}
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Binary Search / Sort - Unable To Retrieve Index

    You're not returning an index at all; you're returning the number (or the closest number) itself. Instead of returning '_numbers[index]', simply return 'index'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sequential is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Binary Search / Sort - Unable To Retrieve Index

    I need both to return the value of the closest number to the input number and an index for to show the associated year. I am not sure if I understand your response completely, but it seems you are suggesting a return of one but not both. This is also why I am lost, I can't have my cake and eat it too, without help.

    Java Code:
    (1989+binarySearch.binarySearchClosestMatch(numbers[i]))
    I tried to get this part to return the index, then add it to the start date, finding a more recent date as a result.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Binary Search / Sort - Unable To Retrieve Index

    I really don't understand what you're trying to do there; could you please tell in words what you're trying to do? (no code please).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sequential is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Binary Search / Sort - Unable To Retrieve Index

    Thank you for trying to help me. I am sorry my words were not well written. I figured out how to accomplish what I needed. I had to re do the array as array list and then search for value.

    Java Code:
    try {
    
    			userInput = JOptionPane.showInputDialog("Enter House Value: ");
    
    			int userNumber = Integer.parseInt(userInput);
    
    			BinarySearch binarySearch = new BinarySearch(new ArrayList<Integer>(Arrays.asList(numbers)));
    
    
    			int closest = binarySearch.binarySearchClosestMatch(userNumber);
    
    			ArrayList numberArray = new	ArrayList<Integer>(Arrays.asList(numbers));
    
    			Collections.sort(numberArray);
    			System.out.println(numberArray);
    
    
    			if (binarySearch.binarySearchClosestMatch(userNumber) == 0) {
    				JOptionPane.showMessageDialog(null, "Searching for house value of "  + defaultFormat.format(userNumber) + 
    						"\nClosest Value: " + defaultFormat.format(closest) + 
    						"\n*** House was not built yet, $25000 & year 1989 is the first value ***");
    			}
    			else {
    				JOptionPane.showMessageDialog(null, "Searching for house value of "  + defaultFormat.format(userNumber) + 
    						"\nClosest Value: " + defaultFormat.format(closest)  + 
    						"\nIn the year: "  +(1988 + numberArray.indexOf(closest)));
    			}

Similar Threads

  1. Replies: 1
    Last Post: 02-11-2013, 02:29 PM
  2. Replies: 1
    Last Post: 12-01-2012, 04:20 AM
  3. Implementing quick sort and binary search
    By syle_q in forum New To Java
    Replies: 1
    Last Post: 04-07-2011, 09:46 PM
  4. Unable to retrieve stock quotes from yahoo
    By the reporter in forum AWT / Swing
    Replies: 4
    Last Post: 06-03-2010, 05:10 PM
  5. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM

Posting Permissions

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