1

*homework

Using the command line command

java class -add 7 8

will add all integers after "-add". But for this program, I am also suppose to print "Argument type mismatch" if anything after "-add" is not an integer.

I have this method to search a String for integers. If it finds something other than an integer, it returns false.

public static boolean isInteger(String s) {
    try {
        int num = Integer.parseInt(s);
        return true;

    } catch (Exception e) {}
    return false;
}

Now in this method I try to use the isInteger method to look at a String in the first for loop.

private static void add(String[] args) {
    for (int j = 1; j < args.length; j++) {
        if (isInteger(args[j]) == false)
        System.out.println("Argument type mismatch");
    }
    if (args.length == 1)
        System.out.println("Argument count mismatch");
    else {
        int result = 0;
        for (int i = 1; i < args.length; i++) {
            result += Integer.parseInt(args[i]);
        }
        System.out.println(result);
    }  
}

Running the first for loop by itself produces "Argument type mismatch" perfectly fine. But when I run the whole method and type the command

java class -add cat

it produces "Argument type mismatch" followed by a java.lang.NumberFormatException: For input string: "cat" error at the line result += Integer.parseInt(args[i]);

How can I fix this error?

2
  • Adding return; after System.out.println("Argument type mismatch"); should solve it. Commented Oct 7, 2014 at 1:11
  • This is because you are passing 2 arguments that is -add and cat to the class. The Integer.parseInt in else throws this error. Commented Oct 7, 2014 at 1:12

2 Answers 2

4

You get the error because you don't terminate exection once you find there's a problem with the input.

Add a return; statement to the code the executes when there's a problem:

for (int j = 1; j < args.length; j++) {
    if (!isInteger(args[j])) {
        System.out.println("Argument type mismatch");
        return; // Added this line
    }
}

Also note the slight change in the if condition. It's better coding style to not compare with boolean literals, ie change:

if (isInteger(args[j]) == false)

to

if (!isInteger(args[j]))

The reason this is good coding practice is it prevents this happening:

// Oops! An assignment that compiles and executes as "true"
if (someBooleanVariable = true)

when you meant to code a comparison:

if (someBooleanVariable == true)

The visual difference is subtle, and can lead to frustrating bugs if you don't visually pick it up, but there is no chance of this problem if you code:

if (someBooleanVariable)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the tip on the if condition. Better to stop coding like this right when I'm in the learning process.
@Jerry yeah - see edit to answer for why it's not good practice
Actually by doing this, the command java class -add 5 4 will no longer print anything when it should print 9.
@Jerry huh? Note the opening brace after the if and the close brace after the return. Have you got those in?
I did not nice catch.
2

It prints Argument type mismatch, but you do not end your program. You can add a return statement as @Bohemian suggested or you can use a if structure like below. I personally don't like return statement in a middle of a function. Choose a solution that fits you :)

Try something like this:

private static void add(String[] args) 
{
    if (args.length <= 1) // Validate number of arguments
    {
        System.out.println("Argument count mismatch");
    }
    else // Number of arguments valid
    {
        // Validate that we received only numbers
        boolean allIntegerValid = true;

        for (int j = 1; j < args.length; j++) 
        {
            allIntegerValid = allIntegerValid && isInteger(args[j]);
        }

        if (allIntegerValid) // We received only numbers
        {
            int result = 0;
            for (int i = 1; i < args.length; i++) 
            {
                result += Integer.parseInt(args[i]);
            }
            System.out.println(result);
        }
        else // At least one argument was not a number
        {
            System.out.println("Argument type mismatch");
        }
    } 
}

1 Comment

I +1 you, but it would be nice if you do not give the code answer away :)

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.