Results 1 to 2 of 2
  1. #1
    katon is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default 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.


    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 if match found at 21 not necessary to go all the way to 100.....

    Simple bit but I am stuck!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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:
    Java Code:
    if (wordObj[j].getWord()==word) {
    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().equals(word)) {

    Much luck!

Similar Threads

  1. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  2. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 08:51 PM
  3. [SOLVED] Array Programming Construct?
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-09-2009, 02:53 AM
  4. Java Programming, Array Help
    By Just Incredible in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 04:18 PM
  5. simple loop syntax( ; ;), ( something : somthing)
    By xcallmejudasx in forum New To Java
    Replies: 4
    Last Post: 12-13-2008, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts