Reputation: 11
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. 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:
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:
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;
}
}
Upvotes: 1
Views: 1127
Reputation: 5315
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
Note: I'm not sure what you're trying to do with your different indexes, but they'll probably all be .get(i)
Upvotes: 1
Reputation: 858
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#$%^&". 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. See WOUNDEDStevenJones answer for more details.
Upvotes: 2
Reputation: 437
The method list.get(i) will return an object of type Guest
. Guest
has methods, getWeight()
and 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
Upvotes: 0