1

I'm new to java, and doing this for class. I have been beating my head against it for hours. I initially just used multiple ArrayLists to do this, but my prof wants me to redo it using one ArrayList, and I can't figure it out. Head --> Wall.

The end goal is for my array list to contain objects that are made up of my custom class. I'm attempting to generate an ArrayList from a file. I'm attempting to use a custom class to generate the objects within the ArrayList.

I believe I've narrowed down the problem that I'm not properly getting the file to load correctly into my custom class.

Notice where I have "System.out.print(weather);", the following prints: Weather_Class@55f96302Weather_Class@3d4eac69Weather_Class@42a57993Weather_Class@75b84c92Weather_Class@6bc7c054Weather_Class@232204a1Weather_Class@4aa298b7

It seems like those each represent an object, and that makes sense because there would be 7 objects from the file, but I would expect weather to print out 7 times, 1 for each line of the file, and whatever it was printing out, should also be added to the ArrayList "list" as an object.

 Scanner keyboard = new Scanner(System.in);
 System.out.print("Please enter the file name: ");
 fileName = keyboard.next();
 //file name: genWeatherSort.txt

 File file = new File(fileName);
 Scanner inputFile = new Scanner(file);

 ArrayList<Weather_Class> list = new ArrayList<Weather_Class>();
 while (inputFile.hasNext()){
    int day = inputFile.nextInt();
    double highTemp = inputFile.nextDouble();
    double recHighTemp = inputFile.nextDouble();
    int year = inputFile.nextInt();
    double humid = inputFile.nextDouble();

    Weather_Class weather = new Weather_Class(day, highTemp, recHighTemp,
       year, humid);
    System.out.print(weather);

    list.add(weather);   
 }
 inputFile.close();

Weather_Class:

public class Weather_Class { 

   private int day; 
   private double highTemp;
   private double recHighTemp;
   private int year;
   private double humid;

   public Weather_Class(int day, double highTemp, 
   double recHighTemp, int year, double humid){
      this.day = day;
      this.highTemp = highTemp;
      this.recHighTemp = recHighTemp;
      this.year = year;
      this.humid = humid;
   }

   public int getDay(){
      return day;
   }

   public double getHighTemp(){
      return highTemp;
   }

   public double getRecHighTemp() {
      return recHighTemp;
   }

   public int getYear() {
      return year;
   }

   public double getHumid() {
      return humid;
   }

}

Here is the information in the txt file I'm using:

   15    88.98    89.2  1944    1.7
  104    63.83    78.3  1981   25.8
  179    80.89   117.3  2008   39.0
  202    83.45    93.4  1947   39.5
  262    57.82   125.3  1983   52.0
  364   120.61   124.8  1985   27.0
    0     0.00     0.0     0    0.0
TyJerome
  • 11
  • 1

0 Answers0