Results 1 to 10 of 10
  1. #1
    elitis is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Understanding an error

    Anyway this is my code

    Java Code:
    public class EG1 {
    public static void main(String args[])
    {
    String s = args[0];
    System.out.println("Hello "+s);
    int x = 40;
    System.out.println(x);
    System.out.println(x+x);
    System.out.println(s+" "+x+x);
    }
    }
    I know it has an error, but I need help understanding the error, if I understand the error then I can fix it in future, I want to know what causes the error in general and how I can fix it.

    Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Understanding an error

    Would you like to post the full text of the error message, or is this a guessing game?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Understanding an error

    If you supply an argument value '2*2*2*5*101=' the error would be less obvious ...

    kind regards,

    Jos ;-)
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    elitis is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Re: Understanding an error

    So im assuming

    String s = args[0];

    The error is accused by putting the value as 0?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Understanding an error

    The problem -may- be that args[0] does not exist - it all depends on how the program is executed. If you have a class Test and you execute it like


    java Test


    args[0] (as in: index 0 in the array) will not exist - there are no program arguments so args will be a completely empty array with length 0. If you run it like this:


    java Test elitis


    then args[0] will exist and contain "elitis" and args.length will be 1.

    If you write a program that accepts arguments, then assume your program is going to be run without any parameters - add defensive checks to catch that situation.

    Java Code:
    public static void main(String[] args){
    
      if(args.length < 1){
        System.out.println("You forgot to pass a name.");
        System.exit(1);
      }
    
      String name = args[0];
      ...
    
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding an error

    My guess is that the following:

    Java Code:
    System.out.println(s+" "+x+x);
    Is printing out s, followed by a space, followed by 4040. Try
    putting x + x in parentheses.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Understanding an error

    Quote Originally Posted by jim829 View Post
    My guess is that the following:

    Java Code:
    System.out.println(s+" "+x+x);
    Is printing out s, followed by a space, followed by 4040. Try
    putting x + x in parentheses.

    Regards,
    Jim
    Or pass an argument value as I showed in my previous reply (don't forget to quote the entire thing to protect it against globbing ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Understanding an error

    Quote Originally Posted by elitis View Post
    So im assuming

    String s = args[0];

    The error is accused by putting the value as 0?
    Did you miss Darryl's post?
    You still haven't told us what the problem is.
    Does it compile?
    Does it run without exceptions?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Understanding an error

    Quote Originally Posted by JosAH View Post
    Or pass an argument value as I showed in my previous reply (don't forget to quote the entire thing to protect it against globbing ;-)
    Ah! I didn't connect the two.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Understanding an error

    Quote Originally Posted by jim829 View Post
    Ah! I didn't connect the two.
    I know; only I seem to think that it is funny; it must be my nerdy dna ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 5
    Last Post: 07-08-2014, 03:07 PM
  2. Not understanding GridLayout
    By grapeShot in forum New To Java
    Replies: 8
    Last Post: 03-14-2014, 11:27 AM
  3. Replies: 6
    Last Post: 12-30-2012, 07:39 PM
  4. Replies: 5
    Last Post: 04-04-2012, 06:40 AM
  5. Need some understanding and help!
    By Kevinius in forum New To Java
    Replies: 8
    Last Post: 05-14-2011, 06:50 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
  •