Ryan Ryan - 1 month ago 6
Java Question

How can I create a loop for user input (until the user enters valid input)?

My program should ask user to enter the amount they want to withdraw from their account and calculating the current balance after withdrawing.
The requirement for withdrawing is minimum 100 and maximum 1000. If the user enters wrong input the program should re-promt and ask user to enter amount again. This process will keep repeating until the user puts right amount.
Once the right amount is choosen it should calculate and display the current balance.

This is how I tried, but I failed to make the loop:

package ex3;

import java.util.Scanner;

public class BankApp {

public static void main(String[] args) {
//displaying the welcome message
System.out.println("Welcome to our bank.\nYour initial balance is 1000 SEK\n");
//initializing all necessary variables
double initialBalance = 1000;
double userChoise = 0;
double currentBalance;

//asking user to enter expected amount
System.out.println("Enter your amount you want to withdraw (in SEK): ");
//creating new instance of the scanner class
Scanner iScanner = new Scanner(System.in);
//store into userChoise whatever amount is chosen by user
userChoise = iScanner.nextDouble();
//checking wheather the user inputs any valid amount or not. In this case it must be minimum 100 and maximum 1000.
if(userChoise < 100 || userChoise > 1000)
{
System.out.println("Error: Enter your amount again(in SEK): ");
}
else {
currentBalance = initialBalance - userChoise;
System.out.printf("You have withdrawn %.2f\n", userChoise);
System.out.printf("Your current balance is %.2f\n", currentBalance);
}
}
}

Answer

Use while loop with TRUE condition and break when you want to break.

package ex3;

        import java.util.Scanner;

        public class BankApp {

            public static void main(String[] args) {
                //displaying the welcome message
                System.out.println("Welcome to our bank.\nYour initial balance is 1000 SEK\n");
                //initializing all necessary variables
                double initialBalance = 1000;
                double userChoise = 0;
                double currentBalance;

                //asking user to enter expected amount
                System.out.println("Enter your amount you want to withdraw (in SEK): ");

                //creating new instance of the scanner class
                Scanner iScanner = new Scanner(System.in);

                while(true){

                //store into userChoise whatever amount is chosen by user
                userChoise = iScanner.nextDouble();
                //checking wheather the user inputs any valid amount or not. In this case it must be minimum 100 and maximum 1000.
                if(userChoise < 100 || userChoise > 1000)
                {
                    System.out.println("Error: Enter your amount again(in SEK): ");
                } 
                else {
                    currentBalance = initialBalance - userChoise;
                    System.out.printf("You have withdrawn %.2f\n", userChoise);
                    System.out.printf("Your current balance is %.2f\n", currentBalance);
                    break;
               }
           }
           }
        }
Comments