1

I need to allow the user to input what they want into the program but restricted to the three options of, R, T and Q. I have tried to do this several times and ended up going back on myself several times. I came up with this solution but now when I run the code it simply loops round no matter what I enter into the console. Any help would be greatly appreciated, thank you.

public class Draw {

public static void main(String[] args) {

String[] options = {"R","T","Q"};
String choice;
List<String> list = Arrays.asList(options);
    Scanner scanner = new Scanner (System.in);
do {
        System.out.println("Press 'R' to start drawing a Rectangle.");
        System.out.println("Press 'T' to start drawing a Triangle.");
        System.out.println("Press 'Q' to stop.");

        System.out.print("Enter one of the options: "); 
        choice = scanner.next();
        if (options.equals("R")) {
            System.out.println("You have selected to draw a Rectangle!");
            System.out.println("Please enter the Height and Width of the rectangle that is within 30cm - 90cm: ");

        }
        else if (options.equals("T")) {
            System.out.println("You have selected to draw a Tringle! Enter the three angles of the triangle between 30cm and 90cm: ");

        }
            else if (options.equals("Q")) {
                System.out.println("Thank you for drawing with the finch, good bye.");
            }
            else {
                System.out.println("Invalid input, try again. Enter with a capital letter one of the three options.");
            }
}
while(!list.contains(choice.toLowerCase()));

}

Daniel
  • 11
  • 2
  • You are checking `options.equals`, not `choice.equals`. Also, what's the guard condition on the loop meant to do? There are no lowercase letters in `options` - why not simply `while (true)`? – Andy Turner Feb 25 '16 at 16:07

2 Answers2

3

Try to write the if condition with using choice instead of options like this:

 if ("R".equals(choice)) {
 ...
 else if ("T".equals(choice)) {
 ...
 else if ("Q".equals(choice)) {

And remove the method toLowerCase from while loop it's unnecessary:

while(!list.contains(choice));  
Abdelhak
  • 8,161
  • 4
  • 19
  • 34
2

Your conditions are checking the wrong variable:

choice = scanner.next(); 
if (options.equals("R")) {

should be

choice = scanner.next(); 
if (choice.equals("R")) {
Andy Turner
  • 122,430
  • 10
  • 138
  • 216