console input & output css 161: fundamentals of computing joe mccarthy 1

31
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Upload: tamsin-jackson

Post on 31-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Console Input & Output

CSS 161: Fundamentals of Computing

Joe McCarthy

1

Page 2: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

System.in & System.out

• Java objects connected to the consoleo System.in: keyboard (instance of InputStream)o System.out: screen (instance of PrintWriter)

CSS 161: Fundamentals of Computing 2

System.in System.out

Page 3: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Console Output• System.out

– Java PrintStream object associated with console (screen)• print(), println()

– Methods for writing to a PrintStream object – For now, we will use them only with System.out

• Syntax:– System.out.print(expression)– System.out.println([expression])

• Semantics:– Evaluate expression, output its value– println:

• expression is optional (hence the square brackets [])• output a carriage return / line feed (newline) at end of line

CSS 161: Fundamentals of Computing 3

Page 4: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Formatted output: printf

• Syntax:System.out.printf(format[,expression]*)

• Semantics:Use format string to output 0 or more expressions

• Examples:System.out.printf("Hello");System.out.printf("Hello\n");System.out.printf("%5.1f", 1.25);System.out.printf("%-5.1f", 1.25);System.out.printf("$.2f", 1.2);

CSS 161: Fundamentals of Computing 4

Page 5: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Format string specifiers

CSS 161: Fundamentals of Computing 5

Page 6: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Escape characters in format strings

CSS 161: Fundamentals of Computing 6

Page 7: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Console input: Scanner class• First: not built-in, so have to import

import java.util.Scanner;• Next: declare & initialize an instance of the class

Syntax: • classname variable = new classname([args])

Semantics:• Declare an instance of classname, initialize it (possibly with args),

assign instance to variableExample:

• Scanner keyboard = new Scanner(System.in);• Create an instance of the Scanner class • Initialize it to accept input from the system console (System.in)• Assign it to keyboard (NB: keyboard not a reserved word)

CSS 161: Fundamentals of Computing 7

Page 8: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Console Input Using the Scanner Class

• The method nextInt reads one int value typed in at the keyboard and assigns it to a variable:int numberOfPods = keyboard.nextInt();

• The method nextDouble reads one double value typed in at the keyboard and assigns it to a variable:double d1 = keyboard.nextDouble();

• Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate methodo Whitespace is any string of characters, such as blank spaces, tabs,

and line breaks that print out as white space

2-8 CSS 161: Fundamentals of Computing

Page 9: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Console Input Using the Scanner Class

• The method next reads one String of non-whitespace characters delimited by whitespace characters such as blanks or the beginning or end of a line

• Given the codeString word1 = keyboard.next();String word2 = keyboard.next();

and the input linejelly beans

The value of word1 would be “jelly”, and the value of word2 would be “beans”

2-9 CSS 161: Fundamentals of Computing

Page 10: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Console Input Using the Scanner Class

• The method nextLine reads an entire line of keyboard input• The code,

String line = keyboard.nextLine();reads in an entire line and places the string that is read into the variable line

• The end of an input line is indicated by the escape sequence '\n'o This is the character input when the Enter key is pressedo On the screen it is indicated by the ending of one line and the beginning

of the next line• When nextLine reads a line of text, it reads the '\n' character,

so the next reading of input begins on the next lineo However, the '\n' does not become part of the string value returned

(e.g., the string named by the variable line above does not end with the '\n' character)

2-10 CSS 161: Fundamentals of Computing

Page 11: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Keyboard Input Demonstration (Part 1 of 2)

2-11 CSS 161: Fundamentals of Computing

Page 12: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Keyboard Input Demonstration (Part 2 of 2)

2-12 CSS 161: Fundamentals of Computing

Page 13: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Another Keyboard Input Demonstration (Part 1 of 3)

2-13 CSS 161: Fundamentals of Computing

Page 14: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Another Keyboard Input Demonstration (Part 2 of 3)

2-14 CSS 161: Fundamentals of Computing

Page 15: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Another Keyboard Input Demonstration (Part 3 of 3)

2-15 CSS 161: Fundamentals of Computing

Page 16: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Pitfall: Dealing with the Line Terminator, '\n'

• The method nextLine of the class Scanner reads the remainder of a line of text starting wherever the last keyboard reading left off

• This can cause problems when combining it with different methods for reading from the keyboard such as nextInt

• Given the code,Scanner keyboard = new Scanner(System.in);int n = keyboard.nextInt();String s1 = keyboard.nextLine();String s2 = keyboard.nextLine();

and the input,2Heads are better than1 head.

what are the values of n, s1, and s2?

2-16 CSS 161: Fundamentals of Computing

Page 17: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Pitfall: Dealing with the Line Terminator, '\n'

• Given the code and input on the previous sliden will be equal to "2",s1 will be equal to "", ands2 will be equal to "heads are better than"

• If the following results were desired instead n equal to "2", s1 equal to "heads are better than", ands2 equal to "1 head"

then an extra invocation of nextLine would be needed to get rid of the end of line character ('\n')

2-17 CSS 161: Fundamentals of Computing

Page 18: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Methods in the Class Scanner (Part 1 of 3)

2-18 CSS 161: Fundamentals of Computing

Page 19: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Methods in the Class Scanner (Part 2 of 3)

2-19 CSS 161: Fundamentals of Computing

Page 20: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Methods in the Class Scanner (Part 3 of 3)

2-20 CSS 161: Fundamentals of Computing

Page 21: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Java Documentation for Scanner

CSS 161: Fundamentals of Computing

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

21

Page 22: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Programming Tip: Prompt for Input

• A program should always prompt the user when he or she needs to input some data:System.out.println(

"Enter the number of pods followed by");

System.out.println(

"the number of peas in a pod:");

2-22 CSS 161: Fundamentals of Computing

Page 23: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Programming Tip: Echo Input

• Always echo all input that a program receives from the keyboard

• In this way a user can check that he or she has entered the input correctlyo Even though the input is automatically

displayed as the user enters it, echoing the input may expose subtle errors (such as entering the letter "O" instead of a zero)

2-23 CSS 161: Fundamentals of Computing

Page 24: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Self-Service Checkout Line (Part 1 of 2)

2-24 CSS 161: Fundamentals of Computing

Page 25: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Self-Service Checkout Line (Part 2 of 2)

2-25 CSS 161: Fundamentals of Computing

Page 26: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

The Empty String

• A string can have any number of characters, including zero characterso "" is the empty string

• When a program executes the nextLine method to read a line of text, and the user types nothing on the line but presses the Enter key, then the nextLine Method reads the empty string

2-26 CSS 161: Fundamentals of Computing

Page 27: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Other Input Delimiters• The delimiters that separate keyboard input can be changed

when using the Scanner class• For example, the following code could be used to create a Scanner object and change the delimiter from whitespace to "##"Scanner keyboard2 = new Scanner(System.in);Keyboard2.useDelimiter("##");

• After invocation of the useDelimiter method, "##" and not whitespace will be the only input delimiter for the input object keyboard2

2-27 CSS 161: Fundamentals of Computing

Page 28: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Changing the Input Delimiter (Part 1 of 3)

2-28 CSS 161: Fundamentals of Computing

Page 29: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Changing the Input Delimiter (Part 2 of 3)

2-29 CSS 161: Fundamentals of Computing

Page 30: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Changing the Input Delimiter (Part 3 of 3)

2-30 CSS 161: Fundamentals of Computing

Page 31: Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1

Next Time(s)

• Wednesday: o Section 3.1: Branching mechanismso Assignment 1 due (3:30pm)o Assignment 2 out

• Friday:o Lab (CSSSKL 161 A): UW1-120, 11:00-1:30

31CSS 161: Fundamentals of Computing