user2817128
user2817128

Reputation: 21

How to keep requesting user to select a valid option?

So, the user has to choose a number between 1 and 3. Otherwise, they're told to try again. If the user tries a number less than 1 or greater than 3, whatever number they chose gets stored in the "choice" variable and causes the program to continue to run when it should just stop. I assumed there would be an easy solution, but apparently it's beyond me as a beginner. The obvious thing to me would be to somehow clear or empty the value that has been assigned to "choice" after the unsuccessful user input. Is that possible?

import java.util.Scanner;

public class Furniture2Test  {

    public static void main(String[] args) {

        wood();

    } // end main

    public static void wood() {

        int choice;

        int pine = 1;
        int oak = 2;
        int mahogany = 3;

        int pineCost = 100;
        int oakCost = 225;
        int mahoganyCost = 310;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What type of table would you like?");
        System.out.println("1. pine");
        System.out.println("2. oak");
        System.out.println("3. mahogany");

        choice = keyboard.nextInt();

        if (choice == 1) {
            choice = pineCost;
        } else if (choice == 2) {
            choice = oakCost;
        } else if (choice == 3) {
            choice = mahoganyCost;
        } else if (choice > 3 || choice < 1) {
            System.out.println("Try again.");
            choice = -1;
            wood();
        }

        System.out.println("That will be $" + choice + ".");

        size(choice);

    } // end wood

    public static void size(int choice) {

        int sizeChoice;
        int large = 35;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What size will that be?");
        System.out.println("1. large");
        System.out.println("2. small");

        sizeChoice = keyboard.nextInt();

        if (sizeChoice == 1)
            System.out.println("That will be $" + (choice + large) + ".");
        else if (sizeChoice == 2)
            System.out.println("That will be $" + choice);
        else
            System.out.println("Please, enter either a 1 or a 2.");

    } // end size

}

Upvotes: 2

Views: 22786

Answers (5)

SachinSarawgi
SachinSarawgi

Reputation: 2692

Your requirement can be done easily with do...while loop. Sample code is as follows:

do{
    System.out.println("Choose option between 1 and 3");
    choice = keyboard.nextInt();
}while(!(choice > 3 || choice < 1));

if (choice == 1) {
    choice = pineCost;
} else if (choice == 2) {
    choice = oakCost;
} else if (choice == 3) {
    choice = mahoganyCost;
}

Hope this helps.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347314

choice is a local variable to the method wood, you are making a recursive call to wood when the user makes a wrong choice. This is an interesting design choice and probably not the best in this case.

When you call wood again, choice is rest (in this to unknown value until it is assigned value from the user).

Now the problem occurs when the wood method exists...each time it returns to the caller, it will call size(choice), where choice is -1 (because that's what you set it to before calling wood again).

  1. You should be using a while-loop instead of recursive calls
  2. You should never call size(choice) with anything other then a valid choice

Take a look at The while and do-while statement for more details

Upvotes: 0

Kaushik Sivakumar
Kaushik Sivakumar

Reputation: 205

whatever number they chose gets stored in the "choice" variable and causes the program to continue to run when it should just stop//

the program is contining to run because you are calling wood() if(choice > 3 || choice < 1)

if you want it to stop remove the wood() call

if you also want to clear the value for choice(instead of -1) you can assign it to null

Upvotes: 0

necromancer
necromancer

Reputation: 24641

import java.util.Scanner;

public class Furniture2Test

{

   public static void main(String[] args)
   {

      wood();

   } // end main


   public static void wood()
   {

      int choice;

      int pine = 1;
      int oak = 2;
      int mahogany = 3;

      int pineCost = 100;
      int oakCost = 225;
      int mahoganyCost = 310;

      Scanner keyboard = new Scanner(System.in);

      System.out.println("What type of table would you like?");
      System.out.println("1. pine");
      System.out.println("2. oak");
      System.out.println("3. mahogany");

      choice = read_range(keyboard, 1, 3);

      if(choice == 1)
      {
         choice = pineCost;
      }
      else
         if(choice == 2)
         {
            choice = oakCost;
         }
         else
            if(choice == 3)
            {
               choice = mahoganyCost;
            }
            else
               if(choice > 3 || choice < 1)
               {
                  System.out.println("Try again.");
                  choice = -1;
                  wood();
               }

      System.out.println("That will be $" + choice + ".");

      size(choice);


   }  // end wood

   public static void size(int choice)
   {

      int sizeChoice;
      int large = 35; 

      Scanner keyboard = new Scanner(System.in);

      System.out.println("What size will that be?");
      System.out.println("1. large");
      System.out.println("2. small");

      sizeChoice = read_range(keyboard, 1, 2);

      if(sizeChoice == 1)
         System.out.println("That will be $" + (choice + large) + ".");
      else
         if(sizeChoice == 2)
            System.out.println("That will be $" + choice);
         else
            System.out.println("Please, enter either a 1 or a 2.");

   } // end size

   private static int read_range (Scanner scanner, int low, int high) {
     int value;
     value = scanner.nextInt();
     while (value < low || value > high) {
       System.out.print("Please enter a value between " + low + " and " + high + ": ");
       value = scanner.nextInt();
     }
     return value;
   }



} // end class

Upvotes: 0

nhgrif
nhgrif

Reputation: 62062

//put the menu logic
while(choice > 3 || choice < 1) {
    //put your try again logic.
}
//can only exit the while loop if the number is 1, 2, or 3, so put your output statement down here after the while loop

Upvotes: 0

Related Questions