In Java write a program that processes command line argument
In Java, write a program that processes command line arguments. The arguments are a mixture of numbers (ints and doubles). Run the program three times with a) b) c) as command-line arguments. Print each number on a separate line and whether it is an int or a double.
Test your program with each of the following lists of arguments:
a) 1 2 3 4 5
b) 1.1 2.2 3.3 4.4
c) 1 2.9 3 4.9 5 6.9
Solution
CommandLineTest.java
import java.util.regex.Pattern;
public class CommandLineTest {
public static void main(String[] args) {
String decimalPattern = \"([0-9]*)\\\\.([0-9]*)\";
String integerPattern = \"[0-9]\";
for(int i=0; i<args.length; i++){
boolean doubleMatch = Pattern.matches(decimalPattern, args[i]);
boolean intMatch = Pattern.matches(integerPattern, args[i]);
System.out.print(args[i]+\" \");
if(intMatch){
System.out.print(\"Integer\");
}
else if(doubleMatch){
System.out.print(\"Double\");
}
System.out.println();
}
}
}
Output:
1 Integer
2.9 Double
3 Integer
4.9 Double
5 Integer
6.9 Double
1.1 Double
2.2 Double
3.3 Double
4.4 Double
1 Integer
2 Integer
3 Integer
4 Integer
5 Integer
