Recieve user input using Scanner class

1.Recieve user input using Scanner class ?

Sample program for Recieve user input using Scanner class.

Here’s a Java program that Recieve user input using Scanner class ( name & age ) and prints out a greeting message:

import java.util.Scanner;

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

        System.out.print("Enter your name: ");
        String name = scanner.nextLine();

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

        System.out.println("Hello, " + name + "! You are " + age + " years old.");
    }
}

In this program, we create a new Scanner object scanner that reads input from the console. We then prompt the user to enter their name and age using System.out.print, and read their responses using scanner.nextLine() and scanner.nextInt(), respectively.

Finally, we use System.out.println to print out a greeting message that includes the user’s name and age. The + operator is used to concatenate the various strings and variables in the message.

You can run above all code here just copy and paste.

Ram Chadar

Hello! I'm Ram Chadar, a passionate software developer and freelancer based in Pune. Welcome to my blog, where I share my experiences, insights, and knowledge in the world of software development, different technologies, freelancing, and more.

View all posts by Ram Chadar →

3 thoughts on “1.Recieve user input using Scanner class ?

  1. You really make it seem really easy together with your presentation but I find this topic to be really one thing that I think I’d never understand. It sort of feels too complex and extremely extensive for me. I’m having a look forward for your subsequent submit, I抣l try to get the dangle of it!

Leave a Reply

Your email address will not be published. Required fields are marked *