I'm having trouble searching my array for a value the user inputs. This project is about taking money from your friends and investing it. The program is to keep track of those investments. I use an array to assign each friend's investment. I import a file called investments.txt, and this is needed for this code to compile.
My problem is the binary search itself. I know my array is valid, and it is sorted via bubble sort. And that all works, so no problems there, but for some reason unknown to me, or at least I can't see it, every time i search a value in my array (a valid value from the txt file import) it considers it "Not found!". It won't print valid info to the user, I'm not sure why.
Example on what the output must look like:
Would you like to search for a investment amount? (Y/N) Y
Enter investment amount: 10075.45
Investment amount 10075.45 is found at position 34.
System.out.println("Would you like to search for an investment amount? (Y/N)");
String answer= in.nextLine();//Prompt the user for binary search
while (answer.equalsIgnoreCase("Y")) {
System.out.println("Enter the investment amount");
double userSrc = in.nextDouble();
int n = 0;
int first = 0;
int last = n - 1;
int middle = (first+last)/2;
while (first <= last) {
if (fileArray[middle] < userSrc)
first = middle + 1;
else if (fileArray[middle] == userSrc) {
System.out.printf("%f found at location %d.\n", userSrc, middle+1);
return;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
System.out.printf("Not found! %f isn't present in the list.\n", userSrc);
return;
}
}
Thank you for the simplified code. The example you made was missing a few things that I promptly added in to get it to compile.
In past edits you included
double[] fileArray = {"5", "100", "146.15", "314.56", "600.92"};
Which isn't bad, test data is required. This didn't need to be removed from the question, it needed the quotations around the values to be removed so they could be treated as proper doubles. With that in mind, it seems that you accidentally initialized int n = 0
instead of int n = fileArray.length
. This seems like it was the problem on both edits of the program. Remember that n is supposed to be the amount of elements you have stored in the array when it comes to this method of sorting, so having it at 0 will cause all of the other values to be initialized incorrectly and treat it like there's 0 elements in the array. Here's my corrected code:
import java.util.*;
public class binarysearch{
public static void main(String []args){
double[] fileArray = {5, 100, 146.15, 314.56, 600.92};
Scanner in = new Scanner(System.in);
System.out.println("Would you like to search for an investment amount? (Y/N)");
String answer= in.nextLine();//Prompt the user for binary search
while (answer.equalsIgnoreCase("Y")) {
System.out.println("Enter the investment amount");
double userSrc = in.nextDouble();
int n = fileArray.length;
int first = 0;
int last = n - 1;
int middle = (first+last)/2;
while (first <= last) {
if (fileArray[middle] < userSrc)
first = middle + 1;
else if (fileArray[middle] == userSrc) {
System.out.printf("%f found at location %d.\n", userSrc, middle+1);
//return;
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
System.out.printf("Not found! %f isn't present in the list.\n", userSrc);
//return;
System.out.println("Would you like to search for an investment amount? (Y/N)");
answer= in.nextLine();
}
}
}
Output for Y 146.15
:
Would you like to search for an investment amount? (Y/N)
Enter the investment amount
146.150000 found at location 3.
Finally, notice that your program returns after finding one value. This return is still in main so it exits the program. This will cause the loop of prompting users to never get anywhere. I would replace this with a break;
to exit ONLY the loop and remove the one at the end of the loop entirely. There might also be hanging newlines from the nextDouble
calls you might want to look out for. I will leave that for you to play around with. Maybe even try with inputs like 146.150000000001
.