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: Help with searching an array for a String (name) using a for loop

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with searching an array for a String (name) using a for loop

    Hey there,

    Working through a Java assignment and seem to have hit a road block. Basically it asks you to create a Java Applet that allows a user to search for a tiger's name through an array of 10 tiger's. If it finds a tiger's name in the array, it will then display that tiger's details using a previously created constructor. Don't focus too much on the constructor, I have that part working fine in another program class required by the assignment. I think there's something wrong with my for loop so I'll just go ahead and link it and see what you guys think:

    Tiger [] tiger = new Tiger[10]; // declaring an array of 10 Tiger objects named "tiger"
     
    tiger[0] = new Tiger("Claw", 5, "Melbourne Zoo", 'F'); // an example of one of the array objects
     
    public void actionPerformed(ActionEvent  event )
       {
          if(event.getSource() == btnMain) // if the btnMain button is pressed perform the following
          {
            if(!txtName.getText().equalsIgnoreCase("")) // checks if there is text in the txtName text field
            {
            String name;
     
            name = txtName.getText(); // txtName is the name of the text field where a user will input a name
     
            for(int i = 0; i<tiger.length; i++)
            {
                if(name.equals(tiger[i].getName())) // getName() is a simple method that returns the name
                {
                    lblRes1.setText("Worked"); // if it finds a name in the array, show "Worked" in the lblRes1 label
                }
                else
                {
                    lblRes1.setText("Error"); // otherwise show "Error"
                }
            }
            }
          }
       }

    Obviously I haven't linked the full code because I don't think it's necessary to show you parts like the adding of objects to the form, changing their fonts, etc. but if you guys need me to I can. Like I said earlier I'm fairly confident the error lies with my for loop.

    At the moment all I'm trying to get it to do is return a "Worked." if it finds a name in the array & an "Error." if it doesn't but all it's doing right now is showing "Error" either way. Any help is appreciated!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with searching an array for a String (name) using a for loop

    You may also want to use equalsIgnoreCase() when comparing the String from the JTextField to the result of getName(), but you should know if that's necessary.

    The reason you're always seeing "Error" is because the for loop looks at every tiger's name in the array and has been ending on an error. "Worked" is probably being sent, but it's going by so fast that you haven't noticed. Break out of the for loop when the first if statement in the for loop is successful.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with searching an array for a String (name) using a for loop

    Quote Originally Posted by GregBrannon View Post
    You may also want to use equalsIgnoreCase() when comparing the String from the JTextField to the result of getName(), but you should know if that's necessary.

    The reason you're always seeing "Error" is because the for loop looks at every tiger's name in the array and has been ending on an error. "Worked" is probably being sent, but it's going by so fast that you haven't noticed. Break out of the for loop when the first if statement in the for loop is successful.
    Hey, thanks for the quick response. Good point about the equalsIgnoreCase() - it's not really required by the assignment but it's still good to cover all grounds.

    Added the break and it works fine now, thanks!

Similar Threads

  1. Replies: 6
    Last Post: March 1st, 2013, 02:06 PM
  2. Searching for string values in a circular list
    By hippo1974 in forum Object Oriented Programming
    Replies: 10
    Last Post: July 11th, 2011, 04:36 AM
  3. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. searching a string
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 01:31 AM