Sum
Task
-
You need to create a
Sumclass which will sum integers from command line arguments and output the sum to console. -
Examples:
java Sum 1 2 3Expected output:
6.java Sum 1 2 -3Expected output:
0.java Sum "1 2 3"Expected output:
6.java Sum "1 2" " 3"Expected output:
6.java Sum " "Expected output:
0. -
Arguments can be:
- digits,
- signes
+and-, - space symbols
-
You can assume that
inttype is sufficient for in-between calculations and result. -
Before doing the task make sure to read docs for classes
StringandInteger. -
For debugging use
System.err, because it will be ingnored by the testing program.
Solution
After reading about String, Integer, System.err we now know about some usefull methods:
Integer.parseInt(String s, int radix)which parses the string argument as a signed integer in the radix specified by the second argument. So it basically converts a number from theStringdata type toint.Character.isWhitespace(char ch)which checks if a characterchis a space symbol or not.
Now let’s start coding. Firstly let’s define the structure of our program. I will be putting the name of the file at the first line comment in a file and its path if its nessesary.
// Sum.java
public class Sum {
public static void main(String[] args) {
// ...
}
}Okay that’s done. What do we do next? Let’s look at String[] args argument to our main method. It represents an array of command line arguments which we need to sum. So know we made our task a little bit easier. Now we can just say that we need to find sum of elements of array args.
How are we going to do it though? First let’s understand what we can do with this array. Let’s modify our class a little bit.
// Sum.java
public class Sum {
public static void main(String[] args) {
System.out.println(args.length);
}
}We’ve added System.out.println(args.length) which takes the field length from our args object and prints it to the console.
Let’s try it. First compile our class using
$ javac Sum.javaAnd then we can do some manual testing.
$ java Sum 1 2 3
3We got 3 as an output as expected. We gave our program 3 command line arguments: 1, 2 and 3.
Here are all of the examples
$ java Sum 1 2 -3
3$ java Sum "1 2 3"
1Note
Notice, that we got 1 instead of 3. That’s because we put our arguments in "" so this becomes a single string argument.
$ java Sum "1 2" " 3"
2$ java Sum " "
1Note
Here program gives us 1 instead of 0, because despite not having any numbers in the arguments a single whitespace is still an argument.
Now let’s try not obly to count our arguments but to list them as well. Let’s modify our program a little bit more.
// Sum.java
public class Sum {
public static void main(String[] args) {
System.out.println("number of arguments: " + args.length);
for (String argument : args) {
System.out.println(argument);
}
}
}Here I used for loop to do printing for every String element in args.
Let’s try this with our examples. And don’t forget to recompile using javac Sum.java.
$ java Sum 1 2 3
number of arguments: 3
1
2
3$ java Sum 1 2 -3
number of arguments: 3
1
2
-3$ java Sum "1 2 3"
number of arguments: 1
1 2 3Note
Again, notice only one string argument.
$ java Sum "1 2" " 3"
number of arguments: 2
1 2
3$ java Sum " "
number of arguments: 1
Okay, now that we know how to iterate (or do something for every element), we can calculate the sum of all the numbers in the args array. To do that we need to create a new variable sum of integer type and assign it the value of 0. Then for every number in args we will add it to sum, and so by the end of array we will have the sum of all numbers stored in variable sum. This is what the code will look like
// Sum.java
public class Sum {
public static void main(String[] args) {
System.out.println("number of arguments: " + args.length);
int sum = 0;
for (String argument : args) {
sum = sum + argument;
System.out.println(argument);
System.out.println(sum);
}
System.out.println(sum);
}
}Seems ok. But let’s test it.
$ javac Sum.java
Sum.java:9: error: incompatible types: String cannot be converted to int
sum = sum + argument;
^
1 errorWe got a compilation error that says String cannot be converted to int. It means that when we try to add 2 variables sum and argument their types don’t match. The type of sum is integer and string for argument. It seems pretty logical because what do we expect when adding for example 1 and apple?..
Important
So we need to cast argument to integer data type so that we can add it to sum.
We can do so using Integer.parseInt() method.
It takes a String s and returns an int, which a string s represents. For example this code will work as expected
// ParseIntExample.java
public class ParseIntExample {
public static void main(String[] args) {
String s = "123";
int sum = 321;
//! sum = sum + s;
sum = sum + Integer.parseInt(s);
System.out.println(sum);
}
}The commented out line (//!) would have given us an exception.