0% found this document useful (0 votes)
27 views4 pages

Program To Show The Usage of Scanner Class Methods

Uploaded by

vineeljoshua04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Program To Show The Usage of Scanner Class Methods

Uploaded by

vineeljoshua04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Activity for Lab

Name: Vineel Joshua


Roll no:23B81A05NK
Subject: OOPS(JAVA)
Program to show the
usage of Scanner class
methods

import java.util.Scanner; // Import the Scanner class

public class ScannerExample {


public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);

// Reading a String input


System.out.print("Enter your name: ");
String name = scanner.nextLine(); // Reads a line of text

// Reading an integer input


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

// Reading a double input


System.out.print("Enter your GPA: ");
double gpa = scanner.nextDouble(); // Reads a double
value

// Reading a boolean input


System.out.print("Are you a student? (true/false): ");
boolean isStudent = scanner.nextBoolean(); // Reads a
boolean value

// Output the inputs back to the user


System.out.println("\nYour Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("GPA: " + gpa);
System.out.println("Student Status: " + (isStudent ? "Yes"
: "No"));

// Close the scanner to prevent resource leaks


scanner.close();}}

You might also like