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!