Scanner class in use to read the input from the user. nextLine() method which used read line of the text.

import java.util.*;
class Example{
    
public static void main(String args[])
{
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your number : ");
        int number = input.nextInt();
        System.out.println();
        System.out.println("Your Number : " + number);
}
}

Ask to enter the Number if you enter as 12. answer below as 12

Output

Enter your number : 

Your Number : 12

Add Two Using Scanner Inputs

input two numbers and read the data and calculate and display the total.

import java.util.*;
class Example{
public static void main(String args[])
{
   Scanner input = new Scanner(System.in);
   System.out.print("Enter Number 1 : ");
   int number1 = input.nextInt();
   System.out.print("Enter Number 2 : ");
   int number2 = input.nextInt();
   int total = number1 + number2;
   System.out.println();
   System.out.println("Total : " + total);
}
}

Output

Enter Number 1 : 23
Enter Number 2 : 45

Total : 68

How to input String using Scanner

import java.util.*;
class Example{
public static void main(String args[])
{
   Scanner input = new Scanner(System.in);
   System.out.print("Enter your text : ");
   String text = input.nextLine();
   System.out.println();
   System.out.println("Your Text : " + text);
}
}

Output

Enter your text : Tutusfunny

Your Text : Tutusfunny

Money Calculate System

import java.util.*;
class Example{
public static void main(String args[])
{
   Scanner input = new Scanner(System.in);
   System.out.print("Enter Salary : ");
   int salary = input.nextInt();
   System.out.println();
   int notes = salary / 5000;
   salary = salary % 5000;
   System.out.println("5000 notes : " + notes);
   notes = salary / 1000;
   salary = salary % 1000;
   System.out.println("1000 notes : " + notes);
   notes = salary / 500;
   salary = salary % 500;
   System.out.println("500 notes : " + notes);
   notes = salary / 100;
   salary = salary % 100;
   System.out.println("100 notes : " + notes);
   notes = salary / 50;
   salary = salary % 50;
   System.out.println("50 notes : " + notes);
   notes = salary / 20;
   salary = salary % 20;
   System.out.println("20 notes : " + notes);
   int coins = salary / 10;
   salary = salary % 10;
   System.out.println("10 coins : " + coins);
   coins = salary / 5;
   salary = salary % 5;
   System.out.println("5 coins : " + coins);
   coins = salary / 2;
   salary = salary % 2;
   System.out.println("2 coins : " + coins);
  System.out.println("1 coins : " + salary);
}
}

Output

Enter Salary : 56515

5000 notes : 11
1000 notes : 1
500 notes : 1
100 notes : 0
50 notes : 0
20 notes : 0
10 coins : 1
5 coins : 1
2 coins : 0
1 coins : 0

Personal Details inputs

import java.util.*;
class Example{
public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter your first name : ");
    String firstName = input.next();
    
    System.out.print("Enter your last name : ");
    String lastName = input.next();
    
    System.out.print("Enter your home number : ");
    String homeNumber = input.next();
    
    System.out.print("Enter your street name : ");
    String streetName = input.next();
    
    
    System.out.print("Enter your city : ");
    String city = input.next();
    
    System.out.println();
    System.out.println("Your Full Name : " + firstName + " " + lastName);
    System.out.println("Your Home Address : No: " + homeNumber + ", " + streetName + " Rd, " + city + ".");
}
}

Output

Enter your first name : Ram
Enter your last name : Kobi
Enter your home number : 87
Enter your street name : Main
Enter your city : Channai

Your Full Name : Ram Kobi
Your Home Address : No: 87, Main Rd, Channai.

IF Examples Using Scanner

Example 1

import java.util.*;
class Example{
public static void main(String args[])
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your age : ");
    int age = input.nextInt();
    if(age < 18){
    System.out.println("Child....");
    }else if(age < 35){
    System.out.println("young....");
    }else{
    System.out.println("Elder....");
}
}
}

Output

Enter your age : 67
Elder….

Example 2

import java.util.*;
class Example{
public static void main(String args[]){
 Scanner input = new Scanner(System.in);
 System.out.print("Enter Number 1 : ");
 int number1 = input.nextInt();
 System.out.print("Enter Number 2 : ");
 int number2 = input.nextInt();
 System.out.println();
 if(number1 > number2){
  System.out.println("Maximum Number : Number 1");
 }else{
 System.out.println("Maximum Number : Number 2");
}
}
}

Output

Enter Number 1 : 45
Enter Number 2 : 66

Maximum Number : Number 2

Example 3

import java.util.*;
class Example{
public static void main(String args[])
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Number 1 : ");
    int number1 = input.nextInt();
    System.out.print("Enter Number 2 : ");
    int number2 = input.nextInt();
    System.out.print("Enter Number 3 : ");
    int number3 = input.nextInt();
    if(number1 > number2){
    if(number1 > number3){
        System.out.println("Maximum Number : Number 1");
    }else{
    System.out.println("Maximum Number : Number 3");
}
    }else if(number2 > number3){
    System.out.println("Maximum Number : Number 2");
    }else{
    System.out.println("Maximum Number : Number 3");
}
}
}

Output

Enter Number 1 : 56
Enter Number 2 : 78
Enter Number 3 : 56

Maximum Number : Number 2

Example 4

import java.util.*;
class Example{
public static void main(String args[])
{
  Scanner input = new Scanner(System.in);
  System.out.print("Enter Subject 1 Marks : ");
  int marks1 = input.nextInt();
  System.out.print("Enter Subject 2 Marks : ");
  int marks2 = input.nextInt();
  System.out.print("Enter Subject 3 Marks : ");
  int marks3 = input.nextInt();
  System.out.print("Enter Subject 4 Marks : ");
  int marks4 = input.nextInt();
  System.out.print("Enter Subject 5 Marks : ");
  int marks5 = input.nextInt();
  System.out.println();
  int total = marks1 + marks2 + marks3 + marks4 + marks5;
  double average = (double)total / 5;
 String result = "Fail";
 if(average >= 50){
 result = "Pass";
 }
  System.out.println("Total : " + total);
  System.out.println("Average : " + average);
  System.out.println("Result : " + result);
}
}

Output

Enter Subject 1 Marks : 56
Enter Subject 2 Marks : 78
Enter Subject 3 Marks : 56
Enter Subject 4 Marks : 78
Enter Subject 5 Marks : 56

Total : 324
Average : 64.8
Result : Pass

Example 6

import java.util.*;
class Example{
public static void main(String args[])
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Subject Marks : ");
    int marks = input.nextInt();
    System.out.println();
if(marks > 100)
{
    System.out.println("Invalid Marks.....");
}
else if(marks < 0)
{
    System.out.println("Invalid Marks.....");
}
else if(marks >= 75)
{
    System.out.println("Grade : A");
}
else if(marks >= 65)
{
    System.out.println("Grade : B");
}
else if(marks >= 55)
{
    System.out.println("Grade : C");
}
else if(marks >= 35)
{
    System.out.println("Grade : S");
}
else{
    System.out.println("Grade : F");
}
}
}

Output

Enter Subject Marks : 56

Grade : C

Example 7

Inventory Billing System 

import java.util.Scanner;

public class FirstApp {

    public static void main(String[] args) {
        
    {
            Scanner scanner = new Scanner(System.in);
            double totalBill = 0.0;
            int choice = 0;

            do {
              System.out.println("Food Menu:");
              System.out.println("1. Burger - $5.99");
              System.out.println("2. Pizza - $7.99");
              System.out.println("3. Salad - $3.99");
              System.out.println("4. Exit");

              System.out.print("Enter your choice: ");
              choice = scanner.nextInt();

              switch (choice) {
                case 1:
                  totalBill += 5.99;
                  break;
                case 2:
                  totalBill += 7.99;
                  break;
                case 3:
                  totalBill += 3.99;
                  break;
                case 4:
                  System.out.println("Exiting...");
                  break;
                default:
                  System.out.println("Invalid choice. Please try again.");
              }
            } while (choice!= 4);

            System.out.println("Your total bill is: $" + totalBill);
          }
    }
}

 

 

 

admin

Share
Published by
admin
Tags: Java Scanner

Recent Posts

Master React Inventory Management System Development

Introduction to Inventory Management Systems In today's fast-paced digital environment, businesses require efficient inventory management…

3 hours ago

React Inventory Management System

Introduction to React Inventory Management Systems In today’s fast-paced business environment, efficient inventory management is…

1 day ago

Login Form Using React

How to make a Login Form in React Step by Step. (more…)

2 days ago

Building Functional Calculator in React for Beginners

Introduction to React Calculator Creating a functional calculator in React is an excellent way to…

3 days ago

How to Create Functional Calculator in React

Introduction to React Calculator Creating a functional calculator in React is an excellent way to…

4 days ago

Inventory Management System Angular Step by Step

This article explain how to make a Inventory Management App in Angular.this app explain the…

6 days ago