A token is a sequence of characters not including any whitespace.

Similar documents
Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

Reading Input from Text File

A+ Computer Science -

Lecture 8 " INPUT " Instructor: Craig Duckett

CSCI 1103: File I/O, Scanner, PrintWriter

CIS 110: Introduction to Computer Programming

A+ Computer Science -

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

CSCI 1103: File I/O, Scanner, PrintWriter

AP Computer Science Unit 1. Programs

Text User Interfaces. Keyboard IO plus

Building Java Programs

File I/O Array Basics For-each loop

Programming with Java

Topic 11 Scanner object, conditional execution

Building Java Programs

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Topic 11 Scanner object, conditional execution

EXCEPTIONS. Fundamentals of Computer Science I

COMP102: Test 1 Model Solutions

Building Java Programs

ing execution. That way, new results can be computed each time the Class The Scanner

A+ Computer Science -

Reading Text Files. 1 Reading from text files: Scanner

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Input. Scanner keyboard = new Scanner(System.in); String name;

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Repetition. Chapter 6

Repetition. Chapter 6

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Supplementary Test 1

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Fundamentals of Programming Data Types & Methods

AP Computer Science. File Input with Scanner. Copyright 2010 by Pearson Education

H212 Introduction to Software Systems Honors

Object-Oriented Programming in Java

Building Java Programs Chapter 6

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

CS 211: Existing Classes in the Java Library

A Quick and Dirty Overview of Java and. Java Programming

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Building Java Programs

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Computational Expression

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 112 Introduction to Programming

Introduction to Java Unit 1. Using BlueJ to Write Programs

CS 112 Introduction to Programming

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Chapter 4 Classes in the Java Class Libraries

More on Java. Object-Oriented Programming

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Excep&ons and file I/O

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

Building Java Programs

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Object Oriented Programming. Java-Lecture 1

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

java_help_index 08/14/18 07:14:34 walton 1 of 1

Anatomy of a Java Program: Comments

Building Java Programs

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Midterm Examination (MTA)

Building Java Programs

Building Java Programs

Using Java Classes Fall 2018 Margaret Reid-Miller

Java I/O and Control Structures

ITERATION WEEK 4: EXMAPLES IN CLASS

Java I/O and Control Structures Algorithms in everyday life

Outline. CIS 110: Introduction to Computer Programming. Exam announcements. Programming assertions revisited. An extended example: mystery

Full file at

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

HW4: Let s Play Poker

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Computer Science is...

MIDTERM REVIEW. midterminformation.htm

Files. Reading data from files. File class. Compiler error with files. Checked exceptions. Exceptions. Readings:

COMP102: Test 2 Model Solutions

Lecture Set 2: Starting Java

Data Conversion & Scanner Class

Lecture Set 2: Starting Java

Introduction to Java (All the Basic Stuff)

Full file at

Midterms Save the Dates!

Robots. Byron Weber Becker. chapter 6

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Branching. Chapter 5 11/14/16 & 11/15/16

Loops. Repeat after me

DPCompSci.Java.1718.notebook April 29, 2018

Chapter 4: Loops and Files

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Transcription:

Scanner A Scanner object reads from an input source (keyboard, file, String, etc) next() returns the next token as a String nextint() returns the next token as an int nextdouble() returns the next token as a double nextline() returns the next line as a String A token is a sequence of characters not including any whitespace.

Scanner tests You can test whether a read will succeed. hasnext() is true if next() will succeed hasnextint() is true if nextint() will succeed hasnextdouble() is true if nextdouble() will succeed hasnextline() is true if nextline() will succeed Each will pause your program if there is not enough information (eg, waiting for <return> on keyboard).

Scanner sources Common sources for a Scanner are the keyboard, a file, or just a String. Scanner a = new Scanner(System.in); // "standard input" Scanner b = new Scanner(new File("foo.txt")); // File "foo.txt" Scanner c = new Scanner("Hello\nWorld!"); // A String

Input buffer String and File Scanners read entire source into input buffer (along with end-of-input indicator). new Scanner("Hello\nWorld!"); Resulting input buffer: Hello\nWorld!<EOF> ^

Input buffer foo.txt: ---------- A AB ABC ---------- <- may end with newline or not new Scanner(new File("foo.txt")); Resulting input buffer: A\nAB\nABC<EOF> ^ A\nAB\nABC\n<EOF> ^ OR (depending if newline at end of file)

Input buffer System.in Scanner adds to buffer each <return> press. new Scanner(System.in); Resulting input buffer a"er user types Hello <return> World! <return> Boo Hello\nWorld!\n ^ <== No EOF, No Boo

What methods do next(): skip whitespace, build token, return token nextint(): skip whitespace, build token, convert to int nextline(): return everything up to next \n or EOF A\nAB\nABC\n<EOF> nextline() A\nAB\nABC\n<EOF> ^ "A" ^ A\nAB\nABC\n<EOF> nextline() A\nAB\nABC\n<EOF> ^ "AB" ^ A\nAB\nABC\n<EOF> nextline() A\nAB\nABC\n<EOF> ^ "ABC" ^

What happens next? public static void main(string[] args) { Scanner in = new Scanner("A\nAB\nABC\n"); System.out.println(":"+in.nextLine()+":"); System.out.println(":"+in.nextLine()+":"); System.out.println(":"+in.nextLine()+":"); System.out.println(":"+in.nextLine()+":"); } :A: :AB: :ABC: Exception in thread "main" java.util.nosuchelementexception: No line found at java.base/java.util.scanner.nextline(scanner.java:1651) at Untitled.main(Untitled.java:11) EOF by itself is not a line. Write small programs to answer "what if" questions.

More examples A\nAB\nABC\n<EOF> nextint() A\nAB\nABC\n<EOF> ^ Exception! ^ A\nAB\nABC\n<EOF> next() A\nAB\nABC\n<EOF> ^ "A" ^ A\nAB\nABC\n<EOF> next() A\nAB\nABC\n<EOF> ^ "AB" ^ A\nAB\nABC\n<EOF> nextline() A\nAB\nABC\n<EOF> ^ "" ^

Programming Write a program that repeatedly prompts the user for input and for each line grabs each token from the response and classifies the token as an int, double, or String. Pseudocode (big picture): prompt for line while there is a line to read process line <= could be method call prompt for line

Programming More detail: prompt for line while there is a line to read read a line while there are tokens left in the line if next token is a int read token and output "int" else if next token is an double read token and output "double" else read token and output "String" prompt for line

import java.util.scanner; public class Identifier { public static void main(string[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a line of text: "); while (in.hasnextline()) { Scanner line = new Scanner(in.nextLine()); while (line.hasnext()) { if (line.hasnextint()) { System.out.println(line.next() + " is an int"); } else if (line.hasnextdouble()) { System.out.println(line.next() + " is a double"); } else { System.out.println(line.next() + " is a String"); } } System.out.print("Enter a line of text: "); } } }

Things to note in sample program I used next() when printing results so it echoes exactly the token. If I used nextdouble() it would convert the token to a double and might change the formatting. Every int can be read as a double, so the test for int has to come before the test for double.