Programming Language II
CSE-215
Prof. Dr. Mohammad Abu Yousuf
yousuf@juniv.edu
1
Read Input From Console
2
System.console().readLine()
• The simplest way to read user input is by using
System.console(). Here is an example:
3
java.util.Scanner and System.in
• Scanner class is present in java.util package so we import
this package in our program. We first create an object of
Scanner class and then we use the methods of Scanner
class. Consider the statement
Scanner a = new Scanner(System.in);
• Here Scanner is the class name, a is the name of object,
new keyword is used to allocate the memory and
System.in is the input stream. Following methods of
Scanner class are used in the program below :-
1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string
4
• The Scanner class has several methods which are used to
take different types of inputs.
• Combining System.in and java.util.Scanner provides a
way to read user input.
• It also provides a way to read different data types.
5
java.util.Scanner and System.in
6
java.util.Scanner and System.in
Read input string :
7
java.util.Scanner and System.in
8
java.util.Scanner and System.in
9
java.util.Scanner and System.in
10
java.util.Scanner and System.in
11
java.util.Scanner and System.in
12
java.util.Scanner and System.in
13
java.util.Scanner and System.in
14
Example 1: input
from user
15
Example 2: input
from user
16
Example 1: How to put a Scanner input int
o an array
17
Example 2: How to
put a Scanner input
into an array
18
Input values into a 2d array
19
Input and storing Strings in two-dimensional arrays
20
Thank you
21