数组列表。 ArrayList为int和double

时间:2014-02-19 16:17:07

标签: java arraylist int double

我遇到的问题是,我无法从arraylist中取一个数字并将其变为intdouble,我必须这样做才能使用体重来衡量BMI高度。请看看!
分配是将客人的体重,长度和姓名放入其中,并对那些长度与身高比例较差的人进行排序。在主要方面,我创建了一个包含几个访客的数组,当我运行它时说:

"Exception in thread "main" java.lang.NumberFormatException: For input string "name"

java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at Diet.putOnDiet(Diet.java:12)
at TestDiet.main(TestDiet.java:7)

Diet类如下:

public class Diet{

    public static ArrayList<Guest> putOnDiet(ArrayList <Guest> list){
        ArrayList<Guest> namn = new ArrayList<Guest>();
        ArrayList<Guest> hej = new ArrayList<Guest>();
        for(int i = 0; i<=list.size()/3; i = i+3){

            int langd = Integer.parseInt(list.get(i+1).toString()); //I dont know how to make this work
            double vikt = Double.parseDouble(list.get(i).toString());
            String name = list.get(i+2).toString();

            if ((vikt) > 1.08*(0.9*(langd -100))){
                namn.add(new Guest(vikt, langd, name));
            }
        }

        return namn;
    }
}

Guest类:

public class Guest { 

   private double weight; private double length; private String name; 

   public Guest(double weight, double length, String name){ 
      this.name = name; this.weight = weight; this.length = length; // Maybe the problem is here. How do you modify the class to make it work?
   } 
   public String getName() {
      return name; 
   } 
   public double getWeight() 
   { 
      return weight; 
   } 
   public double getLength() { 
      return length; 
   } 
   public void setName(String name) { 
      this.name = name; 
   } 
   public void setWeight(double weight) { 
      this.weight = weight; 
   } 
   public void setLength(double length) 
   { this.length = length; 
   } 
   public boolean isSlim() { 
      if (weight >= 1.08 * 0.9 * (length - 100)) { 
         return false; 
      }
      else 
         return true; 
   }
   public String toString() { 
      return name + "\n" + weight + "\n" + length; 
   } 
}

3 个答案:

答案 0 :(得分:2)

您确定要解析整数吗?

无法解析数字时抛出井号解析异常。当字符串不是像“somthing#$%^&amp;”这样的数字时。所以尝试替换这一行

int langd = Integer.parseInt(list.get(i+1).toString());

用这个

try {
       int langd = Integer.parseInt(list.get(i+1).toString());
} catch (NumberFormatException e) {
       System.out.println(list.get(i+1).toString() +" : This is not a number");
       System.out.println(e.getMessage());
}

编辑阅读WOUNDEDStevenJones后,我也认为你甚至不应该使用toString()或解析方法。有关更多详细信息,请参阅WOUNDEDStevenJones答案。

答案 1 :(得分:1)

您似乎想将其更改为

for (int i=0; i<list.size(); i++) {
    double langd = list.get(i).getLength();
    double vikt = list.get(i).getWeight();
    String name = list.get(i).getName();
}

并且为此目的忽略了您的getString()方法

注意:我不确定您要对不同的索引做什么,但它们可能都是.get(i)

答案 2 :(得分:0)

方法list.get(i)将返回类型为Guest的对象。 Guest有方法getWeight()getLength()

list.get(i).getWeight() 

这实际上会给你一个双倍的回报。 和,

Integer.parseInt(list.get(i).getWeight().toString()) 

这应该能够解析。

希望这有帮助。

_san