public class LengthConverter2 { /* This program will convert measurements expressed in inches, feet, yards, or miles into each of the possible units of measure. The measurement is input by the user, followed by the unit of measure. For example: "17 feet", "1 inch", "2.73 mi". Several measurements can be combined on one line of input. For example: "2 miles 5 yards 1 inch". In this case, the measurements are combined into one total. Abbreviations in, ft, yd, and mi are accepted. Negative measurements are not allowed. The program will continue to read and convert measurements until the user enters an empty line. */ public static void main(String[] args) { double measurement; // Numerical measurement, input by user. String units; // The unit of measure for the input, also // specified by the user. double inches, feet, yards, miles; // Measurement expressed in each // possible unit of measure. TextIO.putln("Enter measurements in inches, feet, yards, or miles."); TextIO.putln("For example: 1 inch 17 feet 2.73 miles"); TextIO.putln("You can use abbreviations: in ft yd mi"); TextIO.putln("I will convert your input into the other units of measure."); while (true) { /* Get the user's input, and convert it to inches. */ TextIO.putln(); TextIO.putln(); TextIO.putln("Enter your measurement, or press return to end:"); skipBlanks(); if (TextIO.peek() == '\n') // End if there is nothing on the line. break; inches = readMeasurement(); // If value is < 0, then user's input was illegal. if (inches >= 0) { /* Convert the measurement in inches to feet, yards, and miles. */ feet = inches / 12; yards = inches / 36; miles = inches / (12*5280); /* Output the measurement in terms of each unit of measure. */ TextIO.putln(); TextIO.putln("That's equivalent to:"); TextIO.put(inches, 15); TextIO.putln(" inches"); TextIO.put(feet, 15); TextIO.putln(" feet"); TextIO.put(yards, 15); TextIO.putln(" yards"); TextIO.put(miles, 15); TextIO.putln(" miles"); } // end else TextIO.getln(); // Discard the rest of the input line before // getting the next line. } // end while TextIO.putln(); TextIO.putln("OK! Bye for now."); } // end main() static void skipBlanks() { // Reads past any blanks and tabs in the input. // Postcondition: The next character in the input is an // end-of-line or a non-blank character. char ch; ch = TextIO.peek(); while (ch == ' ' || ch == '\t') { ch = TextIO.getAnyChar(); ch = TextIO.peek(); } } static double readMeasurement() { // Reads the user's input measurement from one line of input. // Preconditino: The input line is not empty. // Postcondition: If the user's input is legal, the measurement // is converted to inches and returned. If the // input is not legal, the value -1 is returned. // The end-of-line is NOT read by this routine. double inches; // Total number of inches in user's measurement. double measurement; // One measurement, such as the 12 in "12 miles" String units; // The units specified for the measurement, such as "miles" char ch; // Used to peek at next character in the user's input. inches = 0; // No inches have yet been read. skipBlanks(); ch = TextIO.peek(); /* As long as there is more input on the line, read a measurement and add the equivalent number of inches to the variable, inches. If an error is detected during the loop, end the subroutine immediately by returning -1. */ while (ch != '\n') { /* Get the next measurement and the units. Before reading anything, make sure that a legal value is there to read. */ if ( ! Character.isDigit(ch) ) { TextIO.putln("Error: Expected to find a number, but found " + ch); return -1; } measurement = TextIO.getDouble(); skipBlanks(); if (TextIO.peek() == '\n') { TextIO.putln("Error: Missing unit of measure at end of line."); return -1; } units = TextIO.getWord(); units = units.toLowerCase(); /* Convert the measurement to inches and add it to the total. */ if (units.equals("inch") || units.equals("inches") || units.equals("in")) inches += measurement; else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) inches += measurement * 12; else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) inches += measurement * 36; else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) inches += measurement * 12 * 5280; else { TextIO.putln("Error: \"" + units + "\" is not a legal unit of measure."); return -1; } /* Look ahead to see whether the next thing on the line is the end-of-line. */ skipBlanks(); ch = TextIO.peek(); } // end while return inches; } // end readMeasurement() } // end class LengthConverter