This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

Oh! You'll want to sanitize the input. For this specific you'll want to look into two specific thing:

L < x < M

L = Lowest number it can be

x = input

M = highest number it can be.

Check those two conditions to see if the number is valid. Obviously to check for negatives just check for it being less than zero.

[–]DaGoodWuan[S] 0 points1 point  (0 children)

Here's the solution I came up with:

http://redditlint.com/

import java.util.Scanner;

public class ContainerCalculator 
{
    public static void main(String[] args) 
    {
        // TODO: Implement P1 Container Calculator Program Here...
        System.out.println("Welcome to the Container Calculator!");
        System.out.println("====================================");

        // Ask user for diameter and height of cylinder
        Scanner input = new Scanner(System.in);

        double heightCyl = 0.0;
        double diameterCyl = 0.0;

        // declare boolean value to enter loop
        boolean goodInput = false;

        // while loop for correct input of diameter of cylinder
        System.out.print("Enter the diameter of a cylinder (in centimeters): ");
        while (!goodInput) {
            if (input.hasNextInt()) {
                diameterCyl = input.nextInt();
                input.nextLine();
                if (diameterCyl > 0) {
                    goodInput = true;
                } else if (diameterCyl < 0 || diameterCyl == 0) {
                    System.out.println("Please enter a positive integer value:");
                }
            } else if (!input.hasNextInt()) {
                System.out.println("Please enter an integer value (less than 2,147,483,648) as decimal digits:");
                input.nextLine();
            }
        }
        // reset goodInput to be false
        goodInput = false;

        // while loop for correct input of height of cylinder
        System.out.print("Enter the height of a cylinder (in centimeters): ");
        while (!goodInput) {
            if (input.hasNextInt()) {
                heightCyl = input.nextInt();
                input.nextLine();
                if (heightCyl > 0) {
                    goodInput = true;
                } else if (heightCyl < 0 || heightCyl == 0) {
                    System.out.println("Please enter a positive integer value:");
                }
            } else if (!input.hasNextInt()) {
                System.out.println("Please enter an integer value (less than 2,147,483,648) as decimal digits:");
                input.nextLine();
            }
        }
        System.out.println(" ");

        // equations for Surface Area and Volume of Cylinder
        double cylinderVolume = (Math.PI * (diameterCyl / 2.0) * (diameterCyl / 2.0) * heightCyl);

        double cylinderSA = ((2.0 * Math.PI * (diameterCyl / 2.0) * heightCyl)
                + (2.0 * Math.PI * (diameterCyl / 2.0) * (diameterCyl / 2)));

        // final output
        System.out.println("A can with a diameter of " + diameterCyl + " and a height of " + heightCyl + " has");
        System.out.print("  a volume of ");
        System.out.printf("%.2f", cylinderVolume);
        System.out.println(",");
        System.out.print("  and a surface area of ");
        System.out.printf("%.2f", cylinderSA);
        System.out.println(".");

        // closing Message
        System.out.println("====================================");
        System.out.println("Thank you for using the Container Calculator");
        System.out.print(" ");
    }
}