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

Java Scanner Class Basics and Examples

This document covers the use of the Scanner class in Java for input operations. It includes details on importing the class, creating a Scanner object, and methods for inputting different data types such as integers, words, and full sentences. Additionally, it provides example programs demonstrating how to accept user input and perform operations like displaying a name and age, calculating the sum of two integers, counting characters in a sentence, and calculating the area of a circle.

Uploaded by

indumukhi.vp
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 views2 pages

Java Scanner Class Basics and Examples

This document covers the use of the Scanner class in Java for input operations. It includes details on importing the class, creating a Scanner object, and methods for inputting different data types such as integers, words, and full sentences. Additionally, it provides example programs demonstrating how to accept user input and perform operations like displaying a name and age, calculating the sum of two integers, counting characters in a sentence, and calculating the area of a circle.

Uploaded by

indumukhi.vp
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

Class 10 Computer Applications – Chapter INPUT IN JAVA (Scanner Class)

1. Which package is required to use the Scanner class?

• Answer: [Link] package.


2. Write the statement to import the Scanner class.

• Answer: import [Link];


3. How do you create a Scanner object?

• Answer: Scanner sc = new Scanner([Link]);


4. Which Scanner method is used to input an integer?

• Answer: nextInt()
5. Which Scanner method is used to input a word?

• Answer: next()
6. Which Scanner method is used to input a full sentence?

• Answer: nextLine()
7. Write the output of:
[Link]("Enter number:"); // no error
(Only prints the text)

• Answer: Enter number:


8. Why do we use [Link] in Scanner?

• Answer: [Link] represents standard keyboard input.


SECTION B – Programs
1. Accept a name and age from the user and display them.
import [Link].*;
class NameAge {
public static void main() {
Scanner sc = new Scanner([Link]);

[Link]("Enter name:");
String name = [Link]();

[Link]("Enter age:");
int age = [Link]();

[Link](name + " : " + age);


}
}
2. Accept two integers and display their sum.
import [Link].*;
class SumInput {
public static void main() {
Scanner sc = new Scanner([Link]);

int a = [Link]();
int b = [Link]();

[Link]("Sum = " + (a + b));


}
}
3. Accept a full sentence using nextLine() and count characters.
import [Link].*;
class SentenceLength {
public static void main() {
Scanner sc = new Scanner([Link]);

String s = [Link]();
[Link]("Length = " + [Link]());
}
}
4. Accept radius (double) and display area of circle.
import [Link].*;
class CircleArea {
public static void main() {
Scanner sc = new Scanner([Link]);

double r = [Link]();
double area = 3.14 * r * r;

[Link]("Area = " + area);


}
}

You might also like