Results 1 to 2 of 2
- 12-20-2009, 02:30 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
Simple Programming problem loop in array
When a if condition is met I want an incremental loop to exit without continuing the loop. How can I do this.
Basically get a word string from user : iterate thorugh 1 string variable in an array of 100 objects.
wordObj[j].getWord()==word
upto j=100
wordObj is an array of objects
getWord retrieves a word string in each object.
Basically go through all array objects if match found exit the 100 loop....ie if match found at 21 not necessary to go all the way to 100.....
Simple bit but I am stuck!!
-
a break can get you out, or you can use a boolean variable that the loop checks before looping, and set it if the word is found.
Incidentally, don't do this:
because you shouldn't compare String variables or literals for equality using ==. The == operator checks to see if one variable refers to the same object as another. But this isn't what you care about. You care that the Strings hold the same text sequences regardless if they are the same object or not. So instead compare Strings with the equals or equalsIgnoreCase method:Java Code:if (wordObj[j].getWord()==word) {
Java Code:if (wordObj[j].getWord().equals(word)) {
Much luck!
Similar Threads
-
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
Need Some Help, Simple Loop For Hangman Game
By Juo in forum New To JavaReplies: 2Last Post: 11-14-2009, 07:51 PM -
[SOLVED] Array Programming Construct?
By iPetey in forum New To JavaReplies: 3Last Post: 04-09-2009, 02:53 AM -
Java Programming, Array Help
By Just Incredible in forum New To JavaReplies: 2Last Post: 01-24-2009, 03:18 PM -
simple loop syntax( ; ;), ( something : somthing)
By xcallmejudasx in forum New To JavaReplies: 4Last Post: 12-13-2008, 12:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks