-4

I am new to Java (or for simplicity sake for any programming language) and this is what I wrote;

3 public class Sample {
4   public static void main(String args[]){
5   int month = Integer.parseInt(args[0]);
6       if (month == 12 || month == 1 || month==2){
7       System.out.println("The season is Winter");
8       }
9           elseif (month==3||month==4||month==5);
10          {
11          System.out.println("The season is Spring");
12          }
13          elseif (month==6||month==7||month==8);
14          {
15          System.out.println("The season is Summer");
16          }
17          elseif (month==9||month==10||month==11);
18          {
19          System.out.println("The season is Autmn");
20          }
21      else{
22      System.out.println("Wrong input entered");
23      }
24
25  }
26 }

This code results in following error; Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Sample.main(Sample.java:5)

can someone help what error i am making here causing it to throw the error?

Shiv Kumar
  • 826
  • 1
  • 9
  • 19
  • 3
    Most likely `args` has no elements, thus has a `length` of `0`, so attempting to access any element will result in an index out of bounds error. Try using an `if` statement to determine if `args` has the expected number of elements (`if (args.length == 1) {...}`) – MadProgrammer Jan 18 '18 at 03:52
  • How are you running the code? If from the console you should pass an argument when you call the method, `$ java sample 1` – Raul Sauco Jan 18 '18 at 03:54
  • 2
    Since this code depends largely on how you're calling it, the information about what command you're running to produce this error is important! Please add that additional context so we can help more effectively! – Dane Hillard Jan 18 '18 at 04:00
  • You need to provide the command line arguments while running the program. If you are using the cmd or terminal, then use `java Sample 10` and then, `args[0]` will be `10`. If you are using an IDE, then provide the command line arguments in the properties of the project. – Sai Kishore Jan 18 '18 at 05:22

5 Answers5

1

Try running the application with arguments. If you are running it from the terminal you can do this by java MyApp 4 where MyApp is your app name and 4 is your argument.

If you are using an IDE, look into run configurations.

If instead, you want to take input from the user once the application is running, check out this question on stack overflow.

And don't give up. You are going to love Java once to get a grasp on the basics.

Rahel
  • 417
  • 6
  • 16
0

You are supposed to pass an argument when you try to execute this code.

if you are executing this code via the command line, try something like this:

java YourClassName 25

If you are using an IDE like Eclipse or IntelliJ or NetBeans refer to the following link:

Adding args on Eclipse

Adding args on IntelliJ

If no arguments are passed the length of the args array is 0 and you are trying to access the first element when you say args[0] which doesn't exist. That is why it throws an exception.

Aditya Vikas Devarapalli
  • 2,359
  • 1
  • 28
  • 46
0

You're probably not passing the month as a program argument. Once you do it should work.

If running it on command line, you need to provide it after the class name in the command. IDEs have UI screens for run configuration which accept program arguments.

Pavan Kumar
  • 3,041
  • 22
  • 39
0

Firstly, you initialize the array wrongly, this is one of the ways to initialize an integer array of 12 size.

int[] month = new int[12];

Then the error you are getting is due to out of bound, meaning the program could not find the next integer in your wrongly initialized variable month

Also the month arrays index starts with 0 and ends at 11 for a size of 12

Meaning it could store 12 stuff numbered from 0 to 11. Hope it helps you to understand arrays more. :D

Edit: ensure that when you run the program you have passed in a number. Otherwise u can do a if statement to check if args is empty

if(args.length!= 0){
//do this
month = Integer.parseInt(args[0]);
//...
}else{
      System.out.println("Wrong input entered");
}
bingcheng45
  • 258
  • 4
  • 12
  • He's not trying to initialize an array, just get the first argument of main – Raul Sauco Jan 18 '18 at 03:59
  • It is not allowing me to even compile the program. See the error that i have mentioned...that is what i am getting during compiling. Also that same error is appearing on when i try to save and run on the NetBeans. – Udai Pratap Singh Jan 18 '18 at 05:10
  • @UdaiPratapSingh if you are running on netbeans and compiling, i would think that the issue is with this line : int month = Integer.parseInt(args[0]); try changing it to int[] month = new int[12]; and see if it works – bingcheng45 Jan 18 '18 at 05:12
  • This is not helping me either. After adding your code, this is what i see in the tool tip message at the respective line where "if" "elseif" statements are starting; bad operand types for binary operator'==' first type: int[] second type: int bad operand types for binary operator'==' first type: int[] second type: int bad operand types for binary operator'==' first type: int[] second type: int – Udai Pratap Singh Jan 18 '18 at 05:19
  • @UdaiPratapSingh erm since u only need one data, [0] is needed to retrieve the first field. Try this instead. int[] month2 = new int[12]; int month = month2[0]; – bingcheng45 Jan 18 '18 at 05:22
  • the main point is to ensure ur code works, before trying to retireve data from args array – bingcheng45 Jan 18 '18 at 05:22
0

If you are trying to take user input for the month, you need to use a scanner. If that is the case, you need to import java.util.Scanner; to import it and then declare a scanner. To do so, type Scanner in = new Scanner(System.in). The "System.in" is your input, in this case the keyboard. Then you can do int month = in.nextInt(); to grab the next integer entered on the keyboard.

If I am incorrect on what you are trying to do send me a comment and I will edit my post and try and further help you. Cheers!

Evan Henry
  • 13
  • 5
  • I think i missed to provide that small piece of information.. The issue is not yet by running the code, instead i am not able to compile this program itself. Also when i try it on NetBeans there too it is giving the error. – Udai Pratap Singh Jan 18 '18 at 05:07
  • @UdaiPratapSingh The reason you cannot compile the program is because of the array out of bounds exception given when you try and use args[0]. When you compile a program in an IDE, you cannot specify arguments. I am giving you an alternative to get user input for getting the month. If you want to use args[0], you need to compile your code from the command line and specify an argument. – Evan Henry Jan 18 '18 at 17:52