Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help in text files and arrays in Java I want to know how to average numbers from

ID: 3569641 • Letter: H

Question

Help in text files and arrays in Java

I want to know how to average numbers from a text file and store them in an array. I want to find the average from 1 -5 is Jan and find the average from 5 -10 is Feb.

The num.txt file contains:

1 44 29
2 43 29
3 43 29
4 43 29
5 43 29
6 43 29
7 43 29
8 43 29
9 43 29
10 43 29

This is what I have so far but it shows an error message no such element exception

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class readTemp
{
   public static void main(String[] args) throws IOException
   {
       readTemp x = new readTemp();
       x.readNumbersJanuary();
   }

   public void readNumbersJanuary()
   {

       String fileName = "num.txt";
       Scanner inputStream = null;
       String line = new String();
       int numHigh;
       int sum=0;
       int count=0;
       int averageHigh;
       int numLow;
       int lowsum=0;
       int averageLow;

       int startline = 0;
       int endline = 5;

       try
       {
           inputStream = new Scanner (new File(fileName));
       }
       catch(FileNotFoundException e)
       {
           System.out.println("Error opening the file " + fileName);
           System.exit(0);
       }


       while(inputStream.hasNextLine())
       {
           for(int i = startline; i < endline +1; i++)
           {
               line = inputStream.nextLine();
               numHigh = Integer.parseInt(line.split(" ")[1]);
               numLow = Integer.parseInt(line.split(" ")[2]);

               lowsum += numLow;
               sum += numHigh;
               count++;
          
           }

           averageHigh = sum/count;
           averageLow = lowsum/count;
          
           System.out.println("Average high for January: " + averageHigh);
           System.out.println("Average low for January: " + averageLow);
       }
  
       inputStream.close();

   }
}

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Scanner;

public class readTemp
{
public static void main(String[] args) throws IOException
{
readTemp x = new readTemp();
x.readNumbersJanuary();
}

public void readNumbersJanuary()
{

String fileName = "num.txt";
Scanner inputStream = null;
String line = new String();
int highsum=0;
int count=0;
int averageHigh;
int lowsum=0;
int averageLow;

try
{
inputStream = new Scanner (new File(fileName));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}

String[] afterSplit = new String[3];
while(inputStream.hasNextLine()) {

line = inputStream.nextLine();
afterSplit = line.split(" ");
  
if (Integer.parseInt(afterSplit[0]) == 6) { // Remember, this block is entered only once when line
// number is 6 and hence average is calculated once only when
// first half of input is read
  
/**** Average of January****/
averageHigh = highsum / count;
averageLow = lowsum / count;
System.out.println("Average high for January: " + averageHigh);
System.out.println("Average low for January: " + averageLow);
  
/**** Resetting the counters and summations ****/
lowsum = 0;
highsum = 0;
count = 0;
}

lowsum += Integer.parseInt(afterSplit[2]);
highsum += Integer.parseInt(afterSplit[1]);
count++;
}

/**** Average of February****/
averageHigh = highsum/count;
averageLow = lowsum/count;
System.out.println("Average high for February: " + averageHigh);
System.out.println("Average low for February: " + averageLow);

inputStream.close();

}
}