0% found this document useful (0 votes)
5 views6 pages

Java Scanner Class Input Guide

The document outlines an experiment using the Scanner class in Java to read user input of various data types. It provides a theoretical background on the Scanner class, its methods, and includes a sample program demonstrating its functionality. The program captures student information such as name, age, and GPA, and displays it to the user.

Uploaded by

zayedshaikh424
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)
5 views6 pages

Java Scanner Class Input Guide

The document outlines an experiment using the Scanner class in Java to read user input of various data types. It provides a theoretical background on the Scanner class, its methods, and includes a sample program demonstrating its functionality. The program captures student information such as name, age, and GPA, and displays it to the user.

Uploaded by

zayedshaikh424
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

Name:…………………………………………….

Department: Computer Engineering

Engineering

Class & Semester: SE, Sem III

Subject: Skill Base Lab- OOPM Java

Expt. No. 1

Title: Use of Scanner class.

Rubrics Score(0to5)
Program Execution using
ethics
Results and Conclusion
Self-learning

Total

Performed On:

Sign:
Experiment No. 1
Problem Definition:

Software: Write aprogramto readuserinputofdifferentdatatypes using Scanner Class

EclipseClass
Theory: Scanner 2023-06 for windows 10
.
TheScanner classinJava isusedtoreadinputfromtheuserItis a veryversatileclassthatcanbeusedto
read input in a variety of formats, including strings, numbers, and dates.

To use the Scanner class, you first need to import it into your program. You can do this by adding t
following line to the top of your program:

import [Link];
Once you have imported the Scanner class, you can create a new Scanner object. You can do this b
calling the new Scanner() constructor. For example:

Scanner scanner = new Scanner([Link]);


The Scanner([Link]) constructor creates a Scanner object that reads input from the console. Yo
also create a Scanner object that reads input from a file or a string.

Once you have created a Scanner object, you can use it to read input from the user. You can do this
calling the various methods of the Scanner class. For example, the following code reads a string fro
user:

String name = [Link]();


The nextLine() method reads the next line of input from the user. It returns a string that contains th
that the user entered.
The Scanner class also has methods for reading numbers, dates, and other types of input. You can
more about the Scanner class by reading the Java documentation.
Here are some of the benefits of using the Scanner class in Java:

● It is easy to use.
● It can be used to read input in a variety of formats.
● It is efficient.
● It is reliable.

The Scanner class in Java has a variety of functions that can be used to read input from the user. He
some of the most common functions of the Scanner class:

● next(): Reads the next token of input from the user. A token is a sequence of
characters that is separated from other tokens by whitespace.
nextLine(): Reads the next line of input from the user.

● nextInt(): Reads the next integer value from the user.


nextFloat(): Reads the next floating-point value from the user.

● nextDouble(): Reads the next double-precision floating-point value from the user.

hasNext(): Returns true if there is more input to be read from the user.

● nextBoolean(): Reads the next boolean value from the user.

hasNextBoolean():
The Scanner class also has aReturns
numbertrue if there
of other is a boolean
functions value
that can be to be read
used from
to read thefrom
input [Link] us
variety of formats. You can learn more about the functions of the Scanner class by reading the Java
documentation.
Here are some examples of how to use the Scanner class to read input from the user:

String name =
[Link](); int age =
[Link](); float gpa =
[Link]();
double number = [Link]();
boolean isMale = [Link]();

These examples show how to read a string, an integer, a floating-point number, a double-precision
floating-point number, and a boolean value from the user using the Scanner class.

The Scanner class is a powerful tool that can be used to read input from the user in a variety of for
is easy to use and efficient, making it a great option for reading input from the user in your Java
programs.
Program:

import [Link];

class Student {
String name;
int age;
double gpa;

Student(String name, int age, double gpa) {


[Link] = name;
[Link] = age;
[Link] = gpa;
}

void printInfo() {
[Link]("---- Student Information ----");
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("GPA: " + gpa);
[Link]("-----------------------------");
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter name of Student 1: ");


String name1 = [Link]();

[Link]("Enter age of Student 1: ");


int age1 = [Link]();

[Link]("Enter GPA of Student 1: ");


double gpa1 = [Link]();
[Link](); // consume leftover newline

Student s1 = new Student(name1, age1, gpa1);

[Link]("Enter name of Student 2: ");


String name2 = [Link]();

[Link]("Enter age of Student 2: ");


int age2 = [Link]();

[Link]("Enter GPA of Student 2: ");


double gpa2 = [Link]();

Student s2 = new Student(name2, age2, gpa2);

[Link]("\nStudent 1 -> Name: " + [Link] + ", Age: " + [Link] + ", GPA: " + [Link]);
[Link]("Student 2 -> Name: " + [Link] + ", Age: " + [Link] + ", GPA: " + [Link]);

[Link]();
[Link]();

[Link]();
}
}
Output Screenshot:

Enter name of Student 1: Arjun


Enter age of Student 1: 19
Enter GPA of Student 1: 8.1
Enter name of Student 2: Rohan
Enter age of Student 2: 18
Enter GPA of Student 2: 7.2

Student 1 -> Name: Arjun, Age: 19, GPA: 8.1


Student 2 -> Name: Rohan, Age: 18, GPA: 7.2

---- Student Information ----


Name: Arjun
Age: 19
GPA: 8.1
-----------------------------
---- Student Information ----
Name: Rohan
Age: 18
GPA: 7.2
-----------------------------
Conclusion:

Course Outcome:

References for Self Learning:

You might also like