編輯:沒有錯誤,但我的輸出不符合預期。顯示沒有數據:Java無法找到符號錯誤
----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.
我不確定問題是什麼,如果有人有任何想法,請讓我知道我做錯了什麼。謝謝。
請只提供相關的代碼。 – 2014-09-25 06:20:32
'w1'是一個'ArrayList',java中的arraylist沒有'sortTemperature'方法 – 2014-09-25 06:22:24