Reputation: 33
Hi I am trying to learn about arrays and our teacher gave us an extra assignment which says: "Create an array with every month of the year(January, February...). My code looks like this:
package array2;
public class Array2 {
static String months[];
public static void main(String[] args) {
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";
int m = Integer.parseInt( args[0] );
System.out.println( months[ m ] );
}
}
But I am getting the error
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at array2.Array2.main(Array2.java:33)
/Users/Mo/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Can someone help clarify this?
Upvotes: 1
Views: 11874
Reputation: 339332
As the other Answers state, you likely are failing to pass an argument when running this problem.
If using an IDE such as NetBeans, IntelliJ, or Eclipse, you must dig around to find where that tool lets you specify arguments to be passed to the main
method.
For example, in NetBeans:
Run
. Arguments
field, type a month number such as 7
. OK
to save the change.For debugging, you should dump the array of arguments to the console to verify. And you should be testing for expected number of arguments. Here is example code doing both those tasks.
public class App {
public static String months[];
public static void main ( String[] args ) {
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 ( "DEBUG args: " + Arrays.toString ( args ) );
if ( args.length == 0 ) {
System.out.println ( "No month specified. No arguments passed to 'main' method." );
} else if ( args.length == 1 ) { // Else we have a single argument as expected.
int m = Integer.parseInt ( args[ 0 ] );
System.out.println ( months[ m ] );
} else if ( args.length > 1 ) { // Else we have multiple arguments, but expected only one.
System.out.println ( "ERROR - more than one argument passed to 'main' method." );
} else { // Else impossible. Should not reach this point. Defensive programming.
System.out.println ( "ERROR - Unexpectedly reached IF-ELSE. Should be impossible." );
}
…
DEBUG args: [8, 7, 9]
ERROR - more than one argument passed to 'main' method.
You can see this code run live at IdeOne.com. Unfortunately, that web app seems to have a bug: failing to pass the input arguments. So not useful at the moment for our purpose here, but I leave the link in case the bug is fixed in the future.
While I understand you are doing exercises to learn Java programming, you should be aware that you would not need to do this definition of months in real-world work. Instead you would use the Month
enum, part of the java.time classe. An enum is a grouping of static constants. Enums in Java are vastly more powerful, flexible, and useful than conventional enums.
Month month = Month.of ( 5 ); // Months are numbered 1-12 for January-December. So 5 = May.
String output = month.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH ); // Automatically localize the name of the month.
String output2 = Month.DECEMBER.getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH ); // Refer to a `Month` object by the name of a constant. Here: Month.DECEMBER
Dump to console.
System.out.println ( "month.toString(): " + month );
System.out.println ( "output: " + output );
System.out.println ( "output2: " + output2 );
month.toString(): MAY
output: mai
output2: décembre
Upvotes: 2
Reputation:
I see that you are using Netbeans
, so Go to your Run Configurations-> Arguments, in the Program arguments add for example "10 5 3 14", and then you need to avoid indexOutOfBoundExcepion
:
int m = Integer.parseInt( args[0] );
if(m>=0&&m<13)
System.out.println( months[ m ] );
Upvotes: 0
Reputation: 10184
The error is at three places:
In your program. You need to make sure that args
is not an empty array (make sure that the user has indeed provided at least 1 arg).
In the way your invoke your program. You are most likely not passing any arguments. If you are running your program from command line, you can do it the following way:
java array2.Array2 5
Should print "May".
Make sure the parsed int from the argument is within range.
if(args.length == 0) {
System.out.println("Error, must specify one argument");
}
else {
int m = Integer.parseInt( args[0] );
if(m<0 || m> 12) {
System.out.println("Invalid month specified");
}
else {
System.out.println( months[ m ] );
}
}
Upvotes: 2
Reputation: 18933
Make sure that you ran it with arguments.
If you are running from Eclipse:
1. Run Configuration
2. Arguments Tab
3. Enter the number in Program Arguments.
Upvotes: 0