Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4091164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:19:54+00:00 2026-05-20T19:19:54+00:00

I’m writing a program to search for a string and tell me were it

  • 0

I’m writing a program to search for a string and tell me were it was found.

import java.io.*;


public class BinarySearchTest
{
   public static void main(String [] args) throws IOException
   {
      int result;
      String searchValue;
      String input;

      // An array of numbers to search.
      String[] numbers = {"Jake", "Jerry", "Bill", "Lousie", "Goku", "Ivan", "John", "sarah", "kim"};

      // Create the console input objects.
      InputStreamReader reader =
                 new InputStreamReader(System.in);
      BufferedReader keyboard =
                 new BufferedReader(reader);

      // First we must sort the array in ascending order.
      IntQuickSorter.quickSort(numbers);

      do
      {
         // Get a value to search for.
         System.out.print("Enter a value to search for: ");
         input = keyboard.readLine();
         searchValue = input;

         // Search for the value
         result = IntBinarySearcher.i;

        // Display the results.
        if (result == -1)
           System.out.println(searchValue + " was not found.");
        else
        {
           System.out.println(searchValue + " was found at " +
                              "element " + result);
        }

        // Does the user want to search again?
        System.out.print("Do you want to search again? (Y or N): ");
        input = keyboard.readLine();
      } while (input.charAt(0) == 'y' || input.charAt(0) == 'Y');
   }
}




public class IntBinarySearcher
{
   static int i;





   public static int search(String[] numbers, String value)
   {
      int first;       // First array element
      int last;        // Last array element
      int middle;      // Mid point of search
      int position;    // Position of search value
      boolean found;   // Flag     

      // Set the initial values.
      first = 0;
      last = numbers.length - 1;
      position = -1;
      found = false;

      setI(Integer.parseInt(value));

      // Search for the value.
      while (!found && first <= last)
      {
         // Calculate mid point
         middle = (first + last) / 2;

         // If value is found at midpoint...
         if (numbers[middle].equals(value))
         {
            found = true;
            position = middle;
         }

         // else if value is in lower half...
         // need tell is value is less then the integer?, with out using equality regulators
         else if (value.compareTo(numbers[middle]) < 0)
            last = middle - 1;
         // else if value is in upper half....
         else
            first = middle + 1;
      }

      // Return the position of the item, or -1
      // if it was not found.
      return position;
   }

    public static void setI(int i) 
    {
        IntBinarySearcher.i = i;
    }

    public static int getI() 
    {
        return i;
    }
}






public class IntQuickSorter
{


   public static void quickSort(String[] numbers)
   {
      doQuickSort(numbers, 0, numbers.length - 1);
   }



   private static void doQuickSort(String[] numbers, int start, int end)
   {
      int pivotPoint;

      if (start < end)
      {
         // Get the pivot point.
         pivotPoint = partition(numbers, start, end);

         // Sort the first sub list.
         doQuickSort(numbers, start, pivotPoint - 1);

         // Sort the second sub list.
         doQuickSort(numbers, pivotPoint + 1, end);
      }
   }



   private static int partition(String[] numbers, int start, int end)
   {
      String pivotValue;    // To hold the pivot value
      int endOfLeftList; // Last element in the left sub list.
      int mid;           // To hold the mid-point subscript

      // Find the subscript of the middle element.
      // This will be our pivot value.
      mid = (start + end) / 2;

      // Swap the middle element with the first element.
      // This moves the pivot value to the start of 
      // the list.
      swap(numbers, start, mid);

      // Save the pivot value for comparisons.
      pivotValue = numbers[start];

      // For now, the end of the left sub list is
      // the first element.
      endOfLeftList = start;

      // Scan the entire list and move any values that
      // are less than the pivot value to the left
      // sub list.
      for (int scan = start + 1; scan <= end; scan++)
      {
         if (pivotValue.compareTo(numbers[scan])< 0) // pivotValue.compareTo(numbers[scan])< 0)
         {
            endOfLeftList++;
            swap(numbers, endOfLeftList, scan);
         }
      }

      // Move the pivot value to end of the
      // left sub list.
      swap(numbers, start, endOfLeftList);

      // Return the subscript of the pivot value.
      return endOfLeftList;
   }

   /**
      The swap method swaps the contents of two elements
      in an int array.
      @param The array containing the two elements.
      @param a The subscript of the first element.
      @param b The subscript of the second element.
   */

   private static void swap(String[] numbers, int a, int b)
   {
      String temp;

      temp = numbers[a];
      numbers[a] = numbers[b];
      numbers[b] = temp;
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:19:54+00:00Added an answer on May 20, 2026 at 7:19 pm

    There’s something missing:

    searchValue = input;
    
    //IntBinarySearcher.search(numbers, searchValue); here?
    
    // Search for the value
    result = IntBinarySearcher.i;
    

    Edit:

    This won’t work either since IntBinarySearcher.i is never “correctly” set (see Edit2) :).

    Better:

    result = IntBinarySearcher.search(numbers, searchValue);
    

    Edit 2:

    Oh, and this would throw a NumberFormatException for value=”Bill”: setI(Integer.parseInt(value));

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.