Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For loop, illegal start of expression

    I'm trying to put a for loop into an if statement for a project of mine (code below)

    Code:
    public void recieveAttack(Player a, Attack x)
        {
            if ( 
            for ( int i = 0; i < 6; ) {
            if (a.(getWeaknesses(i)).equals(x.getType()))
            {
                return true;
            }
            else 
            {
                i++;
            }
            })
            {
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage() * 2);
        }
    I'm getting an "illegal start of expression" error on the line "for ( int i = 0; i < 6; ) {" As far as I can see my syntax is correct, but I'm fairly new to Java so who knows. Why is it doing this?

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    try

    Code:
    for ( int i = 0; i < 6; i++) {         
    if (a.(getWeaknesses(i)).equals(x.getType()))         {
         return true; 
    } 
    }

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Changed it. Same error, same line

    Code:
     public void recieveAttack(Player a, Attack x)
        {
            if ( 
            for ( int i = 0; i < 6; i++) {
            if (a.(getWeaknesses(i)).equals(x.getType())) {
                return true;
            }
            })
            {
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage() * 2);
        }

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    are you sure you can use a for loop as an if test condition?

  • #5
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To be honest I have no idea, but it would seem that it isn't possible. I'm trying to test an array of strings to see if any of them match the "weakness" string, and this is the only way I could think of to iterate through the entire array.

  • #6
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    ok, looking at your code it looks like that if the "weakness" string is not in the array of strings then the rest of the code in the method is executed.

    If that is so, try

    Code:
     
    public void recieveAttack(Player a, Attack x)
        {
            for ( int i = 0; i < 6; i++) {
                if (a.(getWeaknesses(i)).equals(x.getType())) {return true;}
            }
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage() * 2);
        }

  • #7
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, it's the other way around. If the string does exist within the array, then I want the code to be executed.

    Also, I didn't copy the entire method before because it's unfinished, but there are more conditions further down in case the string isn't found, hence the if at the beginning.

    Here's the entire method; I intend to change the other conditions to more for loops however.

    Code:
     public void recieveAttack(Player a, Attack x)
        {
            if  (
            for ( int i = 0; i < 6; i++) {
            if (a.(getWeaknesses(i)).equals(x.getType())) {
                return true;
            }
            })
            {
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage() * 2);
        }
        else if ((a.getNve()).equals(x.getType()))
            {
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage() * (1/2));
        }
        else if ((a.getResistance()).equals(x.getType()))
            {
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage() * 0);
        }
        else
        {
            a.setAttackstat(x.getAttchange());
            a.setDefensestat(x.getDefchange());
            a.setSpeed(x.getSpeedchange());
            a.setAccuracy(x.getAccchange());
            a.setHp(x.getDamage());
        }
    }

  • #8
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I figured out a better way to get it to work, and I'm pretty sure it's impossible to do it the way I was trying. Thanks for your help anyway

  • #9
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    no problem - glad you sorted it out


  •  

    Posting Permissions

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