1 Scanning Tokens. 2 Tokens When a Scanner reads input, it separates it into “tokens”  … at least when using methods like nextInt()  nextInt() takes.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Corso di Algoritmi e Strutture Dati con Laboratorio A.A. 2014/15 Lezione 5.1 The String and Scanner classes.
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
1 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 File Input. 2 Reading Files We have already seen that writing files is (can be?) similar to using System.out Not surprisingly, reading from files is.
Introduction to Python
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Regular Expressions. String Matching The problem of finding a string that “looks kind of like …” is common  e.g. finding useful delimiters in a file,
Expressions, Data Conversion, and Input
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Input, Output, and Processing
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Files Tutor: You will need ….
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Lecture 4.1: More on Scanner, Methods, and Codingbat Michael Hsu CSULA.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Introduction to programming in java
Input/Output.
Formatting Output.
Getting Started with C.
Formatting Output.
Computer Programming Methodology Input and While Loop
getline() function with companion ignore()
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Input and Output in Java
Introduction to Classes and Methods
Python programming exercise
Input and Output in Java
Input and Output in Java
Introduction to Java Applications
Unit 3: Variables in Java
Reading Input and Scanner Class
getline() function with companion ignore()
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

1 Scanning Tokens

2 Tokens When a Scanner reads input, it separates it into “tokens”  … at least when using methods like nextInt()  nextInt() takes the next token from the input, tries to convert it to an int, and returns it  if the conversion can’t be done (by Integer.parseInt() ?) it throws an exception

3 Delimiting Tokens You may have noticed that Scanner will use any “blank space” to separate values  i.e. it doesn’t matter if numbers in a file are separated by a space or a new line By default, Scanner uses any whitespace to delimit tokens  i.e. space, tab, newline, etc. But this isn’t always what you want

4 Changing Delimiters What if you want to read chunks of a file (or user input) separated by something else?  e.g. “1,2,3,4,5,6,7” as seven tokens separated by commas It is possible to configure the way a Scanner delimits tokens The useDelimiter() method returns a new Scanner object with different delimiter string

5 useDelimiter() Example: Scanner filein = new Scanner(…).useDelimiter(“,”); Creates a Scanner that looks for the string “,” between tokens that are read  every time it sees a comma, that will be the end of the token  the character after the comma is the start of the next token

6 Example This reads a file with comma separated tokens  Just reads the tokens as strings and prints them Scanner filein = new Scanner( new File(“file.txt”) ).useDelimiter(“,”); while(filein.hasNext()) { System.out.printf(“(%s)”, filein.next()); }

7 Inflexible Delimiters With useDelimiter(“,”), the Scanner looks strictly for a single comma character  surrounding spaces or other characters are part of the tokens, not the delimiters e.g. In this file, the third token contains a space; the fourth a newline. 1,2, 3, 4,5 Tokens: “1”, “2”, “ 3”, “\n4”, “5”

8 Inflexible Delimiters In that case, using nextInt() on the Scanner would fail  “ 3” is not a valid integer: integers don’t have spaces in them The solution: include the space or newline in the delimiter, so it doesn’t end up in the token  We can’t just use useDelimiter(“, “) since not every separator includes the space

9 More Flexible Delimiters What we need is a more flexible way to express “a comma and then maybe some whitespace” as the delimiter  we want to count any of these: “,” “, ” “,\n” “, \n” “,\t” We have no way to do that at the moment  we can only specify a single literal string  … but the default behaviour (any combination of whitespace) is like what we want, so there is hope …hmm …