Java

Scanner class in Java

Scanner class in java which is assume to input the data and read the data.

Example  1

Input the name using Scanner

public class Example {

    public static void main(String args[])
    {
        Scanner inputvalue = new Scanner(System.in);
        System.out.println("Enter Your Name");
        String name = inputvalue.nextLine();

        System.out.println("Your Name is" + name);
    }

}

Example 2

Input Two Numbers and Calculate the display the Total using Scanner

public class Example {

    public static void main(String args[])
    {
        Scanner inputvalue = new Scanner(System.in);

        System.out.println("Enter Number 1");
        int number1 = inputvalue.nextInt();

        System.out.println("Enter Number 2");
        int number2= inputvalue.nextInt();

        int tot = number1 + number2;

        System.out.println();

        System.out.println("Your Total is " + tot);

    }

Example 3

Input the employee Salary and calculate the notes and coins

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

import java.util.*;
class Example{
    public static void main(String args[])
     {
        Scanner inputvalue = new Scanner(System.in);
        System.out.print("Enter Employee Salary : ");

        int employeesalary = inputvalue.nextInt();
        System.out.println();

        int notes = employeesalary / 5000;
        employeesalary = employeesalary % 5000;
        System.out.println("5000 notes : " + notes);

        notes = employeesalary / 1000;
        employeesalary = employeesalary % 1000;
        System.out.println("1000 notes : " + notes);

        notes = employeesalary / 500;
        employeesalary = employeesalary % 500;
        System.out.println("500 notes : " + notes);

        notes = employeesalary / 100;
        employeesalary = employeesalary % 100;
        System.out.println("100 notes : " + notes);

        notes = employeesalary / 50;
        employeesalary = employeesalary % 50;
        System.out.println("50 notes : " + notes);

        notes = employeesalary / 20;
        employeesalary = employeesalary % 20;
        System.out.println("20 notes : " + notes);

        int coins = employeesalary / 10;
        employeesalary = employeesalary % 10;
        System.out.println("10 coins : " + coins);

        coins = employeesalary / 5;
        employeesalary = employeesalary % 5;
        System.out.println("5 coins : " + coins);

        coins = employeesalary / 2;
        employeesalary = employeesalary % 2;

        System.out.println("2 coins : " + coins);
        System.out.println("1 coins : " + employeesalary);
    }
}

How to get the output look like below.

Enter your First name : John

Enter your Last name : Peter

Enter your Home number : 89

Enter your Road name : main

Enter your city : Japan

Your Full Name : John Peter

Your Home Address : No: 89, main Rd, Japan.

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 Road name : ");
        String roadname = input.next();

        System.out.print("Enter your city : ");
        String cityname = input.next();

        System.out.println();

        System.out.println("Your Full Name : " + firstName + " " + lastName);
        System.out.println("Your Home Address : No: " + homeNumber + ", " + roadname + " Rd, " + cityname + ".");
    }
}

IF Else Condition

when we make a decisions of the programming  that time we can use if condition.
If the condition is true we use IF.if the condition is false use Else

Scanner input = new Scanner(System.in);
System.out.print("Enter your age : ");
int age = input.nextInt();
  if(age > 40)
     {
      System.out.println("Elder");
     }else if(age > 18)
     {
       System.out.println("Young");
     }else
     {
       System.out.println("Child");
     }

Input the Num1,Num2 and calculate the maximum value

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");
}
}
}

Input the Number 1,Number 2,Number 3 and calculate the maximum value.

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");
        }
    }
}

Input the Student Marks 1,Marks 2,Marks 3 calculate and display the total,average,Grade.

In order to calculate the Grade include the following condition.
if the average greater than 50 result will be “Pass” Other wise result will “Fail”

import java.util.*;
class Example{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Marks 1 : ");
        int marks1 = input.nextInt();

        System.out.print("Marks 2 : ");
        int marks2 = input.nextInt();

        System.out.print("Marks 3 : ");
        int marks3 = input.nextInt();

        int tot = marks1 + marks2 + marks3;
        double avg = (double)tot / 3;

        String grade = "Fail";
        if(avg >= 50){
            grade = "Pass";
        }
        System.out.println("Total : " + tot);
        System.out.println("Average : " + avg);
        System.out.println("Result : " + grade);
    }
}

Input the Student Marks 1,Marks 2,Marks 3 calculate and display the total,average,Grade.

In order to calculate the Grade include the following condition.
if the average greater than 75 Grade will be “A”
if the average greater than 65 Grade will be “B”
if the average greater than 55 Grade will be “C”
if the average greater than 35 Grade will be “S”
Other wise Fail

import java.util.*;
class Example{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.print("Marks 1 : ");
        int marks1 = input.nextInt();
        System.out.print("Marks 2 : ");
        int marks2 = input.nextInt();
        System.out.print("Marks 3 : ");
        int marks3 = input.nextInt();

        System.out.println();

        int total = marks1 + marks2 + marks3;
        double average = (double)total / 3;

        System.out.println();

        System.out.println("Total : " + total);
        System.out.println("Average : " + average);


        if(average > 100){
            System.out.println("Invalid Marks.....");
        }else if(average < 0){
            System.out.println("Invalid Marks.....");
        }else if(average >= 75){
            System.out.println("Grade : A");
        }else if(average >= 65){
            System.out.println("Grade : B");
        }else if(average >= 55){
            System.out.println("Grade : C");
        }else if(average >= 35){
            System.out.println("Grade : S");
        }else{
            System.out.println("Grade : F");
        }
    }
}

 

 

 

 

admin

Recent Posts

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

2 weeks ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

1 month ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

1 month ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

2 months ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

2 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago