Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You made lots of errors by not changing the code while copy pasting the logic from one place to the other. Your code can be improved a lot by reducing the repetitions and I will be more optimistic in my 'if' 'else' conditions to capture the right cases first and leaving all the wrong cases to the end...Below is the working version of your code modified slightly by fixing the typos and order of the logic.</p> <pre><code>import java.util.Scanner; public class UnitConversion3b { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String maxInputWarning = "\nError: Too many input characters." + "\nProgram is now terminating."; String lengthOrWeight; final double LENGTH_CONVERSION_FACTOR = 3.2808399; final double WEIGHT_CONVERSION_FACTOR = 2.20462; String whichWeightConversion = "empty", whichLengthConversion = "empty"; double feet = 0, meters = 0, pounds = 0, kilograms = 0; double metersConvertedToFeet, feetConvertedToMeters; double poundsConvertedToKilograms, kilogramsConvertedToPounds; System.out.println(""); System.out.print("What kind of value would you like to convert?"); System.out.print("\nEnter L for length, or W for weight: "); lengthOrWeight = keyboard.nextLine(); if (lengthOrWeight.length() &gt; 1) { System.out.println(maxInputWarning); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if ((!(lengthOrWeight.equalsIgnoreCase("l")) &amp;&amp; (!(lengthOrWeight .equalsIgnoreCase("w"))))) { System.out.println("\nError: Unrecognized conversion type." + "\nProgram is now terminating."); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if (lengthOrWeight.equalsIgnoreCase("l")) { System.out.println("\nConverting feet or meters?"); System.out.print("Enter F to convert feet, or M for meters: "); whichLengthConversion = keyboard.nextLine(); if (whichLengthConversion.length() &gt; 1) { System.out.println(maxInputWarning); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if ((!(whichLengthConversion.equalsIgnoreCase("f")) &amp;&amp; (!(whichLengthConversion .equalsIgnoreCase("m"))))) { System.out.println("\nError: Unrecognized unit of " + "measurement.\nProgram is now terminating."); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if (whichLengthConversion.equalsIgnoreCase("f")) { System.out.print("Enter the number of feet to" + " convert to meters: "); feet = keyboard.nextDouble(); feetConvertedToMeters = feet / LENGTH_CONVERSION_FACTOR; System.out.println(feet + " Feet in Meters is " + feetConvertedToMeters + "."); keyboard.nextLine(); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if (whichLengthConversion.equalsIgnoreCase("m")) { System.out.print("Enter the number of meters to" + " convert to feet: "); meters = keyboard.nextDouble(); metersConvertedToFeet = meters * LENGTH_CONVERSION_FACTOR; System.out.println(meters + " Meters in Feet is " + metersConvertedToFeet + "."); keyboard.nextLine(); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } } else { System.out.println("Converting pounds or kilograms?"); System.out.print("Enter P to convert pounds, or K for kilograms: "); whichWeightConversion = keyboard.nextLine(); if (whichWeightConversion.length() &gt; 1) { System.out.println(maxInputWarning); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if ((!(whichWeightConversion.equalsIgnoreCase("p")) &amp;&amp; (!(whichWeightConversion .equalsIgnoreCase("k"))))) { System.out.println("\nError: Unrecognized unit of " + "measurement.\nProgram is now terminating."); System.out.print("Press Enter to continue ... "); return; } else if (whichWeightConversion.equalsIgnoreCase("p")) { System.out.println("Enter the number of pounds to" + " convert to kilograms:"); pounds = keyboard.nextDouble(); poundsConvertedToKilograms = pounds / WEIGHT_CONVERSION_FACTOR; System.out.println(pounds + " Pounds in Kilograms is " + poundsConvertedToKilograms + "."); keyboard.nextLine(); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } else if (whichWeightConversion.equalsIgnoreCase("k")) { System.out.print("Enter the number of kilograms to" + " convert to pounds: "); kilograms = keyboard.nextDouble(); kilogramsConvertedToPounds = kilograms * WEIGHT_CONVERSION_FACTOR; System.out.println(kilograms + " Kilograms in Pounds is " + kilogramsConvertedToPounds + "."); keyboard.nextLine(); System.out.print("Press Enter to continue ... "); keyboard.nextLine(); return; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload