Basic input and output in Java
Madhavan Mukund
[Link]
Programming Concepts using Java
Week 2
Interacting with a Java program
We have seen how to print data
[Link]("hello, world");
How do we read data
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 2/4
Reading input
Simplest to use is the Console class
Functionality similar to Python
input()
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 3/4
Reading input
Simplest to use is the Console class Console cons = [Link]();
Functionality similar to Python String username =
input() [Link]("User name: ");
char[] passwd =
Defined within System [Link]("Password: ");
Two methods, readLine and
readPassword
readPassword does not echo
characters on the screen
readLine returns a string (like
Python input())
readPassword returns an array of
char — for security reasons
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 3/4
Reading input
Simplest to use is the Console class Console cons = [Link]();
Functionality similar to Python String username =
input() [Link]("User name: ");
char[] passwd =
Defined within System [Link]("Password: ");
Two methods, readLine and
readPassword More general Scanner class
readPassword does not echo Allows more granular reading of input
characters on the screen Read a full line, or read an integer, . . .
readLine returns a string (like
Python input()) Scanner in = new Scanner([Link]);
readPassword returns an array of String name = [Link]();
char — for security reasons int age = [Link]();
....
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 3/4
Generating output
[Link](arg) prints arg and goes
to a new line
Implicitly converts argument to a string
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 4/4
Generating output
[Link](arg) prints arg and goes
to a new line
Implicitly converts argument to a string
[Link](arg) is similar, but does not
advance to a new line
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 4/4
Generating output
[Link](arg) prints arg and goes
to a new line
Implicitly converts argument to a string
[Link](arg) is similar, but does not
advance to a new line
[Link](arg) generates formatted
output
Same conventions as printf in C
Read the documentation
Madhavan Mukund Basic input and output in Java Programming Concepts using Java 4/4