I need help fixing this code. (Java) import java.util.Scanner; public class UnitConverter { public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean done = false; double factor1 = 1; double factor2 = 1; String unit1 = ""; String unit2 = ""; while (!done) { boolean getSecond = true; String command = in.next(); System.out.println("From unit (in, cm, m, again, quit): " + command); if (command.equals("in")) { factor1 = 2.54; // Convert to cm unit1 = command; } else if (command.equals("cm")) { factor1 = 1; unit1 = command; } else if (command.equals("m")) { factor1 = 100; unit1 = command; } else if (command.equals("again")) { getSecond = false; } else if (command.equals("quit")) { done = true; getSecond = false; } else { System.out.println("Sorry, unknown unit.\n"); getSecond = false; } if (getSecond) { System.out.print("To unit: "); unit2 = in.next(); if (unit2.equals("in")) { factor2 = 2.54; // Convert from cm } else if(unit2. equals(" cm")){ factor2 = 1; } else if(unit2.equals("m")){ factor2 = 100; } else{ System.out.println("Sorry, unknown unit."); getSecond = false; } } if(getSecond) { double value = in.nextDouble(); double result = value * factor1 / factor2; System.out.println(value + " " + unit1 + " = " + result + " " + unit2 + "\n"); } } } }
I need help fixing this code. (Java)
import java.util.Scanner;
public class UnitConverter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean done = false;
double factor1 = 1;
double factor2 = 1;
String unit1 = "";
String unit2 = "";
while (!done)
{
boolean getSecond = true;
String command = in.next();
System.out.println("From unit (in, cm, m, again, quit): " + command);
if (command.equals("in"))
{
factor1 = 2.54; // Convert to cm
unit1 = command;
}
else if (command.equals("cm"))
{
factor1 = 1;
unit1 = command;
}
else if (command.equals("m"))
{
factor1 = 100;
unit1 = command;
}
else if (command.equals("again"))
{
getSecond = false;
}
else if (command.equals("quit"))
{
done = true;
getSecond = false;
}
else
{
System.out.println("Sorry, unknown unit.\n");
getSecond = false;
}
if (getSecond)
{
System.out.print("To unit: ");
unit2 = in.next();
if (unit2.equals("in"))
{
factor2 = 2.54; // Convert from cm
}
else if(unit2. equals(" cm")){
factor2 = 1;
}
else if(unit2.equals("m")){
factor2 = 100;
}
else{
System.out.println("Sorry, unknown unit.");
getSecond = false;
}
}
if(getSecond)
{
double value = in.nextDouble();
double result = value * factor1 / factor2;
System.out.println(value + " " + unit1 + " = " + result + " " + unit2 + "\n");
}
}
}
}
![The image shows a comparison between sets of expected and actual outputs in a unit conversion task.
### Comparison 1:
**Output differs. See highlights below.**
- **Input**:
```
cm in 10 quit
```
- **Your output**:
```
From unit (in, cm, m, again, quit): cm
To unit: 10.0 cm = 3.937007874015748 in
From unit (in, cm, m, again, quit): quit
```
- **Expected output**:
```
From unit (in, cm, m, again, quit): cm
To unit: in↵
10.0 cm = 3.937007874015748 in
From unit (in, cm, m, again, quit): quit
```
### Comparison 2:
**Output differs. See highlights below.**
- **Input**:
```
cm in 10 again cm in 20 quit
```
- **Your output**:
```
From unit (in, cm, m, again, quit): cm
To unit: 10.0 cm = 3.937007874015748 in
From unit (in, cm, m, again, quit): again
From unit (in, cm, m, again, quit): cm
To unit: 20.0 cm = 7.874015748031496 in
From unit (in, cm, m, again, quit): quit
```
- **Expected output**:
*The expected output is not provided explicitly, but it can be inferred from the description of the task that it involves similar unit conversion processes.*
**Explanations for Discrepancies:**
In both comparisons, the discrepancy appears to arise from minor formatting differences in the output text, such as the inclusion of a return character in the expected output.
This analysis highlights the importance of precise formatting in automated text outputs, especially in educational or computational tools.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb4d4e6a3-fd83-4270-b000-89a89a72868b%2Fb6dad018-cb31-47e0-bc13-88b8b8956236%2F7wnpbsf_processed.png&w=3840&q=75)
![## Analysis of Output Differences in Unit Conversion Program
In this educational resource, we are examining the differences in outputs for a unit conversion program. The program is designed to convert units such as inches (in), centimeters (cm), meters (m), and feet (ft). Below are two instances where the output differed from the expected result.
### Case 3: Output Comparison
**Input:**
```
cm ft cm m 100 quit
```
**Your Output:**
```
From unit (in, cm, m, again, quit): cm
To unit: Sorry, unknown unit.
From unit (in, cm, m, again, quit): cm
To unit: 100.0 cm = 1.0 m
From unit (in, cm, m, again, quit): quit
```
**Expected Output:**
```
From unit (in, cm, m, again, quit): cm
To unit: ft
Sorry, unknown unit.
From unit (in, cm, m, again, quit): cm
To unit: m
100.0 cm = 1.0 m
From unit (in, cm, m, again, quit): quit
```
**Explanation:**
The expected output shows that the program should first receive an attempt to convert from centimeters to feet, resulting in a 'Sorry, unknown unit' message since 'ft' was not recognized by the program initially. Then, the correct conversion from centimeters to meters occurs.
### Case 4: Output Comparison
**Input:**
```
ft m cm 10 quit
```
**Your Output:**
```
From unit (in, cm, m, again, quit): ft
To unit: Sorry, unknown unit.
```
### Key Observations:
- The input specifies a sequence of conversions ending in 'quit'.
- The program needs to handle unknown units gracefully and proceed to valid conversions.
- The expected output handles the conversion attempts and correctly identifies valid unit conversions.
These cases illustrate the importance of clear error handling and proper recognition of supported units in programming calculators or converters. Understanding and addressing these discrepancies ensures a program functions correctly and efficiently provides the intended service.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb4d4e6a3-fd83-4270-b000-89a89a72868b%2Fb6dad018-cb31-47e0-bc13-88b8b8956236%2F1yjcw1k_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 7 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)