'.class expected (Newbie on java) [duplicate]
I don't know why this cannot run, the error on "num = Integer.parseInt (args[]) ;"
class CommandLine {
public static void main (String args [])
{
int num ;
num = Integer.parseInt (args[]) ;
if (num>=100)
{
} else {
System.out.println("Number is less than 100");
}
}
}
Solution 1:[1]
In order to use args[], you need to pass a command-line argument when you run the class. For example, if you are running from command prompt, you will need to do something like this:
java CommandLine 100
In your code, you could do something like this
class CommandLine {
public static void main (String args[]) {
int num = Integer.parseInt (args[0]) ;
if (num>=100)
{
System.out.println("You entered ");
} else {
System.out.println("Number is less than 100");
}
}
}
Which will result in displaying You entered 100 on the console. If you are running from an IDE like Eclipse, you will need to set up the command-line argument through the "Run Configurations" menu. Then, you enter (space-separated) arguments in the "Program arguments" text area.
In IntelliJ, you do the same through the "Modify run configuration" menu
The argument array args[] in a class' main method is built by the JVM. It is a string of command-line arguments that are passed to the executed class. The JVM parses out the command line instruction and will gather any and all values after the class name and will create that args array. If you pass nothing to the program, then the array will be empty.
An command-line instruction like
java MyClass Hello World Welcome to Java
builds a String array with 5 values.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


