Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class SwitchDemo
{
public static void main(String args[])
{
int marks = Integer.parseInt(args[6]);
switch(marks/6)
{
case 1:
case 2:
case 3:
System.out.println("Excelent");
break;

case 4:
System.out.println("Good");
break;

case 5:
System.out.println("Work Hard");
break;

case 6:
System.out.println("Poor");

default :
System.out.println("Invalid value Entered");
}
}
}

What I have tried:

This is he error?




Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at SwitchDemo.main(SwitchDemo.java:5)
Posted
Updated 12-Feb-17 2:24am
v2
Comments
[no name] 12-Feb-17 8:15am    
Obviously you are trying to access an element of an array that doesn't exist. What you should do is stop trying to access an element of an array that doesn't exist and your error will go away.

Java
int marks = Integer.parseInt(args[6]);

Are you sure that there is a value at args[6]? You should use the args.length property first, to check how many values are present.
 
Share this answer
 
The error is pretty explicit:
ArrayIndexOutOfBoundsException: 6 at SwitchDemo.main(SwitchDemo.java:5)
So look at line 5 and you see:
Java
int marks = Integer.parseInt(args[6]);
Did you provide any arguments when you ran the application?
Did you provide seven arguments (remember, java array indexes start at zero, not one, so the sixth argument is at index 5)?

I'd strongly suggest you check the number of arguments passed, and report a problem to the user if he didn't provide enough when he runs your app:
Java
int argCount = args.length;
Will tell you how many were passed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900