1

Hi I wonder if how can I put a validation on the while of if statements so the program will only execute if the user enters the commands "move" "line" "circle" with their parameters. For example if the user enters "move 200" the program will say Invalid because there's only one or NO parameter. Thanks!

import java.util.Scanner;

public class DrawingProgram1 {

public static void main(String[] args) {

    GraphicsScreen g = new GraphicsScreen();

    String store;
    String command;

    int move1;
    int move2;
    int line1;
    int line2;
    int circle;

    while (true) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Type 'help' for list of commands. Type 'end' to finish.");
        System.out.println("Please enter a command:");
        store = scan.nextLine();
        String [] splitUpText = store.split(" ");
        command = splitUpText[0];


        if (command.equalsIgnoreCase("move")) {
            move1 = Integer.parseInt(splitUpText[1]);
            move2 = Integer.parseInt(splitUpText[2]);
            g.moveTo(move1, move2);

        }
        else if (command.equalsIgnoreCase("line")) {
            line1 = Integer.parseInt(splitUpText[1]);
            line2 = Integer.parseInt(splitUpText[2]);
            g.lineTo(line1, line2);
        }
        else if (command.equalsIgnoreCase("circle")) {
            circle = Integer.parseInt(splitUpText[1]);
            g.circle(circle);
        }
        else if (command.equalsIgnoreCase("help")) {
            System.out.println("Enter a command for move.");
            System.out.println("Enter a command for line.");
            System.out.println("Enter a command for circle.");
        }
        else if (command.equalsIgnoreCase("end")) {
            System.exit(0);
        }
        else {
            System.out.println("Error");
        }
    }   





}

}
2
  • you may force your user to use another input format, e.g. move:"200" Commented Nov 9, 2012 at 9:45
  • Simply check the length of splitUpText. If its not 3, continue the while loop after showing the message. Commented Nov 9, 2012 at 9:47

5 Answers 5

2

In the case , add extra condition for if statement, for eg, you can say

if(command.equalsIgnoreCase("move") && splitUpText.length == 3){

 //do necessary actions
}

for move command, the array size should not be less than or greater than 3.

Add conditions for other if statements according to the parameters for each command.

Sign up to request clarification or add additional context in comments.

Comments

1

You could have either the wrong number of arguments or invalid arguments. Use this to catch 'em all:

if (command.equalsIgnoreCase("move")) {
    try {
       move1 = Integer.parseInt(splitUpText[1]);
       move2 = Integer.parseInt(splitUpText[2]);
       g.moveTo(move1, move2);
    } catch (ArrayIndexOutOfBoundsException e1) {
       Sytem.out.println("Please specify 2 parameters!")
       continue;
    } catch (NumberFormatException e2) {
       Sytem.out.println("Invalid parameters!")
       continue;
    }
}

2 Comments

Fairly ugly - why not simply check the length of splitUpText instead?
because he needs try\catch for the NumberFormatException anyway, and this way he can always add parameters to his commands and implement their actions without having to remember to also update the if statement.
1

What if you wrap all those requests in Command objects? I think that the Command design pattern fits well in your case. Command pattern explanation

Comments

0

replace this

if (command.equalsIgnoreCase("move")) {
    move1 = Integer.parseInt(splitUpText[1]);
    move2 = Integer.parseInt(splitUpText[2]);
    g.moveTo(move1, move2);
}

whith

if (command.equalsIgnoreCase("move")) {
    try {
       move1 = Integer.parseInt(splitUpText[1]);
       move2 = Integer.parseInt(splitUpText[2]);
       g.moveTo(move1, move2);
    } catch (ArrayIndexOutOfBoundsException e) {
       Sytem.out.println("move needs 2 parameters!")
       continue;
    }
}

and apply this to your other commands.

1 Comment

Fairly ugly - why not simply check the length of splitUpText instead?
0

You can use:

if (splitUpText.lenght() != 3)
    System.err.println("Invalid...");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.