Java Input Classes Overview
Java Input Classes Overview
PAMPANGA STATE
UNIVERSITY (former Don Honorio
Lesson Content:
1. What is Scanner Class?
2. What is BufferedReader Class?
3. What is JOptionPane Class?
4. Operators
import [Link];
String name;
name = user_input.next();
Example:
Code
import [Link];
class Main
{
public static void main(String [] args) {
}
}
Output
Example:
Code
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)
}
}
Output
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
You can then use the readLine() method and parse the input to
a specific type of data.
Example:
Code
import [Link].*;
class Main
{
public static void main(String [] args)throws IOException {
[Link]("Enter an integer");
int number = [Link]([Link]());
}
}
Output
Enter an integer
5
Number = 5
PAMPANGA STATE
UNIVERSITY (former Don Honorio
4. [Link]()
5. [Link]()
6. [Link]()
7. [Link]()
8. [Link]()
import [Link];
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
);
∙ 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
∙ 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){
String full_name;
}
}
Output:
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)
[Link](
Component parentComponent,
Object message,
String title,
int messageType
);
Note: use null to center the dialog on screen
∙ 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){
String full_name;
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)
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:
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 [
String full_name;
full_name = [Link]
"Question", JOptionPane.QUESTION_
[Link](n
"Message", joption.INFORMATION_ME
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) {
PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)
}
}
}
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
1. Assignment Operator
Example:
int num = 5;
Example:
Code Output
[Link](“num = ” +
num);
}
}
2. Arithmetic 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;
PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)
Example:
Code Output
[Link](“remainder = ”
+ remainder);
}
}
3. Compound Assignment
a -= 5; a = a - 5;
a /= b; a = a / b;
Example:
Code Output
PAMPANGA STATE
UNIVERSITY
(former Don Honorio Ventura State University)
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
int num = 9;
num++;
[Link]("num = " +
num);
num = 9;
num += 1;
[Link]("num = " +
num);
num = 9;
num = num + 1;
[Link]("num = " +
num);
}
}
== - 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
!= - Not Equal To
> - Greater Than
< - Less Than
>= - Greater Than or Equal To
<= - Less Than or Equal To
Example:
Code Output
}
}
6. Logical Operators
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
&& OPERATOR
|| OPERATOR
Example:
Code Output
[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
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
Syntax:
condition ? result1 : result2
Example:
Code Output
}
}
8. Comma Operator
Example:
Code Output
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
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.
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:
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:
PAMPANGA STATE
UNIVERSITY (former Don Honorio
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. next() reads a single word, while nextLine() reads an entire line of
text including spaces.
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]