r/programminghelp • u/Carrotsandstuff • Jun 12 '20
Java User input validation help
I've been tearing my hair out about how to make this assignment work. I am provided with code that takes two doubles, wallHeight and wallWidth, and multiplies them to get wallArea. My assignment is to use do/while loops that will continue to prompt the user for input if an invalid input is entered. I had no problem sorting out how to not let people input negative numbers. We are supposed to ALSO be learning how to use try/catch blocks to deal with when users attempt to input non-numbers.
I simply can't figure out how to use the two in conjunction. This is the functional do/while block that will continue to prompt if you don't enter positive numbers.
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
do {
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
} while (wallHeight <= 0);
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
do {
System.out.println("Enter wall width (feet): ");
wallWidth = scnr.nextDouble();
} while (wallWidth <= 0);
// Calculate and output wall area
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");
}
}My best attempt at try/catch blocks AT BEST simply stops the code because without the break statements it goes infinite, and it looks like this:
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
do {
try {
System.out.println("Enter wall height (feet): ");
wallHeight = scnr.nextDouble();
}
catch(Exception excpt){
System.out.println("Invalid input.");
break;
}
} while (wallHeight <= 0);
// Implement a do-while loop to ensure input is valid
// Prompt user to input wall's width
do {
try {
System.out.println("Enter wall width (feet): ");
wallWidth = scnr.nextDouble();
}
catch(Exception excpt) {
System.out.println("Invalid input.");
break;
}
} while (wallWidth <= 0);
// Calculate and output wall area
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");
}
}I'm sure this is a mess, but I feel WOEFULLY unprepared for this. I'd appreciate any pointers in the correct direction.
1
u/buddha777353 Jul 12 '20
I feel like I'm a little late to the party on this one. instead of breaking you should be clearing out the scanner input and looking for the next option.
See bellow.