Choking on malformed args?
I'm trying to get a basic TCPEcho example to work; the code is from a book about Java and TCP. The problem is my Program arguments. I've entered them two ways into the Arguments tab in Eclipse (by right-clicking on the Project and selecting Run As | Run Configurations | Arguments tab.
I've tried adding the args this way (space-separated):
127.0.0.1 ThisIsSomeStringToSend
...and this way (on separate lines):
127.0.0.1
ThisIsSomeStringToSend
...in the "Program Arguments" area, and in both cases I get "Exception in thread "main" java.lang.NumberFormatException: For input string: "ThisIsSomeStringToSend"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at TCPEchoClientGUI.main(TCPEchoClientGUI.java:19)"
Do the args need to be surrounded with quotes, or what's wrong here? The pertinent code is:
Code:
public static void main(String[] args) throws IOException {
if ((args.length < 2) || (args.length > 3)) // Test for correct # of args
throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]");
String server = args[0]; // Server name or IP address
// Convert input String to bytes using the default character encoding
byte[] byteBuffer = args[1].getBytes();
int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;