1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
13
Rep Power
0

Java Binary Search

Here's the description of what I need to do for the assignment:
Write a program that creates an array of 1000 random numbers in the range 1 – 5000. Then ask the user for a value between and 5000, and search the array until you either find the value or determine that the value is not in the array. Print a message indicating whether or not the value was found, and how many numbers you had to look at before you found the value (or determined that it was not there).

The program I've written so far stops after one guess it needs to keep cycling until you guess the right number

here's my code so far:
Code:
```import java.util.*;

public class FindNumber
{
final int MaxVal = 5000;
int value;
int numbers[] = new int[1000];
FindNumber()
{
Random rg = new Random();
for (int i=0;i<numbers.length;i++)
numbers[i]=rg.nextInt(MaxVal+1);

Arrays.sort(numbers);
}
public int searchValue(int val)
{
int min=0, max = numbers.length;
int i=0;
while(true)
{
i++;
if (numbers[(max+min)/2]==val)
return i;
else if (val > numbers[(max+min)/2])
min = (max+min)/2 + 1;
else
max = (max+min)/2 - 1;
if (min==max)
return -1;

}

}

public static void main(String[] args)
{
FindNumber fn = new FindNumber();
Scanner kb = new Scanner(System.in);
System.out.println("Enter any positive integer between 1 and 5000.");
int x = kb.nextInt();
if (fn.searchValue(x)>0)
System.out.println("Found in " + fn.searchValue(x) + " tries.");
else
}
}```
does anyone have any ideas on what I could change around or fix with my code
2. I don't see any obvious errors. Are you sure it isn't working?
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
13
Rep Power
0
Yeah, it either stops after one entry or will say found in ect. amount of tries after one entry... so it's definitely not working for me
4. Originally Posted by blyons626
Yeah, it either stops after one entry or will say found in ect. amount of tries after one entry... so it's definitely not working for me
The potential problem I see if here

Code:
```            if (min==max)
return -1;```
This means you are looking at a one element segment of the array, but you're not checking to see if the number there is equal to val.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
13
Rep Power
0
I changed the
Code:
```if (min==max)
return -1;```
to
Code:
```if (min>max)
return -1;```
but its still making me void static main after every guess
6. Originally Posted by blyons626
I changed the
Code:
```if (min==max)
return -1;```
to
Code:
```if (min>max)
return -1;```
but its still making me void static main after every guess
No that's not what I mean.

When min == max, that means that you are now looking at a one element segment of the original array. At that point, the number you are looking for could be at that position.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
13
Rep Power
0
Originally Posted by bullet
No that's not what I mean.

When min == max, that means that you are now looking at a one element segment of the original array. At that point, the number you are looking for could be at that position.

so should I take this out of my code?
8. Originally Posted by blyons626
so should I take this out of my code?
Since you have an infinite loop, you will have to have a way to break it.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
13
Rep Power
0
Yeah I still can't figure it out
10. Originally Posted by blyons626
Yeah I still can't figure it out
Just try checking the one element to see if it's the one you're looking for.