Machine Translated by Google
Chapter 2: Methods & IO
VL 6: Input and Output
Winter Semester 2023/24 As
of October 11, 2024, 3:03 p.m.
Machine Translated by Google
Where are we right now?
2 hhu.de
Machine Translated by Google
lecture feedback
3 hhu.de
Machine Translated by Google
input and output devices
input devices
output devices
Icons: Breeze Icon Set
4 hhu.de
Machine Translated by Google
input and output so far
• Standard output: Text output with System.out • Console
arguments: Passing values that are known when the program starts
problem
What to do if inputs can only be made meaningful while the program is running?
5 hhu.de
Machine Translated by Google
standard input2
definition
Standard input is a communication channel provided by the operating system through which
a program can accept input from its environment during runtime. When a program
is running in the console, standard input is typically populated by user input in the
terminal. In Java, we can access standard input through the System.in variable.
Notes: • The
standard input can be filled in differently via the console (instead of the
user input)
• When input is "finished", EOF (End-of-File) is sent (via keyboard: Ctrl+D1 ) • System.in has the
data type InputStream ÿ need so-called scanners to read standard input 1Windows
(cmd): Ctrl+Z, Windows
(PowerShell): Ctrl+Z, Enter 2standard input
6 hhu.de
Machine Translated by Google
Example: Sum I
Sum.java Java
1 // Tells Java that we want to have scanners available
2 import java.util.Scanner;
3
4 public class Sum {
5 public static void main(String[] args) {
6 // Variable of type Scanner connected to standard input
7 Scanner standardInput = new Scanner(System.in);
8
9 double sum = 0;
10 // as long as there is still input in the standard input
11 while(standardInput.hasNext()) {
12 double nextNumber = standardInput.nextDouble();
13 sum += nextNumber;
14 }
15 System.out.println("The sum is " + sum + ".");
16 }
17 }
7 hhu.de
Machine Translated by Google
Example: Sum II
% java Sum 1 2
3 3.14^D
The total is 9.14.
• ˆD is not entered text, but the place where Ctrl+D was pressed
8 hhu.de
Machine Translated by Google
Important features of the scanner4
Result Name Description
boolean nextBoolean() read next value as Boolean
double nextDouble() int nextInt() read next value as double
boolean hasNext() for read next value as integer
later: true if input is not finished
String next() Read string up to the next whitespace3
String nextLine() read everything (or nothing) until the next line break
+ discard line breaks
Notes:
• leading whitespaces are skipped (exception: nextLine )
• it waits until EOF is reached (blocking behavior)
• Scanner can also be used with other types of input (e.g. files)
3Whitespace: space or line break
4For those interested: everything under
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html
9 hhu.de
Machine Translated by Google
What happens if no number is entered?
% java Sum one
two three
10 hhu.de
Machine Translated by Google
What happens if no number is entered?
% java Sum one
two three
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble (Scanner.java:2564) at Sum.main(Sum.java:12)
• Program crash ( InputMismatchException ) if expected data type and
input does not match
• Checking with hasNextBoolean() etc. possible •
For exercises, valid input can be assumed (for now)
10 hhu.de
Machine Translated by Google
Special feature when reading double values
Confusing behavior with doubles:
• on German systems Java expects decimal point (i.e. 3.1 instead of 3.1) • our test system is an
English system • temporarily force English as input
language when calling java:
5
LANG=en java Sum •
expected language can also be set in the code:
new Scanner(System.in).useLocale(Locale.US);
5(probably) doesn't work in the Windows CMD
11 hhu.de
Machine Translated by Google
Redirecting standard input and output6
• Motivation:
• Save program output to file
• Passing larger amounts of data to the program (impractical with arguments)
• Use the output of one program as input for another program
sign meaning Example
< Put file contents on standard input java Sum < numbers.txt
> Redirect standard output to file java HelloWorld > output.txt
| Standard output of the previous java RandomNumbers | java Sum
(“Pipe”) program with standard input of the
next program
! Warning: > overwrites the target file without confirmation!
6Piping and Redirecting Standard Input and Output
12 hhu.de
Machine Translated by Google
Example: Squaring and summing numbers
• Read numbers from text file numbers
• Calculate squares of these numbers (in 1. Java program)
• Calculate the sum of square numbers (in 2nd Java program)
• Save result in file result
Square.java Java
1 import java.util.Scanner;
2
3 public class Square {
4 public static void main(String[] args) {
5 Scanner standardInput = new Scanner(System.in);
6
7 while(standardInput.hasNext()) {
8 double nextNumber = standardInput.nextDouble();
9 System.out.println(nextNumber * nextNumber);
10 }
11 }
12 }
13 hhu.de
Machine Translated by Google
Example: Call
% cat numbers 1 -2 4
% java Square < numbers 1.0
4.0
16.0
% java Square < numbers | java Sum The sum is 21.0.
% java Square < numbers | java Sum > output % cat output The sum is 21.0.
14 hhu.de
Machine Translated by Google
learning objectives
At the end of the week you can . . .
• Read numbers from standard input . • Connect a file
to standard input . • Redirect standard output to a file . •
Connect the standard output and input of two programs in
the console .
15 hhu.de
Machine Translated by Google
Important Terms
standard input Scanner System.in
nextInt nextDouble >
Pipe | <
16 hhu.de