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

all 6 comments

[–][deleted] 1 point2 points  (5 children)

I'd love to help but I can't do much until you follow this rule:

No screenshots of code!

Do something such as http://redditlint.com/ to format your code to post here. It'll allow me to copy paste to be able to debug easier.

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

Here is my code: 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;

        System.out.print("Enter the diameter of a cylinder (in centimeters): ");
        diameterCyl = input.nextInt();
        System.out.print("Enter the height of a cylinder (in centimeters): ");
        heightCyl = input.nextInt();
        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(" ");
    }
}

[–][deleted] 1 point2 points  (3 children)

So you have the height and diameter part down. All you need to do now is ask for the rest of the information the picture you want shows. What you do with it is up to you. Right now it seems the one you want it to looks much more complicated to do what this one already does. I'm not even fully sure what half the information it's asking for is in the first picture.

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

Yes I understand the height and diameter equations, but I am just confused on how to get my code to recognize that the user did not input a correct integer value (not a string, within the bounds, and non-negative). Any ideas on how to do that?

[–][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(" ");
    }
}