简体   繁体   中英

Java can't find symbol error

Edit: don't have error but my output isn't as expected. Displays no data:

 ----jGRASP exec: java Driver

[CONDITION   TEMPERATURE     DATE, CONDITION     TEMPERATURE     DATE, CONDITION     TEMPERATURE     DATE]

The average of Temperatures is :0
The average of Temperatures is :0
The average of Temperatures is :0
Searching for the condition Foggy
Sorting the data by temperatures: 

 ----jGRASP: operation complete.

I have three classes.

Weather

    import java.util.*;

public class Weather implements Comparable<Weather>

{

     public String condition;

     public int temperature;

     public Calendar date;



     public Weather()

     {

          condition=null;

          temperature=0;

          date=new GregorianCalendar();

     }

     public Weather(String cond, int temp, Calendar d)

     {

          condition=cond;

          temperature=temp;

          date=d;

     }



     public String getCondition()

     {

          return condition;

     }

     public int getTemperature()

     {

          return temperature;

     }

     public Calendar getDate()

     {

          return date;

     }

     public int compareTo(Weather compareTemp)

     {

          int compareQuantity = ((Weather) compareTemp).getTemperature();

          //ascending order

          return this.temperature - compareQuantity;

     }

     public String toString()

     {

          return getCondition()+"\t"+ getTemperature()+"\t"+getDate();

     }

     public int compare(Weather w1, Weather w2) {

        if(w1.getTemperature() == w2.getTemperature()){

            return 0;

        } else {

            return -1;

        }

    }

}

WeatherList

    import java.util.*;

public class WeatherList

{

     public String cond;

     public int temp;

     public Calendar d;

     //public int month, year,day;

     public ArrayList<Weather> weather;

     public int size;



     public WeatherList()

     {}

     public WeatherList(String cond, int temp, Calendar date)

     {

          this.cond=cond;

          this.temp=temp;

          this.d=date;

          weather=new ArrayList<Weather>();

     }

     //add method

     public void addMethod(String cond, int temp, Calendar d)

     {

          Weather w=new Weather(cond, temp,d);

          weather.add(w);

     }

     // size increase method

     public void setsize()

     {

          if(weather.size()==0)

          {

              size=weather.size()*2;

          }

     }

     //averageTemp method that returns average of temperatures

     public int averageTemp()

     {

          int avg;

          int total=0;

          List<Weather> list= weather;

          for(Weather ls: list)

                   total=+ls.getTemperature();

          avg=total/weather.size();



     return avg;

     }



     public String toString()

     {

          return "CONDITION \t TEMPERATURE \t DATE";

     }

     public void display()

     {

          for(int i=0; i<weather.size(); i++)

              System.out.println(weather.get(i));

     }



     public void searchCondition(String s)

     {

          boolean f=false;

          for (int i = 0; i<weather.size(); i++)

          {

                 if (weather.get(i).equals(s))

                 {

                     System.out.println("The date when it is Foggy is: " +weather.get(i).getDate());

                     System.out.println("The difference in the temperature is: " + (averageTemp()- weather.get(i).getTemperature()));

                     f = true;

                 }

          }     

             if (!f)

                 System.out.println("Key not found");

     }





     public void sortTemperature()

     {

          List<Weather> l= weather;

          Collections.sort(l);

        for(Weather str: weather){



             System.out.println(toString());

              System.out.println(str.getCondition()+"\t"+ str.getTemperature()+"\t"+str.getDate());

        }

     }



     /*public void findTemp()

     {

          //int searchkey=75;



          int index = Collections.binarySearch(weather, "75");

              System.out.println("Element found at : " + index);

     }

     */

}

Driver

  import java.util.*;

public class Driver 

{
     public static void main(String args[])

     {

          Calendar gc=new GregorianCalendar();

          String con;

          int tem;

          int day, month, year;

          Calendar d;

          ArrayList<WeatherList> w1=new ArrayList<WeatherList>();

          Scanner scan=new Scanner(System.in);

              gc.set(45, 9+1, 12);

              WeatherList w2=new WeatherList("Foggy", 67, gc);

              gc.set(20, 10+1, 2);

              WeatherList w3=new WeatherList("Rainy", 30, gc);

              gc.set(29, 0+1, 22);

              WeatherList w4=new WeatherList("Cold", 45, gc);

              w1.add(w2);

              w1.add(w3);

              w1.add(w4);

          System.out.println(w1.toString());

          for(int i=0; i<w1.size();i++)

          {

              w1.get(i).display();

          }

          System.out.println();

          for(int i=0;i<w1.size();i++)

          System.out.println("The average of Temperatures is :"+ w1.get(i).averageTemp());

          System.out.println("Searching for the condition Foggy");

          for(int i=0; i<w1.size();i++)

          {

              w1.get(i).display();

          }

          System.out.println("Sorting the data by temperatures: ");

          w1.sortTemperature();

     }
}

The error i'm getting is:

     ----jGRASP exec: javac -g Driver.java

Driver.java:70: error: cannot find symbol
          w1.sortTemperature();
            ^
  symbol:   method sortTemperature()
  location: variable w1 of type ArrayList<WeatherList>
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

I'm not sure what the issue is, if anyone has any idea please let me know what I did wrong. Thanks.

cannot find symbol w1.sortTemperature(); 
^ symbol: method sortTemperature() 
location: variable w1 of type ArrayList 1 error

You are trying to call sortTemperature() on an ArrayList.

You need to call it on your own class instance instead.

Maybe, since w1 is a List of those instances (but not sure what you are trying to do)

 for (WeatherList w: w1){
     w.sortTemperature();
 }

w1 is an arrayList and do not have a method sortTemperature() .

The method sortTemperature() is defined in the Object WetherList .

So you have to read an object from the list w1.get(0) and then you can call sortTemperature() .

w1.get(0).sortTemperature()

sortTemperature is method of WeatherList class,and you are trying to call it on ArrayList .

You should iterate the list,and then call sortTemperature on that object.

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