Results 1 to 16 of 16

Thread: java arguments

  1. #1
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default java arguments

    i) The program below sums up 2 integers passed as command line arguments.
    Java Code:
    public class SumIntegers {
       public static void main( String args[] ) {
    		      int total=0;
          total = Integer.parseInt(args[0]) + Integer.parseInt(args[1]) ;
          System.out.println("The sum is " + total);
       }
    }
    (a) Write down the values of args.length, args[0], and args[1] when the
    program is run with
    Java Code:
    C:\>java  SumIntegers  5  7
    (b) Add statements to check whether the user has actually entered 2 parameters.
    Java Code:
    	If not, issue the warning message as:
    Usage : java SumIntegers <num1> <num2>
    ii) Write a program that sums up all the integers passed as command line arguments as
    Java Code:
    C:\>java  SumIntegers  5 7 12  32  555
    and displays the following result:
    The sum is 611

    iii) Write a program that sums up all the integers passed as ONE command line argument as
    C:\>java SumIntegers "5 7 12 32 555"
    and displays the following result:
    The sum is 611


    please help me to finish it, i have tried a lot of ways in (iii), i have no idea now please help me to write the program of each questions.

    thanks for helping

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by bantony02 View Post
    please help me to finish it, i have tried a lot of ways in (iii), i have no idea now please help me to write the program of each questions.
    If you write the arguments in double quote like the example said "5 7 12 32 555" then it is passed as one agument, so you have to split this string
    args[0].split(" ");. Then you will get a new array with 5 strings and you have to parse them into an integer like the example in i) (maybe its better to use a loop here)

    At the other assignments: What question do you have? What did you not understand?

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    try with args.length that gives you the number of passed arguments and then write a loop with sum += Integer.parseInt and so on. make sure to put your parseInt between a try-catch-block in case of a NumberFormatException.

  4. #4
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    If you write the arguments in double quote like the example said "5 7 12 32 555" then it is passed as one agument, so you have to split this string
    args[0].split(" ");. Then you will get a new array with 5 strings and you have to parse them into an integer like the example in i) (maybe its better to use a loop here)

    At the other assignments: What question do you have? What did you not understand?
    Java Code:
    public class SumIntegers {
       public static void main( String args[] ) {
    		int total=0;
    		String[] array = new String[args.length];
    		for (int i=0;i<args.length;i++){
    		array[i] += args[0].split(" ");
    		total += Integer.parseInt(array[i]);
    		}
    		System.out.println("The sum is " + total);
       }
    }
    I have tried your method, but it still has a problem below
    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "null[Ljava.lang.String;@c17164"
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at SumIntegers.main(SumIntegers.java:7)
    am I misunderstanding your meaning?

  5. #5
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Java Code:
    public class SumIntegers {
       public static void main( String args[] ) {
    		int total = 0;
    		String[] array = args[0].split(" ");
    		for (int i=0;i<array.length;i++){
    		total += Integer.parseInt(array[i]);
    		}
    		System.out.println("The sum is " + total);
       }
    }
    The code you have written is wrong mate. Use the above code. The only argument you get is arg[0] which is a String like "1 2 3". You split up that String to a String[] array using String[] array = args[0].split(" "); Then you loop through the array and add all its contents...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Nicely spoonfed there...

  7. #7
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by santeron View Post
    Java Code:
    public class SumIntegers {
       public static void main( String args[] ) {
    		int total = 0;
    		String[] array = args[0].split(" ");
    		for (int i=0;i<array.length;i++){
    		total += Integer.parseInt(array[i]);
    		}
    		System.out.println("The sum is " + total);
       }
    }
    The code you have written is wrong mate. Use the above code. The only argument you get is arg[0] which is a String like "1 2 3". You split up that String to a String[] array using String[] array = args[0].split(" "); Then you loop through the array and add all its contents...
    thank you!!
    but i have some question, i wonder if the array save a String but here why the array can initialize to args[0].split(" ") straightly and how the args[0].split works?
    args[0].split works like that when there is a space,the "1 2 3" will be apart the "1" "2" "3"?
    Last edited by bantony02; 04-05-2011 at 11:07 AM.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im not quite sure what you are trying to ask, your question is unclear.

    the String.split method returns an array of strings. When you pass an argument into command line which has " around it, it is counted as one single argument. So "1 2 3 4" would be stored in args[0]. When you call split on args[0] it splits it based on whatever the argument is(in this case, a space), and the items are stored in the String array.

    So "1 2 3 4" becomes an array where array[0] = 1, array[1] = 2, array[2] = 3, array[3] = 4.

  9. #9
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by bantony02 View Post
    thank you!!
    but i have some question, i wonder if the array save a String but here why the array can initialize to args[0].split(" ") straightly and how the args[0].split works?
    args[0].split works like that when there is a space,the "1 2 3" will be apart the "1" "2" "3"?
    If you write String[] you create an array of String objects, if you write int[] you create an array of ints and so on. An array is just a "collection" of same type items that you can access by their index (a[1], a[2] etc.). You can assign the values directly to the String[] array from any method that returns a string array. It's the same as writing String[] a = {"1", "2", "3"}. It does all that needs to be done to the right side of the equal and then just assigns it to whatever is on the left side. If you want to know what split or anything else in java is I would encourage to use the documentation of Java. But since I'm helping, if you have a String object (all Strings are objects that's why the capital letter) you can use it's methods like length() or split(). So by calling the split method on your String and providing the char which will use (in this case a space) to split the String, you get the String[] array.

  10. #10
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default question of constructors and new a object!

    thank you! i got it!
    I have some question of constructors and new a object!

    private String name;
    private Station[] stops;
    private int[] distance;
    Java Code:
    Line()	
    a constructor of the class Line accepts three string-typed arguments, namely lineName, stations and fares.
    The stations are separated by a space character. Each station also has the distance information which calculated from the first stop of the Line.  The station name and the distance information separated by a colon “:”.
    The fares are separated by a colon “:” which represent passengerFarePerKm, cargoFarePerKm, crossBorderSurcharge and reservationSurcharge.

    TestProgram:
    Java Code:
    Line line = new Line("Paris-Zurich-Munich", "Paris:France:0 Lyon:France:450 Geneve:Switzerland:600 Zurich:Switzerland:870 St.Gallen:Switzerland:950 Memmingen:Germany:1070 Munich:Germany:1180", "0.06:0.01:30:10");
    how to separate the name, country, distance, fare?

    Java Code:
    	public Line(String lineName,String stations,String fares) {
    		this.name = lineName;
    		for (int i=0;i<stops.length;i++)
    		stops[i] = new Station(stations, stations);
    	}
    public Station(String name,String country) {
    this.name=name;
    this.country=country;
    }
    how to use the stops array object?
    Last edited by bantony02; 04-05-2011 at 02:11 PM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    How do you want to use it?
    Or, put another way, that's not a very meaningful question.

    That constructor is populating the stops array with lots of Station objects that all have the same stations String passed into it.

    Oh, and this probably ought to be a different thread, as it's a completely unrelated question.

  12. #12
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    How do you want to use it?
    Or, put another way, that's not a very meaningful question.

    That constructor is populating the stops array with lots of Station objects that all have the same stations String passed into it.

    Oh, and this probably ought to be a different thread, as it's a completely unrelated question.
    actually I want to know how to separate the name, country, distance, and fare.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Use split() again?
    Try and and see.

  14. #14
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Use split() again?
    Try and and see.
    it still can't!
    the values is already fixed.
    what i can do is to separate the name, country, distance, fare in once and put them to the Station constructor.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    So, what code have you tried?

    We're not just going to give you the code...(santeron above excepted).

  16. #16
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Well actually just corrected his :P
    Mate there is no point on giving you the answers. Check java tutorials or get/dl a book telling you the basics. I recommend O'Reilly Head First Java. Fun book and really easy to follow and gets your head straight on some matters.

Similar Threads

  1. parsing command line arguments in java
    By farahm in forum Advanced Java
    Replies: 7
    Last Post: 11-21-2010, 03:36 PM
  2. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  3. java command line arguments
    By Vipul03 in forum Advanced Java
    Replies: 10
    Last Post: 02-25-2010, 10:12 AM
  4. java command line arguments
    By Vipul03 in forum New To Java
    Replies: 1
    Last Post: 02-22-2010, 02:00 PM
  5. Java Command Line Arguments In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-19-2007, 09:45 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
  •