简体   繁体   English

数组列表。 ArrayList为int和double

[英]ArrayList. ArrayList to int and double

I have the problem that I can't take a number from the arraylist and make it into a int or double which I have to do to cacluate the BMI using weight and height. 我有一个问题,我不能从arraylist取一个数字并将其变成一个intdouble ,我必须做的是使用重量和高度来计算BMI。 Please have a look! 请看一看!
The assignment is to put in guests' weight, length, and name and sort the ones with a bad length to height ratio out. 分配是将客人的体重,长度和姓名放入其中,并对那些长度与身高比例较差的人进行排序。 In the main I create an array with a couple of guests and when I run it says: 在主要的我创建一个包含几个客人的数组,当我运行它时说:

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

and

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)

The Diet class is as follows: 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;
    }
}

And the Guest class: 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; 
   } 
}

Are you sure that the you are parsing an integer? 你确定要解析一个整数吗?

Well number parsing exception is thrown when it can't parse the number. 当无法解析数字时,抛出井号解析异常。 When the string is not a number like "somthing#$%^&". 当字符串不是“somthing#$%^&”之类的数字时。 So try replacing this line 所以尝试更换这一行

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

with this 有了这个

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

EDIT After reading WOUNDEDStevenJones answer I also think you should not be even using toString() or parsing methods. 编辑阅读WOUNDEDStevenJones后,我也认为你不应该使用toString()或解析方法。 See WOUNDEDStevenJones answer for more details. 有关详细信息,请参阅WOUNDEDStevenJones答案。

It looks like you'll want to change it to 看起来你想要改成它

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

and kind of ignore your getString() method for this purpose 并为此目的忽略你的getString()方法

Note: I'm not sure what you're trying to do with your different indexes, but they'll probably all be .get(i) 注意:我不确定你要对不同的索引做什么,但它们可能都是.get(i)

The method list.get(i) will return an object of type Guest . 方法list.get(i)将返回Guest类型的对象。 Guest has methods, getWeight() and getLength() . Guest有方法, getWeight()getLength()

list.get(i).getWeight() 

This would actually give you a double value in return. 这实际上会给你一个双倍的回报。 And, 和,

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

This should be able to parse. 这应该能够解析。

Hope this helps. 希望这可以帮助。

_san _san

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM