0% found this document useful (0 votes)
21 views20 pages

Java Input Classes Overview

This document is a lesson plan from Pampanga State University focusing on Java user input classes: Scanner, BufferedReader, and JOptionPane. It explains how to use these classes to obtain user input in various formats, including strings, integers, and booleans, along with examples of code implementations. Additionally, it covers different types of dialogs available in JOptionPane for user interaction.

Uploaded by

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

Java Input Classes Overview

This document is a lesson plan from Pampanga State University focusing on Java user input classes: Scanner, BufferedReader, and JOptionPane. It explains how to use these classes to obtain user input in various formats, including strings, integers, and booleans, along with examples of code implementations. Additionally, it covers different types of dialogs available in JOptionPane for user interaction.

Uploaded by

ronnelbalcita23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

Lesson 4: Java User Input Class


Scanner Class, BufferedReader Class and JOptionPane Class

Course Code: ITELEC 1


Course Title: Object-Oriented Programming (Java)
Prepared by: Alvin Herrera, Instructor 1

Lesson Content:
1. What is Scanner Class?
2. What is BufferedReader Class?
3. What is JOptionPane Class?
4. Operators

1. What is Scanner Class?

The Scanner class is present in the [Link] package is used


to obtain input for primitive types like int, double, etc., and
strings.

One of the strengths of Java is the huge libraries of code


available to you. This is code that has been written to do specific
jobs. All you need to do is to reference which library you want to
use, and then call a method into action.

One really useful class that handles input from a user is


called the Scanner class. The Scanner class can be found in the
[Link] library.

To use the Scanner class, you need to reference it in your


code. This is done with the keyword import.

import [Link];

To create a new Scanner object the code is this:

Scanner user_input = new Scanner( [Link] );

So instead of setting up an int variable or a String variable,


we're setting up a Scanner variable. We've called ours user_input.
After an equals sign, we have the keyword new. This is used to
create new objects from a class. The object we're creating is from
the Scanner class. In between round brackets we have to tell java
that this will be System Input ([Link]).
To get the user input, you can call into action one of the many
methods available to your new Scanner object. One of these methods
is called next. This gets the next string of text that a user types
on the keyboard:

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS •


LUBAO • CANDABA • APALIT • SAN FERNANDO Office
Address | Moras dela Paz, Sto. Tomas, Pampanga,
Philippines Email Address |
santotomascampus@[Link] • Contact
No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines
PAMPANGA STATE UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus

String name;
name = user_input.next();

Example:
Code

// Java program to demonstrate the use of Scanner class


// to take input from user

import [Link];

class Main
{
public static void main(String [] args) {

Scanner user_input = new Scanner([Link]);


String name;

[Link]("Enter your name:");


name = user_input.nextLine();

[Link]("Hello, " + name + " Welcome to the World of


Programming.");

}
}

Output

Enter your name:


Alvin Herrera
Hello, Alvin Herrera Welcome to the World of Programming.

Other Java Scanner Input Types


Method Description

nextBoolean() Used for reading Boolean value

nextDouble() Used for reading Double value

nextFloat() Used for reading Float value

nextInt() Used for reading Int value

nextLine() Used for reading Line value

Example:
Code

// Java program to demonstrate the use of Scanner class


// to take input from user using different input types

import [Link];

class Main
{
public static void main(String [] args) {
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


Scanner user_input = new Scanner([Link]);
Boolean isMale;
float height;
int age;
String qoute;

[Link]("Are you a male(true/false): "); isMale


= user_input.nextBoolean();

[Link]("Enter your height in meters: "); height


= user_input.nextFloat();

[Link]("Enter your age: ");


age = user_input.nextInt();

//used to be able to input text, usually the program skips the


input text part
user_input.nextLine();

[Link]("Enter a qoute: ");


qoute = user_input.nextLine();

[Link]("Enter a character: ");


char ch = user_input.nextLine().charAt(0);

[Link]("\nMale: " + isMale);


[Link]("Height: " + height);
[Link]("Age: " + age);
[Link]("Qoute: " + qoute);
[Link]("Character: " + ch);

}
}

Output

Are you a male(true/false): true


Enter your height in meters: 1.75
Enter your age: 23
Enter a qoute: Just do it!
Enter a character: A

Male: true
Height: 1.75
Age: 23
Qoute: Just do it!
Character: A
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

2. What is BufferedReader Class?

In Java, Scanner and BufferedReader class are sources that


serve as ways of reading inputs. Scanner class is a simple text
scanner that can parse primitive types and strings. It internally
uses regular expressions to read different types while on the other
hand BufferedReader class reads text from a character-input stream,
buffering characters so as to provide for the efficient reading of
the sequence of characters.

import [Link].*; // import the BufferedReader class

// Creating object of the BufferedReader class inside main() method


BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));

You can then use the readLine() method and parse the input to
a specific type of data.

Example:
Code

// Java program to demonstrate the use of BufferedReader


class // to take input from user

import [Link].*;

class Main
{
public static void main(String [] args)throws IOException {

BufferedReader br = new BufferedReader(new


InputStreamReader([Link]));

[Link]("Enter an integer");
int number = [Link]([Link]());

[Link]("Number = " + number);

}
}

Output

Enter an integer
5
Number = 5

Other Parsing Methods:


1. [Link]()
2. [Link]()
3. [Link]()
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

4. [Link]()
5. [Link]()
6. [Link]()
7. [Link]()
8. [Link]()

3. What is JOptionPane Class?

Another useful class for accepting user input, and displaying


results, is the JOptionPane class. This is located in the
[Link] library. The JOptionPane class helps us to create dialog
boxes such as message dialogs, confirmation dialogs, input dialogs,
and options dialogs.

The first thing to do is to reference the library we want to


use:

import [Link];

This tells java that we want to use the JOptionPane class,


located in the [Link] library.

1. JOptionPane – Input Dialog

To get an input box that the user can type into, we can
use the showInputDialog method of JOptionPane.

Syntax:
1. Basic Input Dialog
[Link](
String message
);
2. Parent Component
[Link](
Component parentComponent,
Object message
);
Note: use null to center the dialog on screen
3. Formatted Input Dialog
[Link](
Component parentComponent,
Object message,
String title,
int messageType
);

List of Message Type

∙ JOptionPane.PLAIN_MESSAGE

∙ JOptionPane.INFORMATION_MESSAGE

∙ JOptionPane.QUESTION_MESSAGE

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

∙ JOptionPane.WARNING_MESSAGE

∙ JOptionPane.ERROR_MESSAGE

Example:
// Java program to demonstrate the use of JOptionPane
class // with input dialog

import [Link];

class Main
{
public static void main(String [] args){

JOptionPane joption = new JOptionPane();

String full_name;

Full_name = [Link](null, "What is your Full Name:",


"Question", JOptionPane.QUESTION_MESSAGE);

}
}

Output:

2. JOptionPane – Message Dialog

To use a message box that displays information on screen,


we can use the showMessageDialog method of JOptionPane.

Syntax:
1. Basic Message Dialog
[Link](
Component parentComponent,
String message
);
2. Formatted Message Dialog

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus

[Link](
Component parentComponent,
Object message,
String title,
int messageType
);
Note: use null to center the dialog on screen

List of Message Type

∙ JOptionPane.PLAIN_MESSAGE

∙ JOptionPane.INFORMATION_MESSAGE

∙ JOptionPane.QUESTION_MESSAGE

∙ JOptionPane.WARNING_MESSAGE

∙ JOptionPane.ERROR_MESSAGE

Example:
// Java program to demonstrate the use of JOptionPane
class // with message dialog

import [Link];

class Main
{
public static void main(String [] args){

JOptionPane joption = new JOptionPane();

String full_name;

full_name = [Link](null, "What is your Full Name:",


"Question", JOptionPane.QUESTION_MESSAGE);

[Link](null, "Nice to meet you, " + full_name,


"Message", joption.INFORMATION_MESSAGE);
}
}
Output:

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
and
Republic of the Philippines Cancel

PAMPANGA STATE UNIVERSITY ∙


(former Don Honorio Ventura State University)

JOptionPane.OK_CANCEL_OPTION - OK and Cancel


Sto. Tomas Campus
Example:

3. JOptionPane – Confirm Dialog // Java program to demonstrate th


class // with confirm dialog
To use confirm, we can use the
showConfirmDialog method import [Link];
of JOptionPane.
class Main
Syntax: {
1. Basic Confirm Dialog public static void main(String [
[Link](
Component parentComponent, JOptionPane joption = new J
Object message
); String full_name;
2. Formatted Confirm Dialog
[Link](
full_name = [Link]
Component parentComponent,
"Question", JOptionPane.QUESTION_
Object message,
String title,
int optionType, [Link](n
int messageType "Message", joption.INFORMATION_ME
);
Note: use null to center the dialog on screen int option;

List of Option Type


BACOLOR (MAIN) • MEXICO • PORAC • STO.
∙ JOptionPane.DEFAULT_OPTION - Just an OK TOMAS • LUBAO • CANDABA • APALIT • SAN
button FERNANDO Office Address | Moras dela Paz,
Sto. Tomas, Pampanga, Philippines
Email Address |
∙ JOptionPane.YES_NO_OPTION - Yes and No santotomascampus@[Link] •
Contact No. | (0921) 373 0913 Website |
[Link]
∙ JOptionPane.YES_NO_CANCEL_OPTION - Yes, No,
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


option = [Link](null, "Do you want to Continue?",
"Confirmation", joption.YES_NO_OPTION, joption.QUESTION_MESSAGE);

if (option == 0) {
[Link](null, "You chose yes!", "Message",
joption.INFORMATION_MESSAGE);
}
if (option == 1) {
[Link](null, "You chose no.", "Message",
joption.INFORMATION_MESSAGE);
}
Output:

4. JOptionPane – Option Dialog

To create a custom option, we can use the


showOptionDialog method of JOptionPane.

Syntax:
1. Formatted Option Dialog
[Link](
Component parentComponent,
Object message,

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
// Java program to demonstrate th
class // with option dialog

import [Link];

class Main
{
public static void main(String [

JOptionPane joption = new J

String full_name;

full_name = [Link]
"Question", JOptionPane.QUESTION_

[Link](n
"Message", joption.INFORMATION_ME

Republic of the Philippines int option;


PAMPANGA STATE UNIVERSITY option = [Link]
(former Don Honorio Ventura State University) "Confirmation", joption.YES_NO_OP

if (option == 0) {
Sto. Tomas Campus [Link](null,
joption.INFORMATION_MESSAGE);
String title, }
int optionType, if (option == 1) {
int messageType, [Link](null,
Icon icon, joption.INFORMATION_MESSAGE);
Object[] options, }
Object initialValue
); String[] options = { "brown
int dessert = [Link]
Example: "Select one:", joption.DEFAULT_O
options, options[0]);
if (dessert == 0) {

BACOLOR (MAIN) • MEXICO • PORAC • STO.


TOMAS • LUBAO • CANDABA • APALIT • SAN
FERNANDO Office Address | Moras dela Paz,
Sto. Tomas, Pampanga, Philippines
Email Address |
santotomascampus@[Link] •
Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


[Link](null, "You chose a brownie!", "Message",
joption.INFORMATION_MESSAGE);
}
if (dessert == 1) {
[Link](null, "You chose pie.", "Message",
joption.INFORMATION_MESSAGE);
}
if (dessert == 2) {
[Link](null, "You chose cake!", "Message",
joption.INFORMATION_MESSAGE);
}

}
}
}

Output:

4. Operators
Commonly used operators:
1. Assignment Operator
2. Arithmetic Operators
3. Compound Assignment
4. Increase and Decrease
5. Relational and Equality Operators
6. Logical Operators
7. Conditional Operator
8. Comma Operator
9. Operator Precedence

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

1. Assignment Operator

The assignment operator (=) assigns a value to a variable.

Example:
int num = 5;

This statement assigns the integer value 5 to the variable


num. The part at the left of the assignment operator (=) is known as
the lvalue (left value) and the right one as the rvalue (right
value). The lvalue has to be a variable whereas the rvalue can be
either a constant, a variable, the result of an operation or any
combination of these. The most important rule when assigning is the
right-to-left rule: The assignment operation always takes place from
right to left, and never the other way.

Example:
Code Output

public class Main{ num = 5


public static void main(String []
args){

int num = 5; // assign value 5 to


variable num

[Link](“num = ” +
num);
}
}

2. Arithmetic Operators

The five arithmetic operators are +, -, *, /, %.

Operations of addition, subtraction, multiplication and division


literally correspond with their respective mathematical operators.

The only one that you might not be so used to see is modulo;
whose operator is the percentage sign (%). Modulo is the
operation that gives the remainder of a division of two values.
For example, if we write:

int remainder = 11 % 3;

The variable remainder will contain the value 2, since 2 is the


remainder from dividing 11 between 3.
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus

Example:
Code Output

public class Main{ remainder = 2


public static void main(String []
args){

int remainder = 11 % 3; // store


the remainder to variable
remainder

[Link](“remainder = ”
+ remainder);
}
}

3. Compound Assignment

When we want to modify the value of a variable by performing an


operation on the value currently stored in that variable we can use
compound assignment operators:
expression is equivalent to

value += increase; value = value + increase;

a -= 5; a = a - 5;

a /= b; a = a / b;

price *= units + 1; price = price * (units + 1);

Example:
Code Output

public class Main{ num = 6


public static void main(String [] num = 5
args){ num = 2
num = 4
int num = 5;

[Link]("num = " + (num


+= 1));
[Link]("num = " + (num
-= 1));
[Link]("num = " + (num
/= 2));
[Link]("num = " + (num
*= 2));
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines
Email Address | santotomascampus@[Link] • Contact No. | (0921) 373 0913
Website | [Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)

Sto. Tomas Campus


}
}

4. Increase and Decrease

Shortening even more some expressions, the increase operator (++)


and the decrease operator (--) increase or reduce by one the value
stored in a variable. They are equivalent to +=1 and to -=1,
respectively. Thus:

num++;
num += 1;
num = num + 1;
are all equivalent in its functionality: the three of them increase
by one the value of num.

Example:
Code Output

public class Main{ num = 10


public static void main(String [] num = 10
args){ num = 10

int num = 9;

num++;
[Link]("num = " +
num);

num = 9;

num += 1;
[Link]("num = " +
num);

num = 9;

num = num + 1;
[Link]("num = " +
num);

}
}

5. Relational and Equality Operators

In order to evaluate a comparison between two expressions we can


use the relational and equality operators.

== - Equal To
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

!= - Not Equal To
> - Greater Than
< - Less Than
>= - Greater Than or Equal To
<= - Less Than or Equal To

The result of a relational operation is a Boolean value that can


only be true or false, according to its Boolean result. We may want
to compare two expressions, for example, to know if they are equal
or if one is greater than the other is.

Example:
Code Output

public class Main{ 8 == 8 = true


public static void main(String [] 8 != 8 = false
args){ 12 < 8 = false
5 > 8 = false
[Link]("8 == 8 = " + 9 <= 8 = false
(8 == 8));
[Link]("8 != 8 = " + 8 >= 17 = false
(8 != 8));
[Link]("12 < 8 = " +
(12 < 8));
[Link]("5 > 8 = " + (5
> 8));
[Link]("9 <= 8 = " +
(9 <= 8));
[Link]("8 >= 17 = " +
(8 >= 17));

}
}

6. Logical Operators

The Operator ! is the C++ operator to perform the Boolean


operation NOT, it has only one operand, located at its right, and
the only thing that it does is to inverse the value of it, producing
false if its operand is true and true if its operand is false.
Basically, it returns the opposite Boolean value of evaluating its
operand. For example:

The logical operators && and || are used when evaluating two
expressions to obtain a single relational result. The operator &&
corresponds with Boolean logical operation AND. This operation
results true if both its two operands are true, and false otherwise.
The following panel shows the result of operator && evaluating the
expression a && b:
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

&& OPERATOR

The operator || corresponds with Boolean logical operation OR. This


operation results true if either one of its two operands is true,
thus being false only when both operands are false themselves. Here
are the possible results of a || b:

|| OPERATOR

Example:
Code Output

public class Main{ !(8 == 8) = false


public static void main(String [] (5 > 6) && (5 == 5) = false
args){ (5 > 6) || (5 == 5) = true

[Link]("!(8 == 8) = "
+ (!(8 == 8)));
[Link]("(5 > 6) && (5
== 5) = " + ((5 > 6) && (5 ==
5))); [Link]("(5 > 6)
|| (5 == 5) = " + ((5 > 6) || (5
== 5)));

}
}

7. Conditional Operator

The conditional operator evaluates an expression returning a


value if that expression is true and a different one if the
expression is evaluated as false.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

Syntax:
condition ? result1 : result2

Example:
Code Output

public class Main{ 8 is not equal to 5


public static void main(String []
args){

String result = 8 == 5? "8 is


equal to 5": "8 is not equal to
5"; [Link](result);

}
}

8. Comma Operator

The comma operator (,) is used to separate two or more variables.

Example:
Code Output

public class Main{


public static void main(String []
args){

int num1, num2, num3;


float num1, num2, num3;
}
}

9. Operator Precedence

When writing complex expressions with several operands, we may


have some doubts about which operand is evaluated first and which
later.

For example, in this expression:


a = 5 + 7 % 2;
we may doubt if it really means:
a = 5 + (7 % 2) // with a result of 6, or
a = (5 + 7) % 2 // with a result of 0

The correct answer is the first of the two expressions, with a


result of 6. There is an established order with the priority of each
operator, and not only the arithmetic ones (those whose preference
come from mathematics) but for all the operators which can appear in
C++.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

● Multiplication and Division has higher precedence than Addition


and Subtraction.
● Additon and Subraction has lower precedence than Multiplication
and Division.

Exercises:

1. Get an integer input from the user and store it to a variable, then
display the value.

Sample Output:

Enter a number: 7
You entered 7.

2. Get an integer input from the user and store the remainder to a
variable, the display the value.

Sample Output:

Enter a number: 10
The remainder of 10 divide by 3 is 1.

3. Get an integer input from the user and increase it by 2, 3 times.

Sample Output:

Enter a number: 5
1st = 7
2nd = 9
3rd = 11

4. Get two integer input from the user and test if they are equal,
display the problem and result either true or false.

Sample Output:

Enter first number: 21


Enter second number: 22
false

5. Create a login program that ask a user to enter his/her username and
password and display appropriate message after validation. Let’s say
his/her username is starboy21 and password is 33444.

Sample Output:

Enter username: starboy21


Enter password: 33444
Login Success!
BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]
Republic of the Philippines

PAMPANGA STATE
UNIVERSITY (former Don Honorio

Ventura State University) Sto. Tomas


Campus

Review Questions and their answers

1. What is the purpose of the Scanner class in Java?

Ans. The Scanner class is used to read input from the user, such as
strings, integers, or other primitive types, from the console.

2. How do you create a Scanner object to read user input from the
keyboard?

Ans. Scanner input = new Scanner([Link]);

3. What is the difference between next() and nextLine() methods in


Scanner?

Ans. next() reads a single word, while nextLine() reads an entire line of
text including spaces.

4. What is the BufferedReader class used for in Java?

Ans. BufferedReader reads text from an input stream efficiently by


buffering characters, often used for reading input from the user via the
console.

5. How do you convert input from BufferedReader to an integer?

Ans. Use [Link]([Link]()) to convert the string input into


an integer.

6. What is the use of the JOptionPane class in Java?

Ans. JOptionPane is used to create pop-up dialog boxes for input,


messages, confirmation, and options in GUI-based applications.

7. What method of JOptionPane is used to get user input?

Ans. The showInputDialog() method is used to display a dialog box that


accepts user input.

8. What is the purpose of arithmetic operators in Java?

Ans. Arithmetic operators (+, -, *, /, %) are used to perform basic


mathematical operations on numeric values.

9. How does the conditional operator (? :) work in Java?

Ans. The conditional operator returns one of two values depending on


whether the condition is true or false, using the syntax:

condition ? value_if_true : value_if_false.

10. What does operator precedence mean, and why is it important?

Ans. Operator precedence determines the order in which operations are


performed in an expression. It's important to ensure correct calculation,
especially in complex expressions.

BACOLOR (MAIN) • MEXICO • PORAC • STO. TOMAS • LUBAO • CANDABA • APALIT • SAN FERNANDO
Office Address | Moras dela Paz, Sto. Tomas, Pampanga, Philippines Email Address |
santotomascampus@[Link] • Contact No. | (0921) 373 0913 Website |
[Link]

You might also like