CSBP219_SP25_Java_Week2
CSBP219_SP25_Java_Week2
Basics of Java
Reading and Writing from Keyboard and Files (I/O events).
Objectives
Output Results to Console
Formatted output with printf method
Input data into memory using input statements
Input & output dialog boxes
File input & output
2
Output
Output on the standard output device is accomplished by using the
standard output object System.out
print leaves the insertion point after the last character of the value of the
expression.
println positions the insertion point at the beginning of the new line.
Slide 3
3
Output
Exercises: what is the output of the following program?
}
}
4
Output
5
Output
Exercises: what is the output of the following program?
Slide 6
6
Formatting Output with printf
Standard output object System.out has two methods:
print()
println()
To output the results, the following statement is used:
7
Formatting Output with printf
To format the output in specific manner, method printf() is used:
OR
8
printf Method
Formatting the output can be done using the printf
method :
Example:
public class PrintFormat {
public static void main (String [] args) {
double x = 1234.23456789;
int num = 12342345;
System.out.printf(“%.2f”, x);
System.out.println();
System.out.printf(“%.3f”, x);
System.out.println();
System.out.printf(“%10d”, num);
System.out.println();
System.out.printf(“%5d%7.2f”, num, x);
System.out.println();
}
} 9
String Method format
Formatting the output can be done using the String
method format:
Example:
10
Input (Read) Statement
Data can be put into variables from the standard input device(keyboard)
using the input statement of Java.
Java provides the class Scanner.
Using Scanner class, an input stream object is created, and it is
associated with the standard input device:
11
Input (Read) Statement
The object console reads the next input as follows:
The expression console.nextInt() retrieves the next input as an integer.
The expression console.nextDouble() retrieves the next input as a
floating‐point number.
The expression console.next() retrieves the next input as a string.
The expression console.nextLine() retrieves the next input as a string
until the end of the line.
The expression console.next().charAt(0) retrieves the next input as a
single printable character.
Slide 12
12
Input (Read) Statement
Example:
Slide 13
13
File Input / Output
Getting input from keyboard and sending output to the
screen have limitations.
If the amount of input data is large:
inefficient: to type it takes a large amount of time
inconvenient: to type it can generate errors
Solution: file input & output
File: an area in secondary storage used to hold
information
14
File Input / Output
To input a file, a Scannerobject is initialized with a FileReader
object:
To close the input & output files, the method close is used:
inputFile.close();
outFile.close();
15
File Input / Output
Summary:
Read from File:
FileReader
replace System.in
add exception
16
File Input / Output
Example:
17
File Input / Output
Console Input/Output File Input/Output
18
Week 2 – Part 2
Basics of Java
Control Structure – Selection
Objectives
In this chapter you will learn:
Control structures
Relational & logic operators
Logic (boolean) expressions
if , if…else
switch
String comparison
20
Relational Operators
Operator Description
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
21
Logical (Boolean) Operators &
Expressions
Logical (Boolean) Operators & Expressions
Operator Description
! Not
&& And
|| Or
22
Conditional Expressions
Scanner console = new Scanner(System.in);
int score = console.nextInt();
if String grade = "";
if … else if … else
Scanner console = new Scanner(System.in);
int score = console.nextInt();
String grade = "";
24
Conditional Operator (? :)
Certain if… else statements can be written more
concisely by using the conditional operator: ? :
? : is a ternary operator:
25
switch Structures
A case value must appear only once.
One or more statements may follow
case label, you do not need to use
braces.
The break statement may or may not
appear after case statements.
A switch structure may or may not
have the default label.
26
switch Structures
Example:
Scanner console = new Scanner(System.in);
String month = console.next(); // “Abc”
switch(month) {
case “Jan”:
Scanner console = new Scanner(System.in); case “Mar”:
char grade = console.next().charAt(0); case “May”:
case “Jul”:
switch(grade) { case “Aug”:
case “Oct”:
case ‘A’: case “Dec”:
System.out.println(“The grade is A.”); System.out.println(“31 Days.”);
break; break;
case ‘B’: case “Apr”:
System.out.println(“The grade is B.”); case “Jun”:
break; case “Sep”:
case ‘C’: case “Nov”:
System.out.println(“The grade is C.”); System.out.println(“30 Days.”);
break; break;
default: break; case “Feb”:
} System.out.println(“28 or 29 Days.”);
break;
default:
System.out.println(“Wrong Month”);
}
27
String Comparisons
28
String Comparisons
Strings are compared character by character, starting
with the first character.
29
equals Method
30
equals Method
31
compareTo Method
More than
32
compareTo Method
33
compareTo Method
34
Comparing String Variables
The expression (str1 == str2) determines whether the
str1 and str2 reference (point to) the same object.
The expression (str1 != str2) determines whether the
str1 and str2 do not reference (point to) the same
object.
Example:
35
Comparing String Variables
The expression (str1 == str2) determines whether the
str1 and str2 point to the same object.
The expression (str1 != str2) determines whether the
str1 and str2 do not point to the same object.
Example:
36
Comparing String Variables
37
Comparing String Variables
import java.util.*;