Question

Java Im doing a binary search on an array and need to allow as many queries...

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.

Enter a number.
3
3 is a prime number. Enter another number.
4
4 is not a prime number. Enter another number.
8
Current file not large enough for 8. Enter another number.
-1
Bye.

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.");

       }

      

      

   }

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Java Im doing a binary search on an array and need to allow as many queries...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, ...

    Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, nextInt, next, next, and nextLine. Then I have to instantiate a User object using those 5 fields and insert that object in the usersArr. I just need to see what this would look like if a text file with 5 fields for 5 users existed. Here's my code so far: int [] usersArr = new int[200];           System.out.print("Enter file name:...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT