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.
arg[0]in your code and running without params?Cargs ^^ This had no connection to the problem anyway, as the loop isn't even entered whenargs.length == 0.