Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Beginner program w/ file, arrays and binary search algorithm - HELP!

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Beginner program w/ file, arrays and binary search algorithm - HELP!

    Hi there. I am a beginner and I can't seem to figure out what is wrong with my code.
    I am trying to write a program that will ask a user to enter a number between 1 and 10,000 and then report whether the number is prime. But when I test it, it keeps telling me that every single number is composite, even though it actually is prime. It is for an assignment for school so I am not allowed to use any more advanced java than what I have here.

    The program is supposed to:
    Read the numbers from a file
    Store them in an array
    Implement a the binary search method to search for the number entered by the user.

    import java.util.Scanner;
    import java.io.*;
     
    public class Primes {
       public static void main(String[] args) throws IOException {
           File inFile = new File("primes.txt");
           Scanner input = new Scanner(inFile);
           Scanner keyboard = new Scanner(System.in);
           String primes[] = new String [1229];
           String numSearch;
           int i = 0;
     
           while(input.hasNext()) {
               primes[i] = input.next();
               i++;
           }
     
           System.out.println("Welcome!\n");
     
           System.out.print("Enter a number from 1 to 10,000 or 'x' to exit: ");
           numSearch = keyboard.next();
           while(!numSearch.equals("x")) {
               int location = binarySearch(primes, numSearch);
               if (location == -1) {
                   System.out.println(numSearch + " is composite.\n");
               }
               else {
                   System.out.println(numSearch + " is prime!\n");
               }
               System.out.print("Enter a number from 1 to 10,000 or 'x' to exit: ");
               numSearch = keyboard.next();
           }
     
           System.out.println("Goodbye!");
     
           input.close();
           keyboard.close();
       }
     
       private static int binarySearch(String[] arr, String key) {
       int low = 0;
       int high = arr.length;
     
       while (low <= high) {
           int mid = (low + high) / 2;
           if (arr[mid].equals(key)) {
               return mid;
           }
           else if (key.compareTo(arr[mid]) < 0) {
               high = mid - 1;
           }
           else {
               low = mid + 1;
           }
       }
     
       return -1;
     
    }
     
     
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Beginner program w/ file, arrays and binary search algorithm - HELP!

    When debugging programs, the print statement is your friend (and the most common way of initial debugging). First print out your value you are checking to ensure that it is what you think you input (remember that blanks, special chars etc) can throw off your test.

    You also may want to be certain you are reading in the values correctly into your String array. Again, remember special chars.

    Next in your binary search routine, use print statements to print the values you are checking against. Then you might get a clue as to what is going on.

    Regards,
    Jim

Similar Threads

  1. Binary Search Algorithm
    By omgitztmarie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2014, 06:22 AM
  2. Sorting arrays using a recursive binary search
    By f0rdperfect in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 2nd, 2014, 08:13 PM
  3. Binary File. Extract float data from binary file and put into array
    By cardinal152 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 13th, 2012, 07:31 PM
  4. Beginner Java Program Help/ txt database search
    By JavaConfused in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2011, 09:20 AM
  5. [SOLVED] Search file program
    By righi in forum Java Theory & Questions
    Replies: 4
    Last Post: November 15th, 2010, 11:08 AM

Tags for this Thread