๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Get User Input in Java  


Introduction:
  • In Java, user input is commonly taken using the Scanner class, which is part of the java.util package.
  • This class provides methods to read data of different types such as int, float, String, etc.
Program:
  • Below is the program demonstrates how to take input from the user and display it:
    import java.util.Scanner; // Import Scanner class
    
    public class UserInputExample
    {
        public static void main(String[] args)
        {
            // Step 1: Create Scanner object
            Scanner scanner = new Scanner(System.in);
            
            // Step 2: Prompt the user for input
            System.out.print("Enter your name: ");
            String name = scanner.nextLine(); // Read a string input
            
            System.out.print("Enter your age: ");
            int age = scanner.nextInt(); // Read an integer input
            
            System.out.print("Enter your favorite decimal number: ");
            double favoriteNumber = scanner.nextDouble(); // Read a double input
            
            // Step 3: Display the input back to the user
            System.out.println("\nThank you for providing the details!");
            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("Favorite Number: " + favoriteNumber);
            
            // Step 4: Close the Scanner
            scanner.close();
        }
    }
    Output:
    Enter your name: Deepak
    Enter your age: 30
    Enter your favorite decimal number: 21.1
    
    Thank you for providing the details!
    Name: Deepak
    Age: 30
    Favorite Number: 21.1
Program Explanation:
  • Scanner scanner = new Scanner(System.in);
    • Here we have created a Scanner object to read input from the standard input stream (System.in).
  • Input Methods:
    • scanner.nextLine(): Here nextLine() method is used to read a line of text (String).
    • scanner.nextInt(): Here nextInt() method is used to read an integer.
    • scanner.nextDouble(): Here nextDouble() method is used to read a double-precision number.
  • Closing Scanner:
    • Use scanner.close() to release the resources after input is complete.

Task:
  • Create a banking application with the following options:
    1. Check Balance - Display the current balance.
    2. Deposit Money - Allow users to add money to their account (validate for non-negative amounts).
    3. Withdraw Money - Allow users to withdraw money (ensure they have sufficient balance and input is non-negative).
    4. Exit - Exit the application.
  • Requirements:
    • Use conditional statements concepts i.e. if, if-else, switch, while etc for above task.
    • Validate deposit and withdrawal amounts.
      (e.g., no negative values, sufficient funds for withdrawal).
    • Allow the user to perform multiple operations until they exit.
    • Handle invalid menu choices appropriately by displaying an error message.
Solution:
  • import java.util.Scanner;
    
    public class BankApp
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            double balance = 0;
            int choice;
    
            while (true)
            {
                System.out.println("Select One Option From Below Bank App:");
                System.out.println("1. Check Balance");
                System.out.println("2. Deposit Money");
                System.out.println("3. Withdraw Money");
                System.out.println("4. Exit");
    
                System.out.print("Enter your choice: ");
                choice = scanner.nextInt();
    
                switch (choice)
                {
                    case 1:
                        System.out.println("Your current balance is: " + balance+" Rs.");
                        break;
                    case 2:
                        System.out.print("Enter amount to deposit: ");
                        double deposit = scanner.nextDouble();
                        if (deposit > 0)
                        {
                            balance = balance + deposit;
                            System.out.println("Amount deposited successfully.");
                        }
                        else
                        {
                            System.out.println("Invalid amount! Please enter a positive value.");
                        }
                        break;
                    case 3:
                        System.out.print("Enter amount to withdraw: ");
                        double withdraw = scanner.nextDouble();
                        if (withdraw > 0 && withdraw <= balance)
                        {
                            balance = balance - withdraw;
                            System.out.println("Amount withdrawn successfully.");
                        }
                        else if (withdraw <= 0)
                        {
                            System.out.println("Invalid amount! Please enter a positive value.");
                        }
                        else
                        {
                            System.out.println("Insufficient balance!");
                        }
                        break;
                    case 4:
                        System.out.println("Thank you for using our banking service.");
                        scanner.close();
                        return; // Exit the program
                    default:
                        System.out.println("Invalid choice! Please enter a number between 1 and 4.");
                }
                System.out.println("------------------------");
            }
        }
    }
    Output:
    Select One Option From Below Bank App:
    1. Check Balance
    2. Deposit Money
    3. Withdraw Money
    4. Exit
    Enter your choice: 1
    Your current balance is: 0.0 Rs.
    ------------------------
    Select One Option From Below Bank App:
    1. Check Balance
    2. Deposit Money
    3. Withdraw Money
    4. Exit
    Enter your choice: 2
    Enter amount to deposit: 1000
    Amount deposited successfully.
    ------------------------
    Select One Option From Below Bank App:
    1. Check Balance
    2. Deposit Money
    3. Withdraw Money
    4. Exit
    Enter your choice: 3
    Enter amount to withdraw: 200
    Amount withdrawn successfully.
    ------------------------
    Select One Option From Below Bank App:
    1. Check Balance
    2. Deposit Money
    3. Withdraw Money
    4. Exit
    Enter your choice: 1
    Your current balance is: 800.0 Rs.
    ------------------------
    Select One Option From Below Bank App:
    1. Check Balance
    2. Deposit Money
    3. Withdraw Money
    4. Exit
    Enter your choice: 8
    Invalid choice! Please enter a number between 1 and 4.
    ------------------------
    Select One Option From Below Bank App:
    1. Check Balance
    2. Deposit Money
    3. Withdraw Money
    4. Exit
    Enter your choice: 4
    Thank you for using our banking service.