Reputation: 11
here is my code
import java.util.Scanner;
public class Paint1 {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0;
double wallWidth = 0.0;
double wallArea = 0.0;
double gallonsPaintNeeded = 0.0;
final double squareFeetPerGallons = 350.0;
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's height
try {
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
if (wallHeight <= 0){
throw new Exception("Invalid number" );
}
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
System.out.println("Enter wall width (feet): ");
wallWidth = scnr.nextDouble();
if (wallWidth <= 0) {
throw new Exception("invalid number");
}
// Calculate and output wall area
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + wallArea + " square feet");
}
catch (Exception excpt) {
System.out.println(excpt.getMessage());
System.out.println("Cannot compute wall area");
}
// Calculate and output the amount of paint (in gallons) needed to paint the wall
gallonsPaintNeeded = wallArea/squareFeetPerGallons;
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
my code works fine for the first sets of input which are 30, and 25. I dont know how to make my code continue after the next input is "thirty" and 25. the program just stops, after giving me null but it needs to continue after "thirty". does anyone have any idea how to help me out.
Upvotes: 0
Views: 131
Reputation: 19565
You need to check if the scanner has appropriate token hasNextDouble
before calling nextDouble
and swallow incorrect input like this:
while (!scnr.hasNextDouble()) {
scnr.next(); // skip "not a number" token
}
wallHeight = scnr.nextDouble();
Then invalid "non-double" input will be quietly discarded until a number is entered and after that you validate if it's correct (greater than 0).
Similarly you wait until a valid number is entered and just output the message that the parameter is invalid.
As soon as both input values are valid, you calculate necessary outputs and exit without handling exceptions (if any occurs it will be rethrown).
Full code:
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0;
double wallWidth = 0.0;
double wallArea = 0.0;
double gallonsPaintNeeded = 0.0;
final double squareFeetPerGallons = 350.0;
// loop for height
while (wallHeight <= 0.0) {
System.out.print("Enter wall height (feet): ");
while (!scnr.hasNextDouble()) {
scnr.next();
}
wallHeight = scnr.nextDouble();
if (wallHeight <= 0){
System.out.println("Invalid wallHeight");
}
}
// loop for width
while (wallWidth <= 0) {
System.out.print("Enter wall width (feet): ");
while (!scnr.hasNextDouble()) {
scnr.next();
}
wallWidth = scnr.nextDouble();
if (wallWidth <= 0) {
System.out.println("invalid wallWidth");
}
}
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + wallArea + " square feet");
// Calculate and output the amount of paint (in gallons) needed to paint the wall
gallonsPaintNeeded = wallArea/squareFeetPerGallons;
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
}
Example output
Enter wall height (feet): twelve
-20
Invalid wallHeight
Enter wall height (feet): 20
Enter wall width (feet): no
0
invalid wallWidth
Enter wall width (feet): 15
Wall area: 300.0 square feet
Paint needed: 0.8571428571428571 gallons
Upvotes: 1
Reputation: 153
You are providing a string value to a double.
So thats the reason you are getting the error.
You can modified the code to -
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double wallHeight = 0.0;
double wallWidth = 0.0;
double wallArea = 0.0;
double gallonsPaintNeeded = 0.0;
final double squareFeetPerGallons = 350.0;
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's height
try {
while (wallHeight <= 0)
wallHeight = getNumber(scnr, "Enter wall height (feet): ");
while (wallWidth <= 0)
wallWidth = getNumber(scnr, "Enter wall width (feet): ");
// Calculate and output wall area
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + wallArea + " square feet");
} catch (Exception excpt) {
System.out.println(excpt.getMessage());
System.out.println("Cannot compute wall area");
}
// Calculate and output the amount of paint (in gallons) needed to paint the wall
gallonsPaintNeeded = wallArea / squareFeetPerGallons;
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
}
private static double getNumber(Scanner scnr, String message) throws Exception {
double number = 0.0;
try {
boolean isValidNumber = false;
while (!isValidNumber) {
System.out.println(message);
String value = scnr.next();
number = Double.parseDouble(value);
isValidNumber = true;
}
} catch (Exception e) {
System.out.println("Value entered is not correct.");
return -1;
}
return number;
}
Upvotes: 0
Reputation: 337
I assume you want your application to keep running and prompt the user for the same inputs again? This can be accomplished with a while loop that just continues to evaluate the code you've already written. Something like this should do:
import java.util.Scanner;
public class Stack {
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args) {
while(true) {
computePaint();
}
}
private static void computePaint() {
double wallHeight = 0.0;
double wallWidth = 0.0;
double wallArea = 0.0;
double gallonsPaintNeeded = 0.0;
final double squareFeetPerGallons = 350.0;
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's height
try {
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
if (wallHeight <= 0) {
throw new Exception("Invalid number");
}
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
System.out.println("Enter wall width (feet): ");
wallWidth = scnr.nextDouble();
if (wallWidth <= 0) {
throw new Exception("invalid number");
}
// Calculate and output wall area
wallArea = wallHeight * wallWidth;
System.out.println("Wall area: " + wallArea + " square feet");
} catch (Exception excpt) {
System.out.println(excpt.getMessage());
System.out.println("Cannot compute wall area");
}
// Calculate and output the amount of paint (in gallons) needed to paint the wall
gallonsPaintNeeded = wallArea / squareFeetPerGallons;
System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
}
}
Really there should be something to interrupt this execution if necessary, but I think while(true)
illustrates the point.
Upvotes: 0