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

Hello, I\'m having trouble getting this program to output to a file. Please let

ID: 3836689 • Letter: H

Question

Hello, I'm having trouble getting this program to output to a file. Please let me know how to fix it. I am just getting an error "The method println(calcWeather) is undefined for the type FileWriter" on the line that says: outFile.println(cw);

Here is the Main Class:

package p5;
import java.util.*;
import java.io.*;
import java.util.Scanner;


public class WeatherMain {

  
   public static void main(String[] args) throws IOException {
       Scanner inFile;
       FileWriter outFile;
     
      
       //default constructor
       CalcWeather cw;
       double temp [][] = new double [2][12];
       inFile = new Scanner(new FileReader("weatherData.txt"));
       outFile = new FileWriter("OutWeatherData.txt");

       //import data
       for (int row = 0; row < temp.length; row++)
           {
               for(int collum= 0; collum < temp[row].length; collum++)
               {
                   temp[row][collum]= inFile.nextDouble();
               }
           }                          
      
       cw = new CalcWeather(temp);
  
  
  
//outfile
System.out.println(cw);
outFile.println(cw); //the error in my program



       inFile.close();
       outFile.close();
   }

}

**********Here is the calc weather class:

package p5;

public class CalcWeather {

   //public static void main(String[] args) {

   //variables
   double [][]temp;
   double averageHigh;
   double averageLow;
   double indexHighTemp;
   double indexLowTemp;
   double lowTemp;
   double highTemp;
   int index;


   //Default constructor
   public CalcWeather()
   {
       lowTemp = 0;
       highTemp = 0;
       averageHigh=0;
       averageLow=0;
       indexHighTemp=0;
       indexLowTemp=0;
       lowTemp=0;
       highTemp=0;
       index=0;
       temp= new double[2][12];
       for (int row = 0; row < temp.length; row++)
       {
           for(int collum= 0; collum < temp[row].length; collum++)
           {
               temp[row][collum]=0;
           }
       }

   } //end default constructor
  
   //alternate constructor
   public CalcWeather(double[][] array)
   {
       lowTemp = 0;
       highTemp = 0;
       averageHigh=0;
       averageLow=0;
       indexHighTemp=0;
       indexLowTemp=0;
       lowTemp=0;
       highTemp=0;
       index=0;
       temp= new double[2][12];
      
       for (int row = 0; row < temp.length; row++)
       {
           for(int collum= 0; collum < temp[row].length; collum++)
           {
               temp[row][collum]=array[row][collum];
           }
       }  
       averageHigh = averageHigh();
       averageLow = averageLow();
       indexHighTemp = indexHighTemp();
       indexLowTemp = indexLowTemp();
      
      
   }//end alternate constructor
  
   //copy constructor
   public CalcWeather(CalcWeather other)
   {
       this.lowTemp = other.lowTemp;
       this.highTemp = other.highTemp;
       this.averageHigh= other.averageHigh;
       this.averageLow= other.averageLow;
       this.indexHighTemp= other.indexHighTemp;
       this.indexLowTemp= other.indexLowTemp;
       this.lowTemp= other.lowTemp;
       this.highTemp= other.highTemp;
       this.index=other.index;
       temp= new double[2][12];
       for (int row = 0; row < temp.length; row++)
       {
           for(int collum= 0; collum < temp[row].length; collum++)
           {
               this.temp[row][collum]=other.temp[row][collum];
           }
       }
   }//end copy constructor


   //(1)   A method that calculates the Average high for the year
   public double averageHigh()
   {
       double average = 0;

       for (int index = 0; index < temp[0].length; index++)
       {
           average += temp[0][index];
       }

       average = average / 12;
       return average;
   }//end average high


   //(2)   A method that calculates the Average low for the year

   public double averageLow()
   {
       double average = 0;

       for (int index = 0; index < temp[1].length; index++)
       {
           average += temp[1][index];
       }
       average = average / 12;
       return average;
   }//end averagelow

   //(3)   A method that calculates and returns the index of the Highest temperature of the year

   public double indexHighTemp()
   {
       int high = 0;

       for (int index = 0; index < temp[0].length; index++)
       {
           if (temp[0][index] >temp[0][high])
               high = index;
       }

       return high;
   }//end indexHighTemp
   //(4)   A method that calculates and returns the index of the Lowest temperature of the year

   public double indexLowTemp()
   {
       int low = 0;

       for (int index = 0; index < temp.length; index++)
       {
           if (temp[1][index]                low = index;
       }

       return low;
   }//end indexlowTemp

   //(5)   Should have all other standard methods such as setters, getters, constructors (default, alternate, and copy), makCopy, getCopy, equals, toString.


   //getters and setters
   public double[][] getTemp()
   {
       return temp;
   }//end getTemp


   public void setTemp(double[][] data)
   {
       for (int row = 0; row < temp.length; row++)
       {
           for(int collum= 0; collum < temp[row].length; collum++)
           {
               temp[row][collum]=data[row][collum];
           }
       }  
   }//end set temp
  
   //getCopy
  
   private CalcWeather getCopy()
   {
       CalcWeather Copy = new CalcWeather(temp);
       return Copy;
   }//end copy
   //makeCopy
   private void makeCopy(CalcWeather other)
   {
       this.lowTemp = other.lowTemp;
       this.highTemp = other.highTemp;
       this.averageHigh= other.averageHigh;
       this.averageLow= other.averageLow;
       this.indexHighTemp= other.indexHighTemp;
       this.indexLowTemp= other.indexLowTemp;
       this.lowTemp= other.lowTemp;
       this.highTemp= other.highTemp;
       this.index=other.index;
       temp= new double[2][12];
       for (int row = 0; row < temp.length; row++)
       {
           for(int collum= 0; collum < temp[row].length; collum++)
           {
               this.temp[row][collum]=other.temp[row][collum];
           }
       }
   }//end makeCopy

   //getavgHigh
   public double getavgHigh()
   {
       return averageHigh();
   }//getavgHight


   //getavgLow
   public double getavgLow()
   {
       return averageLow();
   }//end getavgLow

   //gethigh
   public double gethighTemp()
   {
       return highTemp;
   }//end gethigh

   //getlowtemp
   public double getlowTemp()
   {
       return lowTemp;
   }//end get low

   //toString
   public String toString() {

       String str = "";
//String.format();
       String.format("%.2f%n%.2f%n%.2f", averageHigh, averageLow, indexHighTemp, indexLowTemp);
      
       str = ("This is the average high: " + averageHigh +
               " This is the average low: " + averageLow +
               " This is the highest temp of the year: " + indexHighTemp +
               " This is the lowest temp of the year: " + indexLowTemp);
      

       return str;
   }//end toString
  
   //equals
       public boolean equals(CalcWeather other)
       {
       for (int row = 0; row < temp.length; row++)
       {
           for(int collum= 0; collum < temp[row].length; collum++)
           {
               if (this.temp[row][collum]!= other.temp[row][collum])
                   return false;
               }
           } return true;
       }//end equals
      
}//end class CalcWeather

Explanation / Answer

YOu have not provided the text file so Will not able to run it.. The error is as follows : -

outFile.println(cw); //the error in my program

There is no println method in FileWriter. to write the component in files, we need to use write() method and it takes string as argument, so we cannot pass object cw directly, but we need to use toString() method which you have defined in your class. The statement is as follows: -

outFile.write(cw.toString());

One more thing in the loops wherew your are importing data in temp 2D array, you need to have a check like inFile.hasNext() to check if there is value in the file or not. Otherwise it will send No suchException.

So your updated WeatherMain.java will be something like this.: -

WeatherMain.java


import java.util.*;
import java.io.*;
import java.util.Scanner;

public class WeatherMain {
  
public static void main(String[] args) throws IOException {
Scanner inFile;
FileWriter outFile;

  
//default constructor
CalcWeather cw;
double temp [][] = new double [2][12];
inFile = new Scanner(new FileReader("weatherData.txt"));
outFile = new FileWriter("OutWeatherData.txt");
//import data
for (int row = 0; row < temp.length; row++)
{
for(int collum= 0; collum < temp[row].length; collum++)
{
   if(inFile.hasNext()) {
       temp[row][collum]= inFile.nextDouble();
   }
}
}
  
cw = new CalcWeather(temp);
  
  
  
//outfile
System.out.println(cw);
outFile.write(cw.toString()); //the error in my program



inFile.close();
outFile.close();
}
}

Run: -

Output for my text file is :-

This is the average high: 313.0469333333333
This is the average low: 0.0
This is the highest temp of the year: 11.0
This is the lowest temp of the year: 0.0

Note that this output will be different from yours as weatherData.txt you are using is different.

Please run the above java file with your own txt file. It will work.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote