Suppose an external file is made up entirely of integers. In the model we've been using in this unit, the file is actually read in, line by line, as a sequence of Strings. Using a StringTokenizer inside processLine() method can produce individual tokens that look like numbers, but are actually Strings that are composed of digits. To convert each token from String format to integer format, you need to use the parseInt() method from the Integer class. Thus
int birthYear = Integer.parseInt("1983");
correctly stores the integer 1983 into the birthYear integer variable.
For this assignment you should create a complete processLine method in the EchoSum class, so that it transforms each number in each line to integer format, and then sums the entries on the line and prints their sum on a separate line. For example, if the external text file looks like this:
1 2 3 4
3 4 7 1 11
2 3
your program should display this:
10
26
5
My code is :
int sum = 0;
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
sum += Integer.parseInt(st.nextToken());
}
System.out.println(sum);
I don't know why my code can't compile.