0

The code is as below:

public class examplecode {
    public static void main(String[] args) {
        int p1 = Integer.parseInt(args[0]);
        int p2 = Integer.parseInt(args[1]);
        int p3 = (p1^2)*(p2^2);
        System.out.println(p3);
    }
}

However, when I run the code, I get the error said

"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2\lib\idea_rt.jar=60813:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Desktop\Sample1\out\production\Samplefolder examplecode
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at examplecode.main(examplecode.java:4)

Process finished with exit code 1

Why I get such error?

I searched some resources said, I need to write down the following code in the terminal (10 and 20 are just randomly generated example inputs)

$ java examplecode 10 20 

However, my terminal still shows error and did not give me any result

PS C:\Users\Desktop\Samplefolder> java examplecode 10 20
Error: Could not find or load main class examplecode 
Caused by: java.lang.ClassNotFoundException: examplecode

I am thinking that as args[0] and args[1] indicates the first and second elements in the string, shall I create a string inside main function? Or where should I set that string?

Scarlett
  • 1
  • 2
  • 1
    You either need to set the runtime arguments in your IntelliJ run configuration, or compile your class first with javac in terminal. The two problems are independent, in the first one you do not have the program arguments, in the second one you do not have the compiled class. – Koray Tugay Aug 13 '21 at 18:13
  • See this answer for running your program with the arguments in IntelliJ: https://stackoverflow.com/a/54693742 or https://stackoverflow.com/a/36678408 for compiling your file first in terminal. – Koray Tugay Aug 13 '21 at 18:15
  • @KorayTugay Thank you for helping! I am a beginner in Java and do not quite understand. Would you mind a bit detailed if you get time? That would be huge help! – Scarlett Aug 13 '21 at 18:16
  • @KorayTugay Can I understand as the program 'examplecode' did nothing wrong. And the way to run this program 'examplecode' is also correct. (Write $ Java 10 20 in terminal). But I just need to do some setting stuff in IntelliJ ? Thank you again! – Scarlett Aug 13 '21 at 18:19
  • Try to get your program running in IntelliJ by modifying the Run Configuration you have. Google "how to pass arguments to a java program in IntelliJ" or something similar. I would also take a look at https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html if I were you. – Koray Tugay Aug 13 '21 at 18:20
  • Sorry, I do not understand your question. The program looks ok. It just needs some parameters passed into it when it is run. – Koray Tugay Aug 13 '21 at 18:20
  • @KorayTugay Thank you for the link! I just looked at that least one. Did that means: The way we pass parameter into program 'examplecode' is by writing command line in IntelliJ's terminal section? And in order to be able to write '$ java ...', I need to modifying the Run Configuration with the first two links you provided? (I am just very confused about the Causal relationship, which behaviour leds which results and to be able to achieve that what should I do ) – Scarlett Aug 13 '21 at 18:31
  • In terminal, try `javac example.java` AND then try `java examplecode 10 20`. Hopefully it will work. And then try to modify the run configuration in IntelliJ user interface and run your program. – Koray Tugay Aug 13 '21 at 18:36

0 Answers0