[General boards] [Fall 2018 courses] [Summer 2018 courses] [Winter 2018 courses] [Older or newer terms]

PCRS / file / part1


#1

In this exercise, it want us to use FileInputStremExercise to summ the integer in a file
but when I print the integers, I get the integer plus 48, so if the first integer is 1, using in.read() will give me 49
so i have to deduct 48 from evey number to get the correct output
anyone can explain why is this?
below is my code, it works, but there’s a extra step with the numbers

import java.io.*;

public class FileInputStreamExercise {
public static int sumAll(String path) throws IOException {
int sum = 0;
FileInputStream in = null;

    try {
        in = new FileInputStream(path);
			 int a = 0;
        while (a != -1) {
            a = in.read();
            sum = sum + a - 48;
        }
        sum = sum + 49;       
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return sum;
}

}