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 3 of 3

Thread: Search Term Program Problem

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Search Term Program Problem

    I am having a problem with the output of my term searching program. When I run the program, it seems to work okay but the problem is that when i search a term that is in the ArrayList position of 3 for example, the position outputs 0 instead. It is almost as if the position counter is not increasing inside the loop. If anyone can help me out I would appreciate it.
    package searcher;
    import java.util.*;
    import javax.swing.*;
    /**
     *
     * @author Shane
     */
    public class Searcher {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //Get terms
            ArrayList<String> terms = getTerms();
     
            String searchTerm = JOptionPane.showInputDialog("Please enter a search term.");
            int position = 0;
            boolean found = false;
            while (position < terms.size() && !found){
                if (terms.contains(searchTerm)){
                    found = true;
                }else{
                    position++;
                }
            }
            if (found){
                JOptionPane.showMessageDialog(null, "String " + searchTerm + " found at position " + position + ".");
            }else{
                JOptionPane.showMessageDialog(null, "String " + searchTerm + " not found.");
            }
     
     
     
        }
     
        public static ArrayList<String> getTerms(){
            ArrayList<String> terms = new ArrayList();
            String words;
            do{
                words = JOptionPane.showInputDialog("Please enter words one at a time.\nWhen finished, leave input field blank and press enter.");
                if(!words.equalsIgnoreCase("")){
                    terms.add(words);
                }
            }while(!words.equalsIgnoreCase(""));
            return terms;
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Search Term Program Problem

    If the List contains the element you are searching for, then terms.contains will always return true. Why not use indexOf instead?

  3. The Following User Says Thank You to copeg For This Useful Post:

    Exiled (March 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Search Term Program Problem

    Thanks copeg, that's what I was missing! It runs perfectly now and outputs exactly what it's supposed to. Here is the updated code:

    package searcher;
    import java.util.*;
    import javax.swing.*;
    /**
     *
     * @author Shane
     */
    public class Searcher {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //Get terms
            ArrayList<String> terms = getTerms();
     
            String searchTerm = JOptionPane.showInputDialog("Please enter a search term.");
            int position = 0;
            boolean found = false;
            while (position < terms.size() && !found){
                if (terms.contains(searchTerm)){
                    found = true;
                    position = terms.indexOf(searchTerm);
                    break;
                }else{
                    position++;
                }
            }
            if (found){
                JOptionPane.showMessageDialog(null, "String " + searchTerm + " found at position " + position + ".");
            }else{
                JOptionPane.showMessageDialog(null, "String " + searchTerm + " not found.");
            }
     
     
     
        }
     
        public static ArrayList<String> getTerms(){
            ArrayList<String> terms = new ArrayList();
            String words;
            do{
                words = JOptionPane.showInputDialog("Please enter words one at a time.\nWhen finished, leave input field blank and press enter.");
                if(!words.equalsIgnoreCase("")){
                    terms.add(words);
                }
            }while(!words.equalsIgnoreCase(""));
            return terms;
        }
    }

Similar Threads

  1. 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, 10:20 AM
  2. Lowest Term Function Help
    By thisbeme in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 2nd, 2011, 07:33 AM
  3. [SOLVED] Search file program
    By righi in forum Java Theory & Questions
    Replies: 4
    Last Post: November 15th, 2010, 12:08 PM
  4. Java Web Browser Term Project- I am facing multiple problem...pls help
    By tazbinur in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 8th, 2010, 10:05 AM
  5. search problem
    By farvi_buet in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 8th, 2010, 02:26 PM

Tags for this Thread