Results 1 to 5 of 5
 08172013, 05:38 PM #1Member
 Join Date
 Aug 2013
 Posts
 3
 Rep Power
 0
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 recreating 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; } } } }
 08172013, 06:06 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 08172013, 06:40 PM #3Member
 Join Date
 Aug 2013
 Posts
 3
 Rep Power
 0
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]))
 08182013, 04:02 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 08182013, 08:52 PM #5Member
 Join Date
 Aug 2013
 Posts
 3
 Rep Power
 0
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

Enhanced java.util.TreeMap to access and retrieve elements by index
By geniot in forum Reviews / AdvertisingReplies: 1Last Post: 02112013, 02:29 PM 
Reading in a file, carrying out linear search, then sorting before Binary search
By JavaNewb26 in forum New To JavaReplies: 1Last Post: 12012012, 04:20 AM 
Implementing quick sort and binary search
By syle_q in forum New To JavaReplies: 1Last Post: 04072011, 08:46 PM 
Unable to retrieve stock quotes from yahoo
By the reporter in forum AWT / SwingReplies: 4Last Post: 06032010, 04:10 PM 
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12102009, 02:42 AM
Bookmarks