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 9024223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:57:11+00:00 2026-06-16T05:57:11+00:00

I’m a relatively inexperienced programmer working on a basic array generating/searching program for homework.

  • 0

I’m a relatively inexperienced programmer working on a basic array generating/searching program for homework. I’ve got it working perfectly, but it will randomly freeze up (without throwing any exceptions or error messages that I can detect) after I set the search key. The real problem, though, is that I can’t always reproduce the error by doing the same thing. I’m programming and running the program in Eclipse.

Here is the basic structure of my program; for simplicity’s sake, I’m only including the actual code for the setter and button that seems to be causing the problem. I suspect it’s something simple, but I see no reason that this code should be locking up a program.

public class ArraySearcher extends JPanel
                       implements ActionListener  {


private static final long serialVersionUID = 6449138670325520140L;

/**
 * A program description.
 */


// Fields (array and search parameters)
static int key;
static int arraySize;
static int min;
static int max;
static int midpoint;
// (Number of search steps taken by each search algorithm)
static int linSteps;
static int binSteps;
// (JButtons, JLabels, JTextFields, and the log for displaying results)
static JButton runButton, chKeyButton, newArrayButton, exitButton;
static JTextField lStepField, bStepField;
static JTextField keyField;
static JTextField arraySizeField;
static JTextField time;
static JTextArea log;
// (The arrays to be used)
static int[] randArray, sortArray;
// (Makes the output formatting a little easier to read)
public static String newline = "\n", twolines = "\n\n";


// The constructor
public ArraySearcher() {

// Setting up the fields and GUI
    }


// Getters and setters
protected static int getKey() {
    return key;
}


protected static void setKey() {
    boolean success = false;
    // loop and try catch block to deal with the potential parsing exception
    while (success == false) {
        try {
            key = Integer.parseInt(JOptionPane.showInputDialog(
                    "Please enter the number you\nwish to search for:"));
            keyField.setText(Integer.toString(getKey()));
            success = true;
        }

        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, 
                    "There was a number format error.  Please\n" +
                    "input only positive, whole numbers.");
        }
    }
}

    // More getters and setters...


public static void main(String[] args) {
    // Implement the GUI, all other work is handled from
    // there and from within the constructor
    theGUI();
}


private static void theGUI() {
// Set up the GUI and allow user to set min and
// max values for the random number generator

    }


@Override
public void actionPerformed(ActionEvent e) {
    //Handling of the run/restart button.
    if (e.getSource() == runButton) {
    }

    // Handling the Change Key button
    else if (e.getSource() == chKeyButton) {
        setKey();
        chKeyButton.setText("Change Key");
        linSearch(getRandArray());          // Implicit searches triggered by
        binSearch(getRandArray());          // selecting a new search key
    }

    // Handling the New Array button
    else if (e.getSource() == newArrayButton) {
    }

    // Handling of the exit button.
    else if (e.getSource() == exitButton) {
    }
}


// Method for building the array of random numbers; includes an implicit search
// which can be canceled (i.e. just build and return the array) by passing it
// a false condition when it's called
private void arrayBuilder(boolean fullRun) {

}


private void linSearch(int[] arrayIn) {
    // Linear search method

}


private void binSearch(int[] arrayIn) {
    // Binary search method
    int result = -1;            // Location of a positive match; initialized to an impossible result
    int tempMax = arraySize;    // Dynamic maximum index for progressive midpoint calculations
    int tempMin = 0;            // Dynamic minimum index
    int newMid = 0;             // Dynamic midpoint
    int count = 0;              // Counts the steps required to find value
    boolean success = false;    // A loop escape boolean

    log.append("RUNNING BINARY SEARCH" + newline);      
    log.append("SORTING ARRAY..." + twolines);

    sortArray = sort(arrayIn);              // Sort the array prior to searching

    // Array midpoint index calculation
    midpoint = tempMax/2 - tempMin/2;   // Calculation prevents buffer overflow; allows for nonzero minimum
    newMid = midpoint;

    // Search loop
    while (tempMin != tempMax && success == false) {
        if (sortArray[newMid] == key) {
            success = true;
            result = newMid;
            count++;
        }

        else if (sortArray[newMid] < key) {
            tempMin = newMid;
            newMid = tempMax/2 - tempMin/2;
            count++;
        }

        else if (sortArray[newMid] > key) {
            tempMax = newMid;
            newMid = tempMax/2 - tempMin/2;
            count++;
        }
    }
    binSteps = count;
    bStepField.setText(Integer.toString(binSteps));
    log.append(twolines);

    if (result != -1) {
        log.append("Success!  The number " + Integer.toString(key) + " was found " +
                "at array location " + result + "." + newline);
    }
    else if (result == -1) {
        log.append("Failure.  The number " + Integer.toString(key) + 
                " was not found in the array." + newline);
    }

    log.append("The binary search was completed in " + Integer.toString(binSteps) + 
            " steps." + newline + newline);
    log.setCaretPosition(log.getDocument().getLength());

}


private int[] sort(int[] arrayIn) {
    // Method for sorting the random array before
    // performing a binary search
}
  • 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-06-16T05:57:12+00:00Added an answer on June 16, 2026 at 5:57 am

    Doing tempMax/2 - tempMin/2 will not get you the midpoint. Consider a simple example: if tempMin = 2 and tempMax = 5, then tempMax/2 - tempMin/2 = 5/2 - 2/2 = 2 - 1 = 1.

    The typical way to get the midpoint without overflow is mid = (min + max) >>> 1.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.