LESSON: JAVA INPUT
Java provides different ways to get input from the user. However, in this module, you will
learn to get input from user using the object of Scanner class.
In order to use the object of Scanner, we need to import java.util.Scanner package. Then,
you need to create an object of the Scanner class. You can use the object to take input from
the user.
Input Types
Methods Descriptions
nextBoolean() Accepts a boolean value from the user
nextDouble() Accepts a double value from the user
nextInt() Accepts a int value from the user
nextLine() Accepts a String value from the user
APPLICATION OF JAVA INPUT IN THE PROGRAM
import java.util.Scanner; //Importing the package
Scanner input = new Scanner(System.in); //Creating an object for Scanner
import java.util.Scanner;
public class InputExample
public static void main(String[] args)
Scanner input = new Scanner(System.in);
String sName = “Juan Tamad”;
int iAge = 15;
System.out.println(“My name is ” + sName);
System.out.println(“My age is ” + iAge);
System.out.println(“”);
System.out.print(“Enter your name: ”);
sName = input.nextLine();
System.out.print(“Enter your age: ”);
iAge = input.nextInt();
System.out.println(“Your name is ”+sName+“ and you are ”+iAge+“years old”);
OUTPUT
My name is Juan Tamad
My age is 15
Enter your name: Toothless
Enter your age: 27
Your name is Toothless and you are 27years old
Reminder:
1. The underlined text in the output is the INPUTTED value from the user. Output may
differ if the user inputs different value.
2. The value that users inputted must be jive to the data type of the variable else it will result
to an error.
Activity: ½ yellow pad write the code submit to me when class resume.
Problem # 1: Write a program in Java that will ask the user to input three numbers and then the
program will find the total sum of three numbers given by the user.
SAMPLE OUTPUT:
********************ADDITION OF THREE NUMBERS*********************
Enter First Number: 1
Enter Second Number: 2
Enter Third Number: 3
The sum of 1, 2 and 3 is 6
********************END OF THE PROGRAM*****************************