i have wrote this program which uses binary search to search for a desired word and which displays the word along with the meaning of that particular word.but each time i print the data that the arraylist contains it just print the word being search but not the meaning of that word.. i have attached the textfile from which i am taking the in post. an example would be when i search robot it should display robot along with the meaning of robot but it wont print the meaning.. it just print the work all the time.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //BufferedReader is used to read from text file
         if(jTextField1.getText().equals("")){
             JOptionPane.showMessageDialog(null,"Please add a keyword to be able to search");
         }
        try( BufferedReader buf = new BufferedReader(new FileReader("C:\\Users\\Dell\\Documents\\NetBeansProjects\\InformationRetrievalSystem1\\word.txt"))) {
            //creating array name words with datatype String
            ArrayList<String> words = new ArrayList<String>();
            String lineJustFetched = null;
            String[] wordsArray;//creating an array of string name wordsArray

            while(true){
                lineJustFetched = buf.readLine();//reading line in the textfile
                if(lineJustFetched == null){  
                    break; 
                }else{
                    wordsArray = lineJustFetched.split("\t");//making the reader know that the word and the meaning is separated by delimiter TAB.
                    //joins the word and the meaning together
                    for(String each : wordsArray){
                        if(!"".equals(each)){
                            words.add(each);

                        }
                    }
                }
            }

    String search = jTextField1.getText();//take using input from jtextfield
            for(String each : words){
           jTextArea1.setText(each);//display the meaning in jtextArea
                }
    Collections.sort(words);//sorting the words
    /**
     * the reason for sorting the word is because binary search
     * works best with sorted list rather than unsorted one.
     */

    //calling the method binary search
    int searchIndex = Collections.binarySearch(words,search);

    /**
     * there is two function being executed:
     * if the word is found it will display the word + the index where it is found.
     * else if the word search does not exist then it will display not found.
     */
    jTextPane1.setText(searchIndex != -1 ? words.get(searchIndex)+" - Index is "+searchIndex : "Not found");

}      catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "dictionary not found");//if the file is missing the application will display tis error.

       } catch (IOException ex) {

       }

    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BinarySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BinarySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BinarySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BinarySearch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BinarySearch().setVisible(true);
            }
        });

    }
    //the method used to do all the searching
    public static int binarySearch(String[] a, String x) {
    int low = 0;
    int high = a.length - 1;
    int mid;
// If the element is present at the middle itself
    while (low <= high) {
        mid = (low + high) / 2;

// If element is smaller than mid, then it can only be present
        // in left subarray
        if (a[mid].compareTo(x) < 0) {
            low = mid + 1;

       // Else the element can only be present in right subarray
        } else if (a[mid].compareTo(x) > 0) {
            high = mid - 1;
        } else {
       // We reach here when element is not present in array
            return mid;
        }
    }

    return -1;
    }

Try displaying the arraylist - it looks like maybe you have words and meanings originally adjacent (?), but after sorting there's no relationship between the word to its meaning

I see no code to display a meaning anyway

you have an empty catch, so there may be a serious error that you are ignoring

lines 28 etc you keep changing the text ina loop so only the last pass will have any effect (other than to slow things down)

... I:m sure there's more.
It's better to write your code in small sections and test each before moving on. That way you only have one or two bugs at a time, and you know where to look for them.

ps Why not use a map<word, meaning>?

thank you. you were ryt on point. the sorting part was causing all the trouble. i fixed it by sorting the word while it is saving in the array. thanks alot.

I too suggested a map, but which map implementation "automatically sorts the key"? The obvious (to me at least) would be HashMap that uses hashes for a O(1) time for get and put, but the keys are not ordered in any user-meaningful way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.