I would like to know how could I read data from a file. I have got something like that:
1 10
2 25
3 40
4 25
5 24
6 25
7 25
8 41
9 55
10 11
11 54
12 20
where numbers from 1-12 are actually months, which I have to convert to a String.
At the moment my program is reading all those integers, but I need to make it to read only second column for counting averages...
So far my code:
import java.util.*;
import java.io.*;
class Exercise{
public static void main(String[] args)throws Exception{
ArrayList<Integer> store = new ArrayList<Integer>();
String FileName;
int total = 0;
int number = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Please type in the name of your file: ");
FileName = scanner.nextLine();
File inputFile = new File(FileName);
Scanner reader = new Scanner(inputFile);
while(reader.hasNext()){
int numbers = reader.nextInt();
store.add(numbers);
System.out.println(numbers);
}
Iterator<Integer> myIterator = store.iterator();
while(myIterator.hasNext()){
number = myIterator.next();
total = total + number;
}
System.out.println("The total rainfall for this year is: " + total);
int average = ((total)/12);
System.out.println("The average of monthly rainfall is: " + average);
Many thanks for any help!

New Topic/Question
Reply



MultiQuote






|