public class Packages
{
private ArrayList shipmentList = new ArrayList();
private double totalWeight;
public Packages(String shiplist) throws IOException
{
Scanner scanFile = new Scanner(new File(shiplist));
scanFile.useDelimiter("\n");
while(scanFile.hasNext() == true){
String pack = scanFile.next();
Scanner splitter = new Scanner(pack);
splitter.useDelimiter(" ");
int id = splitter.nextInt();
double weight = splitter.nextDouble();
String state = splitter.next();
shipmentList.add(new Packet(id, weight, state));
}
this.totalWeight = 0;
}
public String toString()
{String allShipments ="";
for(int i =0;i<shipmentList.size();i++)
allShipments += shipmentList.get(i) + "\n";
return allShipments;
}
//needs work. Cannot find method isLight() from class packet, yet toString() fires fine.
public void displayLightPackages()
{
for (int i=0;i<shipmentList.size();i++){
Packet thisShipment = shipmentList.get(i);
if(thisShipment.isLight() == true){
System.out.println("Package: "+ shipmentList.get(i).toString()+" Is light.");
}else{
System.out.print("");
}
}
}
方法displayLightPackages()給我錯誤。這裏是包類:爪哇,無法找到符號和麻煩從數組列表中提取dataType
public class Packet
{
// instance variables - replace the example below with your own
private int idNumber;
private double weight;
private String state;
public Packet(int idNumber, double weight, String state)
{
this.idNumber= idNumber;
this.weight = weight;
this.state = state;
}
public boolean isHeavy()
{
return (weight>10);
}
public boolean isLight()
{
return (weight<10);
}
public String toString()
{
return "ID: "+idNumber+" WEIGHT: "+weight+" STATE: "+state;
}
}
toString方法觸發從主要方法的調用正常,但是,我不能從包類訪問任何其他方法。構造函數在將數據包添加到包含所有數據包的Arraylist時工作。我試圖在displayLightPackages中定義一個數據包,但是我收到了一個不兼容的類型錯誤。在displayLightPackages方法中,當我使用代碼
shipmentList.get(i).isLight()
我在編譯器找不到符號isLight時出現錯誤。我懷疑這個錯誤與arraylist中的dataType有關,但除此之外,我現在處於虧損狀態。
謝謝
哇,謝謝你。徹底解決了我的問題,我學到了一些東西!再次感謝。 – user451711