Results 1 to 7 of 7
  1. #1
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default For Loop, illegal start of expression

    I can't figure out why this for loop is giving me the "Illegal start of expression" error when I try to compile it.

    Java Code:
     public boolean isWeakTo(Type t)
        {
            if 
            (
            [COLOR="Red"]for (i=0; i<4; i++)[/COLOR]
            {
                if (weaknesses[i].equals(t.getType()))
                {
                    return true;
                }
            }
            )
            {
                return true;
            }
            else
            {
                return false;
            }
        }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    It is not the for loop. It is the if statement. Where is your condition?

  3. #3
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    The condition is (supposed to be) that the for loop returns true.

    Java Code:
    public boolean isWeakTo(Type t)
        {
            if 
            [COLOR="Red"](
            for (i=0; i<4; i++)
            {
                if (weaknesses[i].equals(t.getType()))
                {
                    return true;
                }
            }
            )[/COLOR]
            {
                return true;
            }
            else
            {
                return false;
            }
        }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Huh? That makes no sense. Fully explain in English the goal of your code. Not how you are trying to achieve it.

  5. #5
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Sorry haha.

    I want the method to read an array of strings and test if the string provided as a parameter is present in that array, and if so, return true.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    In that case the outer if statement is pointless.
    Java Code:
    loop {
        if found {
            return true
        }
    }
    return false

  7. #7
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Aahhh Ok. I tried to do that earlier, but forgot the "return false" at the end, so it didn't work. Then I guess I just got tied up in unnecessary loops. Thanks for your help!

Similar Threads

  1. Illegal start of expression
    By Nigel in forum New To Java
    Replies: 5
    Last Post: 03-11-2011, 01:42 PM
  2. Need help with illegal start of expression
    By WhopperMan in forum New To Java
    Replies: 4
    Last Post: 10-10-2010, 02:58 AM
  3. illegal start of expression
    By rajivjoshi in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 09:12 AM
  4. Illegal Start of Expression
    By vince425 in forum New To Java
    Replies: 3
    Last Post: 10-18-2008, 07:41 AM
  5. Illegal start of expression
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:09 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
  •