1

I need to handle empty input and remove the stack trace from output.

Here's a snippet of my code:

public class TestShape
{
    public static void main(String args[])
    {

        for(int i = 0; i < args.length; i++)
        {
            try
            {
                Integer.parseInt(args[i]);
            }

            catch(NumberFormatException nfe)
            {
                System.err.println("\n" + "NumberFormatException is caught" + "\n" + "Please input 1, 2 or 3 valid numbers only");
            }

            if (args.length==0 ||args.length>3||Integer.parseInt(args[i])<0) 
                throw new IllegalArgumentException("\n" + "IllegalArgumentException is caught" + "\n" + "Please input 1, 2 or 3 valid numbers only");
        }

I have two problems, first is when a user inputs no value on the command line, it's supposed to throw a IllegalArguementException and the appropriate error message. When I run this code and input no values it runs without throwing the exception. It looks something like this:

C:\Users\XXX\Desktop\Folder>java TestShape

C:\Users\XXX\Desktop\Folder>_

The second problem is when I try out the other exceptions (putting a string instead of an int, negative numbers, exceeding more than 3 numbers etc.) it outputs the proper message but it also prints out the stack trace:

C:\Users\Chef Boi Logro\Desktop\Folder\ICS 112\Lab Exercises\LE8\New>java TestShape -1 2 -3

Exception in thread "main" java.lang.IllegalArgumentException:

IllegalArgumentException is caught Please input 1, 2 or 3 valid numbers only at TestShape.main(TestShape.java:19)

What I need is for the terminal to just display the error messages, without the stack trace.

4
  • I believe the first argument of your program is the name of the program itself. Have you tried printing arg[0] in your code and running without params? Commented Jul 5, 2017 at 8:26
  • @Nathan check the accepted answer here: stackoverflow.com/questions/24339479/… args can have length of 0 if no arguments are entered Commented Jul 5, 2017 at 8:30
  • @ArvindSasikumar Indeed, I mistook with C args ^^ This had no connection to the problem anyway, as the loop isn't even entered when args.length == 0. Commented Jul 5, 2017 at 8:36
  • Yes, Eran's answer is right. Commented Jul 5, 2017 at 8:38

3 Answers 3

2

Your loop is not entered when no arguments are supplied, so no exception is thrown and nothing is displayed.

You should add a condition prior to the loop that throws an exception if args.length==0.

As for the stack trace being displayed, that's the default behavior for exception not handled by the main method. You'll have to catch all the exceptions you throw in order to avoid that.

Perhaps you should just replace

throw new IllegalArgumentException("\n" + "IllegalArgumentException is caught" + "\n" + "Please input 1, 2 or 3 valid numbers only");

with

System.err.println("\n" + "Please input 1, 2 or 3 valid numbers only");

since there's not much point in throwing exceptions from the main method (since there's no other method that can handle such exceptions).

Here's a suggested approach:

public static void main(String args[])
{
    if (args.length == 0 || args.length > 3) {
        System.err.println("Please input 1, 2 or 3 valid numbers only");
    } else {
        for(int i = 0; i < args.length; i++) {
            try {
                if (Integer.parseInt(args[i]) < 0) {
                    System.err.println("\n" + "Please input 1, 2 or 3 valid numbers only");
                }
            }
            catch(NumberFormatException nfe) {
                System.err.println("\n" + "NumberFormatException is caught" + "\n" + "Please input 1, 2 or 3 valid numbers only");
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

How do I go about catching these exceptions? Also the one within the try-catch method, the NumberFormatException also displays the stack trace
@salmonade Simply don't throw an exception. And the NumberFormatException displays the stack trace because it is thrown twice, since you call Integer.parseInt(args[i]) twice, and only catch it once.
0

For the first problem, you are checking condition in wrong place. Refer given code for correction. For the second problem, you are using default exception handling which shows stack trace. For custom error handling, use throws clause. Refer given code for same.

Updated Code:

if (args.length < 1)
{
    throw new IllegalArgumentException("\n" + "IllegalArgumentException is caught" + "\n" + "Please input 1, 2 or 3 valid numbers only");
}
else
{
    for(int i = 0; i < args.length; i++)
    {
        if (args[i].matches("[0-9]+") && args[i].length() <= 3)
            {
                Integer.parseInt(args[i]);
            }
            else
            {
                throw new NumberFormatException("NumberFormatException is caught" + "\n" + "Please input 1, 2 or 3 valid numbers only");
            }
    }
}

Hope this will help. :-)

2 Comments

The NumberFormatException still gives me a stack trace :/
Please mark it as answer if you find it useful. Thanks :-)
0

You can check received values before checking for avoiding exceptions because you have expected numbers for the task (from 0 to 9);

I prefer this approach:

    public static void main(String args[])
    {
      if(arargs.length < 1 || arargs.length > 3) //firstly check input size
      {
        System.out.println("Please input 1, 2 or 3 valid numbers only.");
        return;
      }
      List<String> allowedNumbers = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); //define allowed numbers (please move to constant in your class for avoid multimple initialization)
        for(int i = 0; i < args.length; i++)
        {
          try
          {
            if(allowedNumbers.contains(args[i]))
            {
              Integer.parseInt(args[i]); //now it's safety parse without exceptions
            } 
          }
          catch(NumberFormatException nfe)
          {
            System.err.println(nfe.toString());
          }
        }
      }

Hope it's useful info.

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.