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