Results 1 to 3 of 3
  1. #1
    justinm231 is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Please help me with this loop?

    I have a loop:

    for(int i = 0; i<array.length; i++){


    if (array[i] == "" && array[i+1].contains("@")){

    } while(array[i+1] != "" && !array[i+2].contains("@"));


    It parses through a String which has multiple emails with the format of, the first line will be blank, then the next line will have an email address in it. Please look at code to see where it fails, my brain is fried. Or is there a better way to do this? Thanks!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Please help me with this loop?

    Don't use == to compare strings or other objects, use the equals() method. All classes have an equals() method and it is intended to do the right thing: in this case return true if and only if the two strings consist of the same characters in the same order.

    Java Code:
    // if(foo == bar) {...
    if(foo.equals(bar)) {...
    // if(foo != bar) {...
    if(!foo.equals(bar)) {...
    There is also an equalsIgnoreCase() that can be useful.


    When you post code, use the "code" tags. Ie put [code] at the start of the code and [/code]. That way it remains readable when it appears within a web page.

    Also it is helpful if you can be descriptive. "it fails" doesn't say as much as a description of the actual behaviour when you run (or compile) the program and the behaviour you expected or wanted.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: Please help me with this loop?

    Don't use == (or !=) with Strings. Use the equals() method instead. This advice has been repeated countless times here, so I'll let you do a search for more information.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  4. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 05:21 PM
  5. Replies: 5
    Last Post: 04-30-2010, 06:05 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