Shiv Patel
Shiv Patel

Reputation: 141

loop validation until user selects correct option, if correct option is not selected keep on looping

Need to be able to loop if user selects one of the two options it should terminate, if user selects invalid option keep looping until one of the options is selected

import java.util.Scanner;
    public class conversion {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.println("Which conversion do you want?");
            System.out.println("Pounds to Kilograms(enter 1)");
            System.out.println("Kilograms to Pounds(enter 2)");
            double kilograms, pounds;
            int choice = input.nextInt();

            while(){

                if(choice == 1){
                    System.out.println("What weight value do you want converted?");
                    pounds = input.nextDouble();
                    kilograms = pounds / 2.20462;
                    kilograms = Math.round(kilograms * 100.0) / 100.0;
                    System.out.println( +pounds+ " pounds is equal to " +kilograms+ " kilograms");
                }

                else if(choice == 2){
                System.out.println("What weight value do you want converted?");
                    kilograms = input.nextDouble();
                    pounds = kilograms/0.453592;
                    pounds = Math.round(pounds * 100.0) / 100.0;
                    System.out.println( +kilograms+ " kilograms is equal to " +pounds+ " pounds");
                    }
                else if((choice != 1) || (choice != 2)){
                    System.out.println("Invalid please try again");
                    System.out.println("Which conversion do you want?");
                    System.out.println("Pounds to Kilograms(enter 1)");
                    System.out.println("Kilograms to Pounds(enter 2)");
                    choice = input.nextInt();
                }


            }

        }
    }

Display the menu and ask the user which conversion they want:

Which conversion do you want?

Then ask the user for what weight value should be converted.

What weight value do you want converted?

If the user chooses 1, then convert the pounds to Kilograms, else if the user chooses 2 convert the kilograms to pounds, else if the user choice is anything else, tell the user their answer is invalid and request another answer.

Upvotes: 0

Views: 796

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191844

Use while (true). Put a break; when you want to exit the loop

Put int choice = input.nextInt(); at the top, inside of the loop instead of at the end too.

Can also just use else to capture all possible invalid input.


double kilograms, pounds;
while (true) {

        System.out.println("Which conversion do you want?");
        System.out.println("Pounds to Kilograms(enter 1)");
        System.out.println("Kilograms to Pounds(enter 2)");

        int choice = input.nextInt();

       if (choice == 1) {
            // okay 
            lbsToKg(); // TODO: implement
            break; // stops the loop 
        } else {  
            // invalid, will already continue the loop 
        } 

Upvotes: 1

Related Questions