Its the basic convert feet and inches to centimeters... if user inputs neg numbe
ID: 3623856 • Letter: I
Question
Its the basic convert feet and inches to centimeters... if user inputs neg numbers or characters instead of numbers, throw and handle exceptions. I am not sure that I did it right, but it does work. Only thing is, I need it to repeat itself if the wrong input is entered for both feet and inches until the user enters the correct input.
Would offer more Karma, but I am about out of points.
Thanks
import java.util.*;
public class Ch11Ex1
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
double length_feet,length_inch,totalinches,centim;
double centi=2.54,inch=12;
String str;
{
try
{
System.out.print("Enter total feet: ");
length_feet = console.nextDouble();
if (length_feet < 0 )
throw new InputMismatchException ("Positive Number Required");
System.out.println();
System.out.print("Enter total inches: ");
length_inch = console.nextDouble();
if (length_inch < 0 )
throw new InputMismatchException ("Positive Number Required");
System.out.println();
totalinches = inch*length_feet+length_inch;
centim=centi*totalinches;
System.out.println("You have entered " + length_feet + " feet and " + length_inch + " inches." + " Equivalent in centimeters is " + centim + " centimeters" );
}
catch (Exception eRef)
{
if (eRef instanceof ArithmeticException)
System.out.println("Positive Numbers Only");
else if (eRef instanceof InputMismatchException)
System.out.println("Invalid Input!Please Enter Only Positive Numbers");
}
}
}
}
Explanation / Answer
fixed myself. Providing another solution if anyone needs to see:
//File One:Ch11Ex1
import java.util.*;
public class Ch11Ex1 {
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
double feet,inches;
boolean done = false;
double conversion = 0;
double conversionb = 0;
double centimeters = 2.54;
double inchesPerFoot = 12.0;
do
{
try
{
System.out.println("Please enter the number of feet and the number of inches separated by a space");
feet = console.nextDouble();
inches = console.nextDouble();
if(feet < 0 || inches < 0)
throw new NegativeNumberException();
conversion = (inches * centimeters)+(feet * inchesPerFoot * centimeters);
conversionb = (inchesPerFoot * feet) + (inches);
System.out.printf("Total Number of Inches: %.0f",conversionb);
System.out.printf(" Total Number of Centimeters: %.2f",conversion);
done = true;
//System.out.println();
}
catch(NegativeNumberException nne)
{
System.out.println(nne.toString());
console.nextLine();
}
catch(InputMismatchException ime)
{
System.out.println("Please Enter only Positive Numbers " + ime.toString());
console.nextLine();
}
}while(!done);
}
}
//File Two: NegativeNumberException
public class NegativeNumberException extends Exception{ public NegativeNumberException()
{
super("Cannot enter a negative value");
}
public NegativeNumberException(String strMessage)
{ super(strMessage); }
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.