0

I'm working on a program that calculates the area of either a circle (C), square (S), or rectangle (R), depending on what letter the user inputs. I've tested it and it works fine; the code is below:

import java.util.Scanner;

public class TestLoops {
public static void main(String[] args) {
   Scanner input = new Scanner(System.in);

    System.out.println("What is your shape? Enter C for circle, S for " + 
            "square, R for rectangle, or X to exit: ");
    String Shape = input.nextLine();

    if (Shape.equals("C")) {
        System.out.println("What is your circle's radius?: ");
        double Radius = input.nextDouble();
        double cFormula = (3.14 * Radius * Radius);
        System.out.println("Your circle's area = " + cFormula); 
    }
    else if (Shape.equals("S")) {
        System.out.println("What is the length of your shape's sides?: ");
        double Side = input.nextDouble();
        double sFormula = (Side * Side);
        System.out.println("Your square's area = " + sFormula);           
    }
    else if (Shape.equals("R")) {
        System.out.println("What is your rectangle's height?: ");
        double Height = input.nextDouble();
        System.out.println("What is your rectangle's width?: ");
        double Width = input.nextDouble();
        double rFormula = (Height * Width);
        System.out.println("Your rectangle's area = " + rFormula);
    }

   }

  }

Now, what I want to do is add a loop to the program. For example, if the user inputs C for circle and puts in the number 22 for the radius, they'll get an answer, but I want the program to loop back to the beginning again so that it asks the user "What is your shape?...". Also, if the user types in X instead of C, S, or R, I want the program to quit, but I'm not sure how to add that in, either.

I know that I need to add a 'while' loop, but I was hoping someone could point me in the right direction, because I don't know where to insert that part of the code. Do I add the 'while' loop somewhere at the beginning of the code, after the last "if else" statement, or... Also, I'm not actually sure what to type. Should it be something like,

while (Shape == C, S, R) {
   ....?

Any help or pointers would be appreciated by any one in the coding community! I will continue to work on this code on my own as well.

hoshicchi
  • 77
  • 1
  • 6
  • Two things. If you want the user to be asked again after a successful shape process is done, you put everything in a loop. If you want to validate input, you put another loop inside and make it run while input is not c or s or r. Unfortunately I can give examples in Python only (or pseudo code) – Daniel Springer Feb 19 '17 at 00:19
  • a good start would be to identify the lines in your existing code that shall be repeated and put them into a method of their own and call that from `main`. In the second step you can wrap that single line in in main calling that new method with the loop. – Timothy Truckle Feb 19 '17 at 00:21
  • `while (Arrays.asList("C", "S", "R").contains(Shape))`. You can build `Arrays.asList("C", "S", "R")` once, and reuse it. – Andy Turner Feb 19 '17 at 00:24

1 Answers1

1

I would go for the do, while

So, the program will always do something while the conditions that are set are being accomplished, so you want your program to look something like:

public class TestLoops {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean thisB = false; /*this is the guy who will tell the loop to stop the execution when the user inserts X*/
        String shape;
        do{
            System.out.println("What is your shape? Enter C for circle, S for " +
                    "square, R for rectangle, or X to exit: ");
            shape = input.next();

            if(shape.equalsIgnoreCase("C") || shape.equalsIgnoreCase("S") || shape.equalsIgnoreCase("R")) {
                if (shape.equals("C")) {
                    System.out.println("What is your circle's radius?: ");
                    double Radius = input.nextDouble();
                    double cFormula = (3.14 * Radius * Radius);
                    System.out.println("Your circle's area = " + cFormula);
                } else if (shape.equals("S")) {
                    System.out.println("What is the length of your shape's sides?: ");
                    double Side = input.nextDouble();
                    double sFormula = (Side * Side);
                    System.out.println("Your square's area = " + sFormula);
                } else if (shape.equals("R")) {
                    System.out.println("What is your rectangle's height?: ");
                    double Height = input.nextDouble();
                    System.out.println("What is your rectangle's width?: ");
                    double Width = input.nextDouble();
                    double rFormula = (Height * Width);
                    System.out.println("Your rectangle's area = " + rFormula);
                }
            }
            else if (shape.equalsIgnoreCase("X")) thisB = true;/*or in other words: stop*/
        }
        while(!thisB);
    }

}

Things to consider:

1) Naming conventions, always start variable names with undercase using camelCase, in your example shape started with UpperCase

2) When in a while loop, use only next(), not nextLine() to pick up the values as the latter will duplicate the question in the System.out.Println.

3) The optimum way to do this is to put all your if clauses in a method and call it with the parameter from the Scanner input. Even better would be having a method per shape, as things can get hairy depending on requests

Steven
  • 1,127
  • 1
  • 10
  • 31
  • Thank you so much for your input! I find that it made the most sense of all the answers because it was closest to the level of coding I'm doing at the moment. I would like to ask, what does thisB mean, however? I'm guessing that the name you're chosen for the boolean variable? – hoshicchi Feb 19 '17 at 00:53
  • Yes, that was just me being lazy, it's short for thisBoolean, but you should call it control or something that makes sense. Glad I could help, if you believe the answer fit, please upvote and accept it :) – Steven Feb 19 '17 at 00:55
  • Will do! I'm going to be adjusting my code and changing a few things like the point you made about changing variables to lowercase letters. I don't mean to eat up too much of your time, but would it be all right with you if I shoot you another question or two if I run into an issue? I feel like I'm on the cusp of where I need to be and I appreciate how you've cleared up somethings I've been wondering about. – hoshicchi Feb 19 '17 at 00:58
  • Sure! Fire any question, I might fall asleep in not too long as it's pretty late uk time, but while awake I will try to help – Steven Feb 19 '17 at 01:00
  • I've been looking over the code/testing it and it works like a dream. I have no questions, so my next step is to just study the steps you've added in so I can understand them for use next time. My textbook and instructor also mentioned Booleans during our lectures, so I appreciate you using familiar beginner concepts. You're awesome and I appreciate your time, help, and patience! – hoshicchi Feb 19 '17 at 01:04
  • Great! Happy coding now! :) – Steven Feb 19 '17 at 01:07