04-scanner - • To create a Scanner for use: Scanner...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Console Input CSE 114: Computer Science I 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Streams Java input and output are treated as streams of characters (actually, bytes) A stream is a sequence of data that Fows in one direction e.g., keyboard to program, program to display Data is processed in ±rst-in, ±rst-out order 2
Background image of page 2
Basic Input In Java 1.5 or later, use the Scanner class to read from the keyboard (or other sources) Scanner provides methods that return the next value of a given type nextLine(), nextInt(), nextDouble(), etc. These methods only work if the remaining input begins with the proper type Most methods don’t consume newlines! 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How to Use Scanner You must include import java.util.*; at the top of your source code Fle!
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: • To create a Scanner for use: Scanner myScan = new Scanner(System.in); • Creates a reference (myScan) to a Scanner object • Tells Scanner to read from keyboard (System.in) 4 Scanner Example Scanner s = new Scanner(System.in); System.out.print(“Enter temperature: ”); int temp = s.nextInt(); s.nextLine(); // Dispose of extra newline System.out.print(“Enter your name: ”); String name = s.nextLine(); 5 Other Useful Scanner Methods • hasNext(), hasNextLine(), hasNextInt(), etc. • These methods return true if: • the input stream contains more data, AND • the next value that can be read is of the given type 6...
View Full Document

This note was uploaded on 04/07/2008 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 6

04-scanner - • To create a Scanner for use: Scanner...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online