# How can i get a number obtained from a buffered reader file?

Asked by At

I need to get a number from a textFile, using a BufferedReader. When i get it, the number is coded in ASCII, and i cant convert it into integer.

This exercise is for a class exercise. Exercise: Create a text file in a folder with the Windows notebook. This file will contain integers, one for each line. Create a program in Java that reads the previous data file and shows us on screen, the average of the values, the highest value and the smallest value. The user will indicate the name of the file.

``````char valor;
fs = new FileReader(path+filename);
bf = new BufferedReader(fs)

//THE TEXTFILE CONTAINS INT NUMBERS, FIRST OF THEM IS NUMER 3
valor = (char)bf.read(); //I NEED THIS NUMBER, WITHOUT ASCII CODED

//I NEED TO USE THIS VALUE FOR CALCULATE
System.out.print(valor); //CONSOLE :      3

---------------------------------------------
//IF I CONVERT, CONSOLE SHOWS
System.out.print((int)valor);   //CONSOLE:     51
``````

## 2 Answers On

This happens because the index in ASCII table of character '3' is 51 (ASCII table). You need to parse it:

``````int value = Character.getNumericValue(valor);
``````

or subtract ASCII code of '0' (48)

``````int value = valor - '0';
`````` On

One approach would be to use the constructor from the `Integer` class which can accept a string input:

``````int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int sum = 0;
int cnt = 0;

try (BufferedReader br = Files.newBufferedReader(Paths.get(path+filename))) {
String line;
while ((line = br.readLine()) != null) {
Integer val = new Integer(line);
if (val < min) min = val;
if (val > max) max = val;
sum += val;
++cnt;
}
}
catch (IOException e) {
System.err.format("IOException: %s%n", e);
}

System.out.println("average value: " + (sum / cnt));
System.out.println("highest value: " + max);
System.out.println("smallest value: " + min);
``````