Reputation: 31
public class Month {
static String months[];
public static void main(String[] args) {
// TODO code application logic here
months = new String[13];
months[0] = "null";
months[1] = "january";
months[2] = "february";
months[3] = "march";
months[4] = "april";
months[5] = "may";
months[6] = "june";
months[7] = "july";
months[8] = "august";
months[9] = "september";
months[10] = "october";
months[11] = "november";
months[12] = "december";
System.out.println("enter number:");
int a = Integer.parseInt(args[0]);
System.out.println(a);
}
}
I have to get userinput for the month number and output the month name. Can someone please tell me why I keep getting an error at
int a = Integer.parseInt(args[0]);
Here is the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Month.main(Month.java:22)
Thanks
Upvotes: 2
Views: 40
Reputation: 849
You ask the user to enter a number, but don't use it for anything. You pass the first argument of the main function to the parseInt call, this causes the error you get.
Upvotes: 0
Reputation: 23029
For user input, you want to use this :
System.out.println("enter number:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
Also remember, to add import java.util.Scanner;
to the top of your file, just below the package
line
Args are arguments, you have to run them with the program from command line or add it in Run section of your IDE as arguments.
Upvotes: 3