Java Im doing a binary search on an array and need to allow as many queries as p
ID: 3697292 • Letter: J
Question
Java
Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this.
My code so far looks like this.
public static void main(String[] args)
{
try
{
File f = new File("file.txt");
Scanner input1 = new Scanner(f);
Scanner input2 = new Scanner (f);
Scanner keyboard = new Scanner (System.in);
String s = "";
int total = 0;
int lnCount = 0;
System.out.println("Enter A Number");
int n = keyboard.nextInt();
while(input2.hasNextLine())
{
input2.nextLine();
lnCount++;
}
int arr[] = new int[lnCount];
int i = 0;
while(input1.hasNextLine())
{
s = input1.nextLine();
arr[i] = Integer.parseInt(s);
i++;
total = Integer.parseInt("" + s);
//System.out.print(total + " ");
}
/*
for(int j = 0; j < arr.length; j++)
{
System.out.print(arr[j] + " ");
}
*/
//System.out.println(primes);
int scan = n;
int first = 0;
int last = lnCount - 1;
int mid = (first + last) / 2;
while(first <= last)
{
if(arr[mid] < scan)
first = mid + 1;
/*for(int j = 0; j < mid + 1; j++)
{
System.out.print(arr[j] + " ");
}
*/
//System.out.println(arr[mid]);
else if(arr[mid] == scan)
{
/*
for(int j = 0; j < mid; j++)
{
//System.out.print(arr[j] + " ");
}
*/
System.out.println(scan + " is a prime number. Enter another number.");
break;
}
else if(scan < 0)
{
System.out.println("No Negatives. Bye");
break;
}
else
{
last = mid - 1;
/*for(int j = 0; j < mid - 1; j++)
{
System.out.print(arr[j] + " ");
}
*/
}
//System.out.println(arr[mid]);
mid = (first + last) / 2;
}
if(first > last)
{
System.out.println("Current file is not large enough for " + scan + ".");
}
}
//System.out.println(total);
catch(Exception e)
{
System.out.println("File not found.");
}
}
}
Explanation / Answer
public static void main(String[] args)
{
try
{
File f = new File("file.txt");
Scanner input1 = new Scanner(f);
Scanner input2 = new Scanner (f);
Scanner keyboard = new Scanner (System.in);
String s = "";
int total = 0;
int lnCount = 0;
System.out.println("Enter A Number");
int n = keyboard.nextInt();
while(input2.hasNextLine())
{
input2.nextLine();
lnCount++;
}
int arr[] = new int[lnCount];
int i = 0;
while(input1.hasNextLine())
{
s = input1.nextLine();
arr[i] = Integer.parseInt(s);
i++;
total = Integer.parseInt("" + s);
//System.out.print(total + " ");
}
/*
for(int j = 0; j < arr.length; j++)
{
System.out.print(arr[j] + " ");
}
*/
//System.out.println(primes);
int scan = n;
int first = 0;
int last = lnCount - 1;
int mid = (first + last) / 2;
while(first <= last)
{
if(arr[mid] < scan)
first = mid + 1;
/*for(int j = 0; j < mid + 1; j++)
{
System.out.print(arr[j] + " ");
}
*/
//System.out.println(arr[mid]);
else if(arr[mid] == scan)
{
/*
for(int j = 0; j < mid; j++)
{
//System.out.print(arr[j] + " ");
}
*/
System.out.println(scan + " is a prime number. Enter another number.");
break;
}
else if(scan < 0)
{
System.out.println("No Negatives. Bye");
break;
}
else
{
last = mid - 1;
/*for(int j = 0; j < mid - 1; j++)
{
System.out.print(arr[j] + " ");
}
*/
}
//System.out.println(arr[mid]);
mid = (first + last) / 2;
}
if(first > last)
{
System.out.println("Current file is not large enough for " + scan + ".");
}
}
//System.out.println(total);
catch(Exception e)
{
System.out.println("File not found.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.