mardi 4 septembre 2018

Why does my code have a compilation error after the else statement?

Bear with me.

After creating the if-else statement, every time I check to see the else statement if it can go back to the menu if the string isn't true it always ends up in an error like:Exception in thread "main" java.lang.NumberFormatException: For input string: ""

public class geo {
public static void main(String[] args){
byte choice = 0;
int num1 = 0;
int num2 = 0;
int num3  = 0;
float num4 ;
double result1 = 0;
boolean quit;
String UnitofMeasurement;
String feet = "feet";
String inches = "inches";
DecimalFormat format = new DecimalFormat("0.00");
Scanner key = new Scanner(System.in);
while (choice != 1) {
System.out.println("\t1. Determine the perimeter of a square");
switch (choice){
  case 1:
  System.out.println("The perimeter of a square is computed 
  by multiplying the measure of one side by 4.");
            System.out.println("Enter the unit of measurement(i.e. inches/feet):");
            UnitofMeasurement = key.next();// gets the unit of measurement
            if(UnitofMeasurement.equals(feet)||UnitofMeasurement.equals(inches)){
            System.out.println("You have chosen " + UnitofMeasurement + " as the unit of measurement.");
            System.out.println("Enter the measurement of one side: ");
            num1 = key.nextInt();
            System.out.println("Side of the square: "+ format.format(num1));
            key.nextLine();
            result1 = num1 * 4;
            System.out.println("Perimeter of the square = "+ format.format(result1) +" "+  UnitofMeasurement);
            }
            else
                {
                    System.out.println("Please only enter feet/inches");

                }
            enter();
            break;

default;

Aucun commentaire:

Enregistrer un commentaire