Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Errors checking

oLgAa25oLgAa25 Member Posts: 5
I have a question
I have this command line calculator, but I want to make changes, like check for users input( like a char, or a double number) and then I want to display a message into the console saying that was wrong, try again
how could I do that
help please
Olga

Comments

  • merlin_barmerlin_bar Member Posts: 136
    [b][red]This message was edited by merlin_bar at 2003-2-27 20:1:43[/red][/b][hr]
    Olga, you didn't post any code, but by the sounds of things all you need is a few if else statements to decide what to do depending on the input. Unless I totally misunderstand you, your problem is fairly simple. =)


  • oLgAa25oLgAa25 Member Posts: 5
    Hello again
    You are right I need if statements, but how do I do it??
    thank you again
    Olga
    here is my code:

    // Exercise11_1.java: Pass parameters from the command line
    public class Exercise11_1
    {
    // Main method
    public static void main(String[] args)
    {
    // The result of the operation
    int result = 0;

    if (args.length != 3)
    {
    System.out.println(
    "Usage: java Calculator operator operand1 operand2");
    System.exit(0);
    }

    // Determine the operator
    switch (args[0].charAt(0))
    {
    case '+': result = Integer.parseInt(args[1]) +
    Integer.parseInt(args[2]);
    break;
    case '-': result = Integer.parseInt(args[1]) -
    Integer.parseInt(args[2]);
    break;
    case '*': result = Integer.parseInt(args[1]) *
    Integer.parseInt(args[2]);
    break;
    case '/': result = Integer.parseInt(args[1]) /
    Integer.parseInt(args[2]);
    }

    // Display result
    System.out.println(args[1] + ' ' + args[0] + ' ' + args[2]
    + " = " + result);
    }
    }
Sign In or Register to comment.