Home > Mobile >  Can someone help me with this exception error
Can someone help me with this exception error

Time:01-28

I am really new at java. I wrote down this code. Problem statement is: Write a program HelloGoodbye.java that takes two names as command-line arguments and prints hello and goodbye messages

Example: Input: kevin harry
Output: Hello kevin and harry Goodbye harry and kevin

I am geting this error:

Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException:Index 0 out of bounds for length 0 at HelloGoodbye.main(HelloGoodbye.java:6)

I don't know why I am getting this error.

this is my code:

import java.util.Scanner;
public class HelloGoodbye {
   public static void main(String[] args) {
      Scanner scanner=new Scanner(System.in);
      for(int i=0;i<2;i  ) {
          args[i]=scanner.next();
      }
     
     System.out.println("Hello"  args[0]  " and "  args[1] "." );
     System.out.println("Goodbye"  args[1]  " and "  args[0] "." );
     
}
}

CodePudding user response:

Please read about how to run a program with command line arguments: https://www.digitalocean.com/community/tutorials/command-line-arguments-in-java

Eclipse command line arguments

I am also sure that in the main() method you will not need to use Scanner(), because in your case, it will only overwrite the values in the args[] array

  • Related