Java无法找到符号错误

时间:2014-09-25 06:19:37

标签: java compiler-errors

编辑:没有错误,但我的输出并不像预期的那样。不显示数据:

 ----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.

我有三节课。

天气

    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);

     }

     */

}

驱动程序

  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();

     }
}

我得到的错误是:

     ----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.

我不确定问题是什么,如果有人有任何想法请告诉我我做错了什么。感谢。

3 个答案:

答案 0 :(得分:2)

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

您正尝试在ArrayList上调用sortTemperature()

您需要在自己的类实例上调用它。

也许,因为w1是这些实例的列表(但不确定你要做什么)

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

答案 1 :(得分:0)

w1是一个arrayList,没有方法sortTemperature()

方法sortTemperature()在对象WetherList中定义。

因此,您必须从列表w1.get(0)中读取一个对象,然后才能调用sortTemperature()

w1.get(0).sortTemperature()

答案 2 :(得分:0)

sortTemperatureWeatherList类的方法,您尝试在ArrayList上调用它。

您应该迭代列表,然后在该对象上调用sortTemperature