02-2 Input & Output
02-2 Input & Output
Programming Methods 1
Chapter 2
42
Contents
Introduction
JOptionPane Class
▪ Read & Print Text
▪ Read & Print Letter
▪ Read & Print Number
Scanner Class
▪ Read & Print Text
▪ Read & Print Letter
▪ Read & Print Number
BufferedReader Class (You will study later)
Java Essential 43 Essam Ahmed Abdalla (Abu 3rwa)
Introduction
Since its introduction, Java has been notable for its
lack of built-in support for basic input, and for its
reliance on fairly advanced techniques for the
support that it does offer.
The Scanner class was introduced in Java 5.0 to
make it easier to read basic data types from a
character input source.
However, it requires some knowledge of object-
oriented programming to use this class.
The Scanner class is in the package java.util.
Read through Scanner class is non-GUI program.
(Dialog Box)
46
Custom Dialogs
Dialog boxes are a window in which important
messages addressed to the user are shown, or that
give output from the program.
Many Java applications use dialog boxes to display
text instead of command window such as web
browsers.
Java has several standard dialog boxes that are
defined in the classes JOptionPane, JColorChooser,
and JFileChooser.
The JOptionPane class includes a variety of methods
for making simple dialog boxes that are variations on
three basic types: a “message” dialog, a “confirm”
dialog, and an “input” dialog.
يظهر رسالة في الصندوق بدون رموز ال يوجد رمز _JOptionPane. PLAIN
MESSAGE
(Scanner)
66
Java Scanner
The Scanner class is used to get user input, and
it is found in the java.util package.
The Java Scanner class is widely used to parse
text for strings and primitive types using a
regular expression.
It is the simplest way to get input in Java. By the
help of Scanner in Java, we can get input from
the user in primitive types such as int, long,
double, byte, float, short, etc.
System.out.print("Hello Java");
System.out.println("Hello Java");
import java.util.Scanner;
public class MyName2
{
public static void main(String[] args(
{
//2 & 3.(System) ( في الفئةin) ( يتصل بالكائنScanner) تعريف وإنشاء كائن من الفئة
(1) (2)
import java.util.Scanner;
public class MyName2
{
public static void main(String[] args(
{
//2 & 3.(System) ( في الفئةin) ( يتصل بالكائنScanner) تعريف وإنشاء كائن من الفئة
Scanner input= new Scanner (System.in);
String st; char ch;
System.out.println("Enter Letter ");
// 4. (Scanner) ) الموجودة في الفئةnext( إستخدام دالة قراءة النص
st=input.next(); // st إدخال النص وتخزينه في المتغير
// Read one character of the text using the charAt(Position) function
ch=st.charAt(0);
System.out.println( "The Letter is "+ch);
}
}
Java Essential 77 Essam Ahmed Abdalla (Abu 3rwa)
Read & Print Letter
(Buffer Reader)
81
References
Y. Daniel Liang “Introduction to Java Programming , 8th Edition “ -
Armstrong Atlantic State University,USA – 2012.