简体   繁体   中英

Counting the number of weeks in a txt file

I am reposting this because i feel i didn't word it correctly

I am trying to find the percentage of weeks with an average temperature of 90 or above.

the text file looks like:

1/1/2009 76.0 81.1 68.1 86.7 99.2 97.5 92.9
1/8/2009 61.0 86.2 99.3 74.2 89.5 100.2 80.7
1/15/2009 95.7 76.2 92.1 66.5 66.7 80.2 76.3
1/22/2009 97.5 63.0 77.3 71.9 84.8 73.8 80.7

....for a total of 39 weeks

How can i find the average temperatures for each week?

we are supposed to use a for loop to read the 7 temperatures but i do not even understand how to get started.

this is what i have so far

 while (scan.hasNextLine())
 {
 count++;
 scan.nextLine();
 }
 for (

 System.out.println("Temperature Statistics:");  
 System.out.println("Number of weeks: " + count);
 System.out.println("Weeks with an average of 90 or higher: "+averageAbove);  

I presume you can count the number of rows in the text file:

LineNumberReader  lnr = new LineNumberReader(new FileReader(new File("YourFile.txt")));
lnr.skip(Long.MAX_VALUE);
System.out.println("Number of weeks: " + lnr.getLineNumber());

EDIT:

Due to your question udpate, this is what you can do in order to only print the total number of the weeks instead of printing each week:

 while (scan.hasNextLine())
 {
   count++;
   scan.nextLine();
 }

System.out.println(count);

Are you just looking to simply count the number of lines in a text file?

If so, the following code will do it, however it's not very efficient.

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int count = 0;
while ((line = br.readLine()) != null) {
   count++
}
br.close();
System.out.println("Number of weeks: " + count);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM