Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Pieterjan Espeel
3,656 PointsError: NumberFormatException whilst running code.
Was trying to make a basic program to try some of the new things I learned but i constantly got the same error while trying to run the code. I didn't add an extra space, neither did I double tap enter.
public class Introductions {
public static void main(String[] args) {
Console console = System.console();
String startingProcedure = console.readLine("You started this program so I guess you want to calculate the forceOfGravity .. ? Type 'Y' if that is what you want if not press any other key :");
if (startingProcedure.equalsIgnoreCase("y")) {
String heightl = console.readLine("What is the height of the object toward the ground (in meters):");
int height = Integer.parseInt("heightl");
String weightl = console.readLine("What is the weight of the ball (in kilograms):");
int weight = Integer.parseInt("weightl");
String onEarth = console.readLine("Are you on earth right now ? .. :");
if(onEarth.equalsIgnoreCase("y") || onEarth.equalsIgnoreCase("yes")) {
Ball ball = new Ball(weight, height, 10);
ball.calculateForce();
}
else {
System.exit(0);
}
}
else {
System.exit(0);
}
} }
Error msg:
What is the height of the object toward the ground (in meters):4
Exception in thread "main" java.lang.NumberFormatException: For input string: "heightl"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Introductions.main(Introductions.java:10)
If someone wouldn't mind taking a bit of their time to help me it would be very nice.
2 Answers
Matthew Caloger
12,903 PointsRemove the double-quotes from Integer.parseInt("heightl"); and Integer.parseInt("weightl");.
If something is between the double-quotes "like this" it's a String, so while you're trying to reference your variable Height1 you're actually just telling it to parse the int of a string that says "Height1", which it can't do.
Let me know if you need further explanation, and good luck!
Pieterjan Espeel
3,656 PointsThankyou very much !