Stupid if-then-else statements. (this should be basic)

A place for tutorials on how to get the most out of Flash

Stupid if-then-else statements. (this should be basic)

Postby BlueLight » Mon Apr 16, 2012 7:20 am

Okay, so i've always had a problem with If statements and i don't know why. Anyways I need to fix this before I can't get something important to work.

I've got this section of code.
Code: Select All Code
public void actionPerformed(ActionEvent event) {
      // TODO Auto-generated method stub
      String state = event.getActionCommand();
      Log.Logging(this.getClass().getName(), ""+state);
      if(newGameString == state){Center.texyTextAdderCenter(state, false, true);}
      if(saveGameString == state){Center.texyTextAdderCenter(state, false, true);}
      if(loadGameString == state){Center.texyTextAdderCenter(state, false, true);}
      if(optionsString == state){Center.texyTextAdderCenter(state, false, true);}
      if(quitString == state){Center.texyTextAdderCenter(state, false, true);}
      else {
         Center.texyTextAdderCenter("What the F did you do? This is a bug and needs to be reported!... Like now.", false, true);
         Center.texyTextAdderCenter("I need you to go to LoK forums or my blog and post this third line in a comment or something that I can read.", false, true);
         Center.texyTextAdderCenter(event+"", false, true);
         Center.texyTextAdderCenter("Again, how the hell did you do this? My if statement logic leaves no room for error so how the hell did you trigger the else statement?", false, true);
      }
   }
}


Here's the problem, when ever my action listener fires (the thing above.) it will compare the values just fine, but, the else statement will also fire. I'm getting results like
Code: Select All Code
New Game
What the F did you do? This is a bug and needs to be reported!... Like now.
I need you to go to LoK forums or my blog and post this third line in a comment or something that I can read.
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=New Game,when=1334556942200,modifiers=Button1] on javax.swing.JMenuItem[,1,3,85x21,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.MetalBorders$MenuItemBorder@360be0,flags=264,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=true,paintFocus=false,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=New Game]
Again, how the hell did you do this? My if statement logic leaves no room for error so how the hell did you trigger the else statement?

from one click.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Stupid if-then-else statements. (this should be basic)

Postby dirtyc101 » Tue Apr 17, 2012 10:23 pm

See Attachment. Looks to me like you have an extra } floating, which is probably messing up your logic.
DirtyC101
ClipBoard.jpg
ClipBoard.jpg (64.57 KiB) Viewed 1918 times
dirtyc101
 
Joined: Fri May 21, 2010 10:18 pm

Re: Stupid if-then-else statements. (this should be basic)

Postby BlueLight » Tue Apr 17, 2012 11:46 pm

dirtyc101 Wrote:See Attachment. Looks to me like you have an extra } floating, which is probably messing up your logic.
DirtyC101
ClipBoard.jpg


WHY MUST YOU BE TALKING TO MY DAD, GAH!!!!!
He's always telling me that i should comment after a bracket so people know what's open and what's closed.

So the third bracket is to state the class ends. classes are objects or items that you can change and modify in some way.
So a int is a object and there is in fact a class called integer in the API (primitives are the strangest objects because they have their own class but you don't have to call the class to make them.)
basically that should be there in the main code but you can't see what that is connected to with this example.

Anyways i fixed the problem, this is stupid beyond belief which i'm not shocked to learn, you have put a else in front of the if statements after the first one.

Code: Select All Code
public void actionPerformed(ActionEvent event) {//start actionListener, in GameMenuBar class.
      // TODO Auto-generated method stub
      String state = event.getActionCommand();
      Log.Logging(this.getClass().getName(), ""+state);
      if(newGameString == state){Center.texyTextAdderCenter(state, false, true);}
      else if(saveGameString == state){Center.texyTextAdderCenter(state, false, true);}
      else if(loadGameString == state){Center.texyTextAdderCenter(state, false, true);}
      else if(optionsString == state){Center.texyTextAdderCenter(state, false, true);}
      else if(quitString == state){System.exit(0);}
      else {
         Center.texyTextAdderCenter("What the F did you do? This is a bug and needs to be reported!... Like now.", false, true);
         Center.texyTextAdderCenter("I need you to go to LoK forums or my blog and post this third line in a comment or something that I can read.", false, true);
         Center.texyTextAdderCenter(event+"", false, true);
         Center.texyTextAdderCenter("Again, how the hell did you do this? My if statement logic leaves no room for error so how the hell did you trigger the else statement?", false, true);
      }// end else
   } //end method
}// end class


I also added a few comments.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Stupid if-then-else statements. (this should be basic)

Postby IrrelevantComment » Sun Apr 22, 2012 2:28 pm

Sorry but that isn't stupid, it makes so much sense :P Without the else statement, each if statement is being called in turn regardless of whether the others returned true.

Although you could also put "return;" into each of the curly brace sections after the if statements, that way it would end the function once one returns true
IrrelevantComment
 
Joined: Tue Mar 15, 2011 7:46 pm

Re: Stupid if-then-else statements. (this should be basic)

Postby BlueLight » Sun Apr 22, 2012 10:16 pm

You can only put return in to a method that has something to return. This method returns "Void" which is a another way of saying nothing.
Why is everyone commenting on my request for help today? I mean i fixed these at least a week ago.

As for why it's stupid. According to oracle, the if() statements test until one passes the test or you run out of if() statements
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users