Results 1 to 15 of 15
  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default 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;

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    NumberFormatException: For input string: "ThisIsSomeStringToSend"
    This is an especially hard String for the parseInt method to convert to an int.
    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 array
    Last edited by Norm; 07-19-2011 at 06:58 PM.

  3. #3
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    This is an especially hard String for the parseInt method to convert to an int.
    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 array
    OK, I was thinking that second arg was the initial msg to send.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Java Code:
    Integer.parseInt(args[2])
    Remember arrays are 0 based, [2] is the third element

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  6. #6
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.
    Yes, I did:
    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

  7. #7
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by blackbird View Post
    Yes, I did:
    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
    and then, after adding those args, I ran the app via Ctrl+11 in Eclipse (or Run As | Java Appliation)

  8. #8
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Java Code:
    Integer.parseInt(args[2])
    Remember arrays are 0 based, [2] is the third element
    Yes, but according to the code, arg0 is the servername ("127.0.0.1"), arg1 is the data to send "ThisIsSomeStringToSend" and arg2 is optional. If nonexistent, 7 is used.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  10. #10
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.
    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....

  11. #11
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by blackbird View Post
    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....
    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...

  12. #12
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by blackbird View Post
    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...
    Dang it - my bad: I'm still not used to it that you can be working on one project, and yet it's source is not displayed in the window, but rather it could be the source from another project!

  13. #13
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by blackbird View Post
    Dang it - my bad: I'm still not used to it that you can be working on one project, and yet it's source is not displayed in the window, but rather it could be the source from another project!
    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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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?

  15. #15
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?
    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

  1. Malformed expression: "(ERROR)"<
    By Ashish_jain in forum New To Java
    Replies: 8
    Last Post: 07-13-2011, 10:10 AM
  2. use of String[] args
    By mallikanala in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 08:09 AM
  3. PDFBox certificate malformed
    By wanderer2019 in forum Advanced Java
    Replies: 0
    Last Post: 06-09-2010, 10:29 AM
  4. Args[0]
    By hakan123 in forum New To Java
    Replies: 11
    Last Post: 12-03-2009, 05:40 PM
  5. problem in args
    By MS_Dark in forum New To Java
    Replies: 13
    Last Post: 08-27-2008, 01:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •