Results 1 to 4 of 4
  1. #1
    Spiritwo is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Java if else statement not working

    Most relevant code:
    Java Code:
    if (cmd.getName().equalsIgnoreCase("silkon"))
    			{
    				if (args.length == 0)
    				{
    					if (player.hasPermission("MachiSilk.silktoggle") || player.isOp())
    					{
    						if (this.activePlayers.contains(playerName))
    						{
    							player.sendMessage(ChatColor.RED + "Silk Touch mode is already activated.");
    							return true;
    						}
    						
    						this.activePlayers.add(playerName);
    						player.sendMessage(ChatColor.GREEN + "Silk Touch mode enabled. " + ChatColor.DARK_AQUA + "\nType " + ChatColor.YELLOW + "/silkoff" + ChatColor.DARK_AQUA + " to disable silk touch mode.");
    						return true;
    					}
    					else
    					{
    			            System.out.println("1");
    			            player.sendMessage(ChatColor.RED + "You do not have permission for this command.");
    			            return true;
    			        }
    				}
    The else statement is not outputting a "1" or sending the player the message as stated.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java if else statement not working

    To see what is happening when you run the code use some lines to output useful debugging information:

    Java Code:
    if (cmd.getName().equalsIgnoreCase("silkon"))
    {
        System.out.println("silkon: args.length =" + args.length);
        if (args.length == 0)
        {
            System.out.println("has permission = " + player.hasPermission("MachiSilk.silktoggle"));
            System.out.println("is op = " + player.isOp());
            if (player.hasPermission("MachiSilk.silktoggle") || player.isOp())
            {
                System.out.println(
                        "active players contains name = " 
                        + this.activePlayers.contains(playerName));
                if (this.activePlayers.contains(playerName))
                {
                    player.sendMessage(
                            ChatColor.RED + "Silk Touch mode is already activated.");
                    return true;
                }
    						
                this.activePlayers.add(playerName);
                player.sendMessage(
                        ChatColor.GREEN + "Silk Touch mode enabled. " + ChatColor.DARK_AQUA 
                        + "\nType " + ChatColor.YELLOW + "/silkoff" + ChatColor.DARK_AQUA 
                        + " to disable silk touch mode.");
                return true;
            }
            else
            {
                System.out.println("1");
                player.sendMessage(
                        ChatColor.RED + "You do not have permission for this command.");
                return true;
            }
        }
    If you can't understand the output, post it and the code you are using. Note that for code to be readable here it is preferable to use use spaces to indent rather than tab characters and to avoid excessive line length.

  3. #3
    Spiritwo is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Java if else statement not working

    I've tried placing debugging output lines in other locations and they all work, except within the else statement.
    Full .java:
    #4269919 - Pastie

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Java if else statement not working

    Try erasing the close-bracket on line 98 of your Pastie code. I'm not sure if it will solve your problem, but it doesn't need to be there.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. Why isn't this if statement working?
    By ocomobock in forum New To Java
    Replies: 3
    Last Post: 03-27-2012, 02:13 AM
  2. 'if' statement not working?
    By Etimer in forum New To Java
    Replies: 4
    Last Post: 03-10-2012, 05:18 AM
  3. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 08:40 PM
  4. SQL statement working in one class but not another
    By Dcalladi in forum New To Java
    Replies: 4
    Last Post: 11-11-2011, 01:35 AM
  5. if statement with strings not working... again
    By hardcorebadger in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 06:02 AM

Posting Permissions

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