Home > Net >  Issues regarding if-else
Issues regarding if-else

Time:12-24

My problem is that in my code, the error message is not being properly displayed. I know this is some issue with the if and else statements in my code, but I can't place exactly what is wrong/needs to be done.

Here is the skeleton of my code: ``

//there's a String called command taking in scanner input at the beginning of the loop
while(true){
if (command.equalsIgnoreCase("add")) { 
    // adds a new task to the list
}

else if (command.equalsIgnoreCase("set")) { 
    // sets the priority of a task
}

else if (command.equalsIgnoreCase("complete")) { 
    // marks a task as completed
}

else if (command.equalsIgnoreCase("delete")) { 
    // removes a task from the list
}

else if (command.equalsIgnoreCase("search")) { 
    // searches for tasks containing a specified search term
}

else if (command.equalsIgnoreCase("print") || command.equalsIgnoreCase("-p")) { 
    // prints the list of tasks
}

else if (command.equalsIgnoreCase("save") || command.equalsIgnoreCase("-r")) { 
    // saves the list of tasks to a file
}

else if (command.equalsIgnoreCase("help") || command.equalsIgnoreCase("-h")) { 
   // displays a list of commands and their descriptions
}

else if (command.equalsIgnoreCase("exit") || command.equalsIgnoreCase("-x")) { 
    // exits the program
}

if (command.length() >= 3) {
    
    if (command.substring(0, 3).equals("-a ")) { 
        // adds a new task to the list
    } 
    else if (command.substring(0, 3).equals("-s ")) { 
        // sets the priority of a task
    }
     else if (command.substring(0, 3).equals("-c ")) { 
        // marks a task as completed
    }
     else if (command.substring(0, 3).equals("-d ")) { 
        // removes a task from the list
    }
     else if (command.substring(0, 3).equals("-v ")) { 
        // searches for tasks containing a specified search term
    } 
    else {//empty}
}

else { 
    // system.out.prints an error message if the input is not a recognized command
}
}

`` To further explain, I had to add the additional if statment in all of the other if elses since substring was causing my program to crash. It now works as intended, but the problem is that when the user types something wrong it doesn't tell them it's wrong, and sometimes they might type a command (which works properly) but then it tells them there's an error.

CodePudding user response:

You might use the command switch instead, and put your error code in the default, or surround with :

try {
// Your code
} catch (IOException e) {
System.err.println("Error : "   e.getMessage());
}

CodePudding user response:

Here's a minimal reproducible example for free:

public class Eg {
    public static void main(String[] args) {
        while (true) {
            String command = "set";
            if (command.equalsIgnoreCase("set")) {
                // adds a new task to the list
            }
            if (command.length() > 3) {
                if (command.substring(0, 3).equals("-a ")) {
                    // adds a new task to the list
                }
            } else {
                System.out.println("Bad command:"   command);
                break; // leave the loop to make debugginmg easier
            }
        }
    }
}

It's much easier to understand your code when it is short and correctly formatted.

You can see that we always execute the if (command.length() > 3) block, so if command length is not more than 3, we will always print the error, even if one of the commands in the first set of if/else statements matched.

The simplest way to fix your issue would be to add a continue; statement in each block where a command is recognised, and move your error reporting to a statement at the end of the loop:

public class Eg {
    public static void main(String[] args) {
        while (true) {
            String command = "seu";
            if (command.equalsIgnoreCase("set")) {
                System.out.println("set");
                continue;
            }
            if (command.length() > 3) {
                if (command.substring(0, 3).equals("-a ")) {
                    System.out.println("-a");
                    continue;
                }
            }
            System.out.println("Bad command:"   command);
        }
    }
}

The continue statement takes the flow of control back to the start of the loop. (The loop condition actually)

  • Related