InputTest2.javaimport java.util.Scanner;
/**
* This program demonstrates command line argument input.
*/
public class InputTest2
{
public static void main(String[] args)
{
if (args.length<2) {
System.out.println("Correct Usage is InputTest2 name age");
System.exit(0);
}
// get first input
String name = args[0];
// get second input
int age = Integer.parseInt(args[1]);
// display output on console
System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
}
}
No arguments
C:\ece538\class3\InputTest>java InputTest2 Correct Usage is InputTest2 name age
Correct arguments
C:\ece538\class3\InputTest>java InputTest2 John 35 Hello, John. Next year, you'll be 36
Can not have spaces in argument:
C:\ece538\class3\InputTest>java InputTest2 John Loomis 42
Exception in thread "main" java.lang.NumberFormatException: For input string: "L
oomis"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at InputTest2.main(InputTest2.java:19)
unless you enclose it in quotes:
C:\ece538\class3\InputTest>java InputTest2 "John Loomis" 42 Hello, John Loomis. Next year, you'll be 43
Maintained by John Loomis, updated Tue May 20 21:56:20 2014