Results 1 to 15 of 15
Thread: Choking on malformed args?
- 07-19-2011, 06:30 PM #1
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
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:
Java 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;
- 07-19-2011, 06:56 PM #2
This is an especially hard String for the parseInt method to convert to an int.NumberFormatException: For input string: "ThisIsSomeStringToSend"
Could you give it something easier like "123"?
If you are having problems figuring out where your args are, print them out:
System.out.println("args=" + java.util.Arrays.toString(args)); // show the args arrayLast edited by Norm; 07-19-2011 at 06:58 PM.
- 07-19-2011, 07:06 PM #3
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-19-2011, 07:08 PM #4
Remember arrays are 0 based, [2] is the third elementJava Code:Integer.parseInt(args[2])
- 07-19-2011, 08:29 PM #5
A problem is that the OP did not post the command line he used to get the error. A copy of ALL of the console showing the command line and the error message would be useful.
- 07-19-2011, 08:31 PM #6
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-19-2011, 08:32 PM #7
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-19-2011, 08:35 PM #8
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-19-2011, 08:35 PM #9
The full text from the console with the commandline vs an edited or reconstructed (something like this..) is the best.
Too often there is a slip. A copy of the console is more reliable.
also the println of the args would help.
- 07-19-2011, 08:43 PM #10
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
OK...I added the debug line:
System.out.println("args=" + java.util.Arrays.toString(args)); // show the args array
...and now the err msgs no longer appear. The console now says:
TCPEchoServer [Java Application] C:\Program Files\Java\jre6\bin\javaw.exe (<datetime>)
Handling client at 127.0.0.1 on port 4447
...yet I don't see the result of the "debug line" added above.... curiouser and curiouser....
- 07-19-2011, 08:47 PM #11
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
However, if I first start my minimalistic Server, and then the Client, the Console barks:
"Exception in thread "main" java.lang.IllegalArgumentException: Parameter(s): <Server> [<Port>]
at TCPEchoClientGUI.main(TCPEchoClientGUI.java:16)"
I'm assuming the "16" above is supposed to be referring to a source code line, yet line 16 is commented out...
- 07-19-2011, 08:49 PM #12
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-19-2011, 08:53 PM #13
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
So now, after adding that "debug line" to the appropriate class, the Console reads:
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)
args=[127.0.0.1, ThisIsSomeStringToSend]Last edited by blackbird; 07-19-2011 at 08:56 PM. Reason: i goofed up
- 07-19-2011, 08:59 PM #14
It doesn't "add up". Post #13 shows 2 args: 0 and 1
The code in post #1 uses 2: Integer.parseInt(args[2])
I assume that line with the parseInt() shown above is/was line 19.
Time to start over with a clean program and commandline.
What is happening now?
- 07-19-2011, 09:14 PM #15
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
It's actually running now, although I've now got to get it actually sending something to the server for it to echo back to me.
The problem was I had two similar projects, namely TCPEchoClient and TCPEchoClientGUI, and although I THOUGHT I was looking at the latter's code, I was looking at the former's. One expects either 1 or 2 args, the other expects 2 or 3 args. I guess I have to be more alert, but I wish moving to a project was more obvious/dramatic - it's weird when one project runs, but the other one's source is being displayed in the editor.
Similar Threads
-
Malformed expression: "(ERROR)"<
By Ashish_jain in forum New To JavaReplies: 8Last Post: 07-13-2011, 10:10 AM -
use of String[] args
By mallikanala in forum New To JavaReplies: 1Last Post: 06-21-2011, 08:09 AM -
PDFBox certificate malformed
By wanderer2019 in forum Advanced JavaReplies: 0Last Post: 06-09-2010, 10:29 AM -
Args[0]
By hakan123 in forum New To JavaReplies: 11Last Post: 12-03-2009, 05:40 PM -
problem in args
By MS_Dark in forum New To JavaReplies: 13Last Post: 08-27-2008, 01:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks