Java Console (Standard Input Output)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

2.4.

1 Standard Output
When a program computes a result, we need a way to display this
result to the user of the program. One of the most common ways to
do this in Java is to use the console window.
The console window is also called the standard
output window. We output data such as the
computation results or messages to the console
window via System.out.
Every data item we send to System.out will appear on
this console window. We call the technique to output
data by using System.out the standard output.
We use the print method to output a value. For
example, executing the code
System.out.print("Hello, Dr. Caffeine.");
2.4.2 String
The textual values we passed to the print method or
the constructor of the Jframe class are instances of
the String class. A sequence of characters separated
by double quotes is String constants. As String is a
class, we can create an instance and give it a name.
For example,
String name;
name = new String("Jon Java");
Unlike in other classes, the explicit use of new to create an
instance is optional for the String class. We can create a new
String object, for example, in this way:
Code:
String name;
name = "Decafe Latte";
There are close to 50 methods defined in the String class. We
will introduce three of them here: substring, length, and
indexOf. We can extract a substring from a given string by
specifying the beginning and ending positions. For example,
Code:
String text;
text = "Espresso";
System.out.print(text.substring(2, 7));
Notice the use of method composition in the last
statement, where the result of a method call is used
as an argument in another method call.
In the statement
Code:
System.out.print (text.substring(2,7));
the result of method call
text.substring(2,7)
Result of extracting and displaying the substring of
"Espresso" from index position’s 2 to 6.
The index position of the first character in a string is
0.

Figure 2.14 Individual characters in a string are numbered from 0.


is passed as an argument when calling the showMessageDialog
method. The sample statement is equivalent to
Code:
String tempStr;
tempStr = text.substring(2,7);
System.out.print(tempStr);
• Individual characters in a String object are indexed from 0, as
illustrated in
Figure 2.14. The first argument of the substring method
specifies the position of the first character, and the second
argument specifies the value that is 1 more than the
position of the last character. Figure 2.15 shows how the
substring method works.
Here are some more examples:
An error will result if you pass invalid arguments, such as
negative values, the second argument larger than the number
of characters in a string, or the first argument larger than the
second argument.
Figure 2.15 The effect of the substring method is shown. Notice that a
new string is created, and the original string remains intact.
We can find out the number of characters in a String
object by using the length method. For example, if the
name text refers to a string Espresso, then
text.length()
will return the value 8, because there are eight
characters in the string. Here are some more
examples:
To locate the index position of a substring within
another string, we use the indexOf method. For
example, if the name text refers to a string I Love Java,
then
text.indexOf("Love")
will return the value 2, the index position of the first
character of the designated string Love. If the
searched substring is not located in the string, then 1
is returned.
Notice that the search is done in a case-sensitive
manner. Thus,
text.indexOf("java")
will return 1. If there is more than one occurrence of
the same substring, the index position of the first
character of the first matching substring is returned.
Here are some more examples:
Beyond the three methods we cover here and the
remaining methods of the String class, we have one
very useful string operation in Java called string
concatenation.
We can create a new string from two strings by
concatenating the two strings. We use the plus
symbol () for string concatenation. Here are the
examples:
Lab practical:
The sample class Ch2StringProcessing divides the
given full name into the first and last names and
displays the number of letters in the last name.
(Refer to code.notepad docs - Program 0ne)
2.4.3 Date and SimpleDateFormat
The Date class is used to represent a time instance to
a millisecond (one-thousandth of a second) precision.
This class is in the java.util package. When a new Date
object is created, it is set to the time it is created (the
current time is determined by reading the time
maintained by the operating system on your
machine). The Date class includes the toString
method that converts its internal format to a string
representation, which we can use to display the time.
For example, executing the code
Date today;
today = new Date( );
System.out.println(today.toString());
will display the current time in this format:
Thu Dec 18 18:16:56 PST 2008
Notice that the current time, when converted to a
string format, includes the date information also.
Internally, the time is kept as an elapsed time in
milliseconds since the standard base time known as
the epoch, which is January 1, 1970, 00:00:00 GMT
(Greenwich Mean Time).
If we do not like the default format, say we want to
display only the month and year or only the hours and
minutes in the AM/PM designation, then we can use
the SimpleDateFormat class. This class is in the
java.text package. For example, if we want to display
the month, day, and year in the MM/dd/yy shorthand
format, such as 07/04/08, we write
Date today;
SimpleDateFormat sdf;
today = new Date( );
sdf = new SimpleDateFormat("MM/dd/yy");
System.out.println(sdf.format(today));
If today is June 28, 2008, the code will display the date as
06/28/08
Notice the format designation is done by passing the
formatting string when a new SimpleDateFormat object is
created. The letters in the formatting string are
casesensitive.
The formatting string in this example must be MM/dd/yy,
and the letters d and y must be in lowercase. By increasing
the number of formatting letters, we can
change the length of the information, say, 2008 instead of
08. In case of the month, we change it from the number to
a name. For example, when we change sdf to
sdf = new SimpleDateFormat("MMMM dd, yyyy");
the dialog will display
June 28, 2008
If we want to display which day of the week today is,
we can use the letter E as in
Date today;
SimpleDateFormat sdf;
today = new Date( );
sdf = new SimpleDateFormat("EEEE");
System.out.println("Today is " + sdf.format(today));
Lab Practical
(Refer to Program two)
2.4.4 Standard Input
Analogous to System.out for output, we have System.in
for input. We call the technique to input data using
System.in standard input. System.in accepts input from
the keyboard. We also use the term console input to
refer to standard input. Using System.in for input is
slightly more complicated than using System.out for
output.
System.in is an instance of the InputStream class that
provides only a facility to input 1 byte at a time with its
read method. However, multiple bytes are required to
represent common types of data such as strings. The
Scanner class from the java.util
package provides a necessary input facility to
accommodate various input routines.
We limit our discussion here to input of string values.
We extend our discussion to input of numerical values
in Chapter 3.
To input data from the standard input by using a
Scanner object, we first create it by passing System.in
as follows:
import java.util.*;
...
Scanner scanner;
scanner = new Scanner(System.in);
Once we have a Scanner object, then we can input a
single word by using its next method. Here’s code to
input the first name of a person:
Scanner scanner = new Scanner(System.in);
String firstName;
//prompt the user for input
System.out.print("Enter your first name: ");
firstName = scanner.next( );
System.out.println("Nice to meet you, " + firstName +
".");
The user interaction of this sample code is shown in
Figure 2.16. In the diagram, the characters entered
by the user are displayed in the console window as
they are typed in, so the user can see what’s been
entered. Printing out the values just entered is called
echo printing. The string input is processed until the
Enter (or Return) key is pressed, so we can erase the
characters by pressing the Backspace key while
entering the data.
Lab practical
(Refer to program three (A) and (B))

You might also like