Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java won't load.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Java won't load.

    Why won't this work? after being compiled.
    public class Add {
     
       public static void main(String[] args) {
          int a = Integer.parseInt(args[0]), b = Integer.parseInt(args[1]);
          System.out.println(a + " + " + b + " = " + (a + b));
       }
     
    }

    Untitled.jpg
    Last edited by NewMember; February 18th, 2013 at 01:27 AM. Reason: indentation


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Java won't load.

    The error means String[] args is empty but you're trying to get values from it. Even if String[] args contained something that could be converted to an integer, you would still receive compiler errors because you cannot initialize variables on the same line using your approach. That is:

    public class Add {
       public static void main (String[] args) {
           int a, b, sum;
           int[] myArray = {1, 2};
           a = myArray[0];
           b = myArray[1];
           sum = a + b;
           System.out.println(a + " + " + b + " = " + sum);
         }
    }

  3. The Following User Says Thank You to SunshineInABag For This Useful Post:

    NewMember (February 18th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java won't load.

    The code should test if the args array has anything in it before trying to use it. Use the array's length attribute.
    If args.length is >= 2 then the code can get args[0] and args[1]
    If the length is < 2 print out an error message and exit.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    NewMember (February 18th, 2013)

  6. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java won't load.

    Alright thanks for the quick response, it seems to be working.

Similar Threads

  1. Won't load next class...
    By Huw in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 21st, 2012, 12:07 AM
  2. won't load png
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2012, 03:24 PM
  3. Settlers of Catan Java Applet won't load
    By ragalie in forum JDBC & Databases
    Replies: 1
    Last Post: March 1st, 2012, 05:48 AM
  4. How to up load java file when you post??
    By zhen1606 in forum Collections and Generics
    Replies: 1
    Last Post: February 23rd, 2012, 09:12 AM
  5. Java Applet Will Not Load.
    By PaulMoretti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 05:42 AM

Tags for this Thread