Results 1 to 16 of 16
Thread: java arguments
- 04-04-2011, 03:20 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
java arguments
i) The program below sums up 2 integers passed as command line arguments.
(a) Write down the values of args.length, args[0], and args[1] when theJava 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); } }
program is run with
(b) Add statements to check whether the user has actually entered 2 parameters.Java Code:C:\>java SumIntegers 5 7
ii) Write a program that sums up all the integers passed as command line arguments asJava Code:If not, issue the warning message as: Usage : java SumIntegers <num1> <num2>
and displays the following result:Java Code:C:\>java SumIntegers 5 7 12 32 555
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
- 04-04-2011, 03:40 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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?
- 04-04-2011, 03:49 PM #3
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.
- 04-05-2011, 03:43 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
I have tried your method, but it still has a problem belowJava 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); } }
am I misunderstanding your meaning?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)
- 04-05-2011, 05:12 AM #5
Member
- Join Date
- Apr 2011
- Location
- Athens, Greece
- Posts
- 52
- Rep Power
- 0
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...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); } }
- 04-05-2011, 08:58 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Nicely spoonfed there...
- 04-05-2011, 10:48 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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.
- 04-05-2011, 11:22 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-05-2011, 12:58 PM #9
Member
- Join Date
- Apr 2011
- Location
- Athens, Greece
- Posts
- 52
- Rep Power
- 0
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.
- 04-05-2011, 01:43 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
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:
how to separate the name, country, distance, fare?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");
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); }how to use the stops array object?public Station(String name,String country) {
this.name=name;
this.country=country;
}Last edited by bantony02; 04-05-2011 at 02:11 PM.
- 04-05-2011, 01:48 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-05-2011, 01:56 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
- 04-05-2011, 01:58 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Use split() again?
Try and and see.
- 04-05-2011, 02:23 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
- 04-05-2011, 02:28 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
So, what code have you tried?
We're not just going to give you the code...(santeron above excepted).
- 04-05-2011, 03:03 PM #16
Member
- Join Date
- Apr 2011
- Location
- Athens, Greece
- Posts
- 52
- Rep Power
- 0
Similar Threads
-
parsing command line arguments in java
By farahm in forum Advanced JavaReplies: 7Last Post: 11-21-2010, 03:36 PM -
Sorting objects with 2 strings arguments and 2 int arguments
By tirwit in forum New To JavaReplies: 8Last Post: 09-23-2010, 12:07 AM -
java command line arguments
By Vipul03 in forum Advanced JavaReplies: 10Last Post: 02-25-2010, 10:12 AM -
java command line arguments
By Vipul03 in forum New To JavaReplies: 1Last Post: 02-22-2010, 02:00 PM -
Java Command Line Arguments In Eclipse IDE
By JavaForums in forum EclipseReplies: 0Last Post: 05-19-2007, 09:45 AM


LinkBack URL
About LinkBacks


Bookmarks