Reputation:
So I'm making a program and I want an exception, so the program wont crash.
If the user puts a string that is not in the else if statements
then it would not crash.
Also, I tried doing that for the integer
, so if someone tries to write something that is not an integer
it wont crash. And the program will catch it and would say that it isn't an integer
.
How can I get exception in my try catch
in java
.
Thank for your help
Here is the code:
import java.util.InputMismatchException;
import java.util.Scanner;
public class WeightOnADifferentPlanet {
public static void main ( String[] args ){
Scanner scan = new Scanner ( System.in );
System.out.println("Where do you want to travel:?");
try{
String planetName = scan.nextLine();
}
catch(/*need help here*/){
System.out.println("Please check your spelling");
}
System.out.println("Please enter your weight:");
try{
int weight = scan.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("That is not an integer");
}
double earthCalculation = weight * 1.0;
double jupiterCalculation = weight * (21.0 / 8.0); //check
double marsCalculation = weight * (3.0 / 8.0);
double mercuryCalculation = weight * (3.0 / 10.0);
double neptuneCalculation = weight * (11.0 / 10.0); //check
double plutoCalculation = weight * (7.0 / 10.0);
double saturnCalculation = weight * (6.0 / 5.0); //check
double uranusCalculation = weight * (9.0 / 10.0);
double venusCalculation = weight * (7.0 / 8.0);
if (planetName.equalsIgnoreCase("Earth"))
{
System.out.println("Your weight on "+planetName+" is: "+earthCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Jupiter"))
{
System.out.println("Your weight on "+planetName+" is: "+jupiterCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mars"))
{
System.out.println("Your weight on "+planetName+" is: "+marsCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mercury"))
{
System.out.println("Your weight on "+planetName+" is: "+mercuryCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Neptune"))
{
System.out.println("Your weight on "+planetName+" is: "+neptuneCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Pluto"))
{
System.out.println("Your weight on "+planetName+" is: "+plutoCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Saturn"))
{
System.out.println("Your weight on "+planetName+" is: "+saturnCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Uranus"))
{
System.out.println("Your weight on "+planetName+" is: "+uranusCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Venus"))
{
System.out.println("Your weight on "+planetName+" is: "+venusCalculation+" pounds.");
}
}
}
}
Upvotes: 1
Views: 59
Reputation:
So I ended up fixing it and here is the result. import java.util.InputMismatchException; import java.util.Scanner;
public class WeightOnADifferentPlanet {
static Scanner scan = new Scanner ( System.in );
public static void main ( String[] args ){
System.out.println("What planet do you want to travela:?");
String planetName = scan.nextLine();
System.out.println("Please enter your weight:");
int weight = Integer();
//int weight = scan.nextInt();
double earthCalculation = weight * 1.0;
double jupiterCalculation = weight * (21.0 / 8.0); //check
double marsCalculation = weight * (3.0 / 8.0);
double mercuryCalculation = weight * (3.0 / 10.0);
double neptuneCalculation = weight * (11.0 / 10.0); //check
double plutoCalculation = weight * (7.0 / 10.0);
double saturnCalculation = weight * (6.0 / 5.0); //check
double uranusCalculation = weight * (9.0 / 10.0);
double venusCalculation = weight * (7.0 / 8.0);
if (planetName.equalsIgnoreCase("Earth"))
{
System.out.println("Your weight on "+planetName+" is: "+earthCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Jupiter"))
{
System.out.println("Your weight on "+planetName+" is: "+jupiterCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mars"))
{
System.out.println("Your weight on "+planetName+" is: "+marsCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mercury"))
{
System.out.println("Your weight on "+planetName+" is: "+mercuryCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Neptune"))
{
System.out.println("Your weight on "+planetName+" is: "+neptuneCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Pluto"))
{
System.out.println("Your weight on "+planetName+" is: "+plutoCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Saturn"))
{
System.out.println("Your weight on "+planetName+" is: "+saturnCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Uranus"))
{
System.out.println("Your weight on "+planetName+" is: "+uranusCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Venus"))
{
System.out.println("Your weight on "+planetName+" is: "+venusCalculation+" pounds.");
}
else {
System.out.println("Planet not recognized");
}
}
public static int Integer(){
while (true)
{
try
{
return scan.nextInt();
}
catch (InputMismatchException e)
{
scan.next();
System.out.print("That’s not an integer. Try again: ");
}
}
}
}
Upvotes: 0
Reputation: 1471
You don't really need to use exceptions. You can enter anything for planet and it won't crash, since you are checking for nextLine()
. For weight, just check if scan.hasNextInt()
before setting the weight.
import java.util.Scanner;
public class WeightOnADifferentPlanet {
public static void main ( String[] args ) {
Scanner scan = new Scanner(System.in);
System.out.print("Where do you want to travel? ");
String planetName = scan.nextLine();
System.out.print("Please enter your weight: ");
int weight = 0;
if (scan.hasNextInt()) {
weight = scan.nextInt();
double earthCalculation = weight * 1.0;
double jupiterCalculation = weight * (21.0 / 8.0); //check
double marsCalculation = weight * (3.0 / 8.0);
double mercuryCalculation = weight * (3.0 / 10.0);
double neptuneCalculation = weight * (11.0 / 10.0); //check
double plutoCalculation = weight * (7.0 / 10.0);
double saturnCalculation = weight * (6.0 / 5.0); //check
double uranusCalculation = weight * (9.0 / 10.0);
double venusCalculation = weight * (7.0 / 8.0);
if (planetName.equalsIgnoreCase("Earth")) {
System.out.println("Your weight on " + planetName + " is: " + earthCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Jupiter")) {
System.out.println("Your weight on " + planetName + " is: " + jupiterCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Mars")) {
System.out.println("Your weight on " + planetName + " is: " + marsCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Mercury")) {
System.out.println("Your weight on " + planetName + " is: " + mercuryCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Neptune")) {
System.out.println("Your weight on " + planetName + " is: " + neptuneCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Pluto")) {
System.out.println("Your weight on " + planetName + " is: " + plutoCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Saturn")) {
System.out.println("Your weight on " + planetName + " is: " + saturnCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Uranus")) {
System.out.println("Your weight on " + planetName + " is: " + uranusCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Venus")) {
System.out.println("Your weight on " + planetName + " is: " + venusCalculation + " pounds.");
} else {
System.out.println("Planet not recognized");
}
} else {
System.out.println("Invalid weight");
}
scan.close();
}
}
Upvotes: 0
Reputation: 4191
There are several things wrong with your program:
String planetName
needs to be declared outside of the try block.
You shouldn't have planetName = scan.nextLine();
in a try/catch. You should figure out a way to keep asking the user for a planet name until they get a proper one.
Also, the int weight
needs to be declared outside of the try block.
Same thing here, you need to figure out a way to keep asking the user for an integer if they don't give you one (e.g. you get an exception).
EDIT: As suggested by MasterBlaster, you should also close out your scanner with scan.close()
Upvotes: 0
Reputation: 13
I believe scan.nextInt();
will only scan an integer anyways, so there shouldn't be any need to catch non integers
Upvotes: 1