Upload
zoe-bond
View
219
Download
2
Embed Size (px)
Citation preview
Review of Week 2 topics
• Declaring variables– int cansPerPack = 6;– String str = "Harry Potter";
• Number types– int , double
2
Review of Week 2 topics (2)
• Keyboard input: nextInt() – reads an integer value nextDouble() – reads a floating-point valuenextLine() – reads a line of text string up to but not
including the newline character.next() – reads the next whitespace-delimited string,
that is, only one word is read.
• String operations:–substring(startPosition, endPosition(optional)) e.g.str.substring(0, 5); str.substring(6); –length(), + symbol for string concatenation.
3
Keyboard input using the Scanner Class
import java.util.Scanner;/* The package java.util contains the class java.util.Scanner.
A package is a collection of classes with a related purpose. The import statement is typed in as the first line of the program before the definition of the class which uses java.util.Scanner.
*/
Scanner in = new Scanner(System.in);/* new is used to create a new object of type Scanner.
The name of the new object is called in to read keyboard input. Note that new is a reserved word, and “Scanner” on the left acts like a type declaration.System.in identifies the keyboard as the source of input.
*/4