-1

I am getting error while taking input with Integer.parseInt(args[0]); error is in args section i know i can change it to scanner but i want to know this method.

Can anybody point out or show the solution to my problem?


class NegativeOutputException extends Exception{

    private final int ex;

     NegativeOutputException(int a){
        ex = a;
    }

    public String toString(){
        return "NegativeOutputException!("+ex+")";
    }

}


public class practice6_creating_custom_exception {

     public static void main(String args[]){

         int x = Integer.parseInt(args[0]);//Error Here argument at position one
         int y = Integer.parseInt(args[1]);//argument at position two
         //argument at position twenty one which doesn't exsist
         int a;

         try{
             a = x * y;
             if(a < 0)
                 throw new NegativeOutputException(a);
                 System.out.println("Output >>" + a);
         }

         catch(NegativeOutputException e){
             System.out.println("Caught >>" + e);
         }

     }

}

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

Process finished with exit code 1
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 3
    How are you running this? Are you passing it two arguments? – Federico klez Culloca Sep 09 '22 at 07:31
  • `java -cp . practice6_creating_custom_exception -6 7` or some IDE option. One in java normally writes `int[] args` which is more logical (type parts together). Your use was syntax for C/C++ programmers, where there is a weird syntax. One also would write `Practice6CreatingCustomException`. – Joop Eggen Sep 09 '22 at 07:39
  • 1
    @JoopEggen I found your comment about `int[] args` a bit confusing until I understood you were talking about the position of `[]` and not about the fact that `args` should have been declared as an array of `int`s. Might be better to avoid confusion by writing `String[] args`. – Federico klez Culloca Sep 09 '22 at 07:44
  • 1
    @FedericoklezCulloca you are right **`String[]`**. Just typing mindless will become `int`. – Joop Eggen Sep 09 '22 at 08:18

2 Answers2

0

It gives you a java. lang.ArrayIndexOutOfBoundsException because you are trying to access a position in an empty array.

parseInt() is not used for taking inputs in such a case you need to use Scanner

For your case, you need first take input (by using Scanner) and then assign it to an integer variable, directly parsing the argument array will not provide the input.

Scanner sc = new Scanner();
int x = sc.nextInt(); //Scans the next token of the input as an int

parseInt() function - The parseInt() method parses a value as a string and returns the first integer. Source

Scanner Class - Scanner object holds the address of InputStream object present in the System class. Input Stream object of system class reads data from the keyboard which is byte stream/byte form. The Scanner class converts this read byte into a specific data type. Source

The parseInt() function cannot read data from the input stream which is byte stream/byte form, hence you cannot directly parse the args[] array and assign it to an integer variable as it is empty since it is not yet scanned.

If you are looking for different ways of taking input in Java then here they are:

Using Buffer Reader Class, Using Scanner Class, Using Console Class, Using Command Line Argument, Source

Sprivro
  • 37
  • 9
0

Most probably you are simply not passing any arguments.

One way to pass arguments to the main method in Java is with the command to run the application in terminal. You can simply add the arguments after the java command to run the application separating them with a space. If you want the user to input the data, then you should use Scanner instead.

In your case, navigate to the folder where your java file sits and run the following:

java practice6_creating_custom_exception 0 1

In this example, 0 and 1 are the arguments you are passing. If you are using an IDE then this can usually be done in the run configurations.

Side note, you might need to compile the application before running it and the command for that is the following:

javac practice6_creating_custom_exception.java
Vahe Aslanyan
  • 76
  • 1
  • 4