Scanner
class, which is part of the java.util
package.
int
, float
, String
, etc.
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();
}
}
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
Scanner scanner = new Scanner(System.in);
Scanner
object to read input from the standard input stream (System.in
).
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.
scanner.close()
to release the resources after input is complete.
if
, if-else
, switch
, while
etc for above task.
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("------------------------");
}
}
}
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.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.