Token vs Line Based Processing



Similar documents
Line-based file processing

AP Computer Science File Input with Scanner

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

13 File Output and Input

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Reading Input From A File

Introduction to Java

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Using Files as Input/Output in Java 5.0 Applications

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Topic 11 Scanner object, conditional execution

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

Chapter 2: Elements of Java

CS 121 Intro to Programming:Java - Lecture 11 Announcements

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Programming Languages CIS 443

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Python Lists and Loops

Building Java Programs

Programming Project 1: Lexical Analyzer (Scanner)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Part I. Multiple Choice Questions (2 points each):

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

First Bytes Programming Lab 2

WebFOCUS BI Portal: S.I.M.P.L.E. as can be

Building Java Programs

Arrays. Introduction. Chapter 7

Recursion and Recursive Backtracking

Compiler Construction

CS 106 Introduction to Computer Science I

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

CS170 Lab 11 Abstract Data Types & Objects

Lecture 5: Java Fundamentals III

03 - Lexical Analysis

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Building Java Programs

Explain the relationship between a class and an object. Which is general and which is specific?

Sample CSE8A midterm Multiple Choice (circle one)

csce4313 Programming Languages Scanner (pass/fail)

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

PIC 10A. Lecture 7: Graphics II and intro to the if statement

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Java 進 階 程 式 設 計 03/14~03/21

(Eng. Hayam Reda Seireg) Sheet Java

AP Computer Science Static Methods, Strings, User Input

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Sources: On the Web: Slides will be available on:

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

D06 PROGRAMMING with JAVA

Moving from CS 61A Scheme to CS 61B Java

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

JAVA ARRAY EXAMPLE PDF

J a v a Quiz (Unit 3, Test 0 Practice)

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Chapter 2 Introduction to Java programming

Interactive Programs and Graphics in Java

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Introduction to Java. CS 3: Computer Programming in Java

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Sharpen Solutions. 1 Part One. Sometimes there s more than one right answer. And sometimes the

Basics of Java Programming Input and the Scanner class

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Hypercosm. Studio.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Embedded SQL programming

Course Intro Instructor Intro Java Intro, Continued

Performance Improvement In Java Application

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Lesson 10: Video-Out Interface

IMDB Data Set Topics: Parsing Input using Scanner class. Atul Prakash

CSE 1020 Introduction to Computer Science I A sample nal exam

Programming and Software Development CTAG Alignments

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

LAB4 Making Classes and Objects

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

CS 241 Data Organization Coding Standards

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

The separation principle : a principle for programming language design

Visual Basic Programming. An Introduction

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Overview. What is software testing? What is unit testing? Why/when to test? What makes a good test? What to test?

Building Java Programs

P3PC ENZ0. ScanSnap N1800 Network Scanner Salesforce Chatter Add-in User s Guide

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Some Scanner Class Methods

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

Homework/Program #5 Solutions

ios App for Mobile Website! Documentation!

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Computer Programming I

Chapter 2 Basics of Scanning and Conventional Programming in Java

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Programmierpraktikum

Unit Testing & JUnit

Animations in DrRacket

Transcription:

Token vs Line Based Processing Mixing token based processing and line based processing can be tricky particularly on the console Problems I saw in program 5: console.next() <- the first call returns a string console.next() <- the second call returns an empty string.

Why Didn t that work? Code says: console.nextdouble() returns 2.0 User types in 2.0, then hits enter but what comes into java is: 2.0\n ^ console.next() returns Java at this point has only consumed the new line character! console.next() at this point can now accept new input from the user! 2.0\n^

Tokenizing Strings A Scanner can also work just on String objects Scanner <name> = new Scanner(<String>); Example: String text = This is a sentence"; Scanner scan = new Scanner(text); // There are 4 tokens

For Files Read in each line of the file, then use the Scanner to break apart each line. Scanner input = new Scanner(new File(sFileName)); while ( input.hasnextline() ) String line = input.nextline(); Scanner linescan = new Scanner(line); // process the line

FYI: Passing Scanner (drawthingfromfile.java) Scanner input = new Scanner( fobjectfile ); linecolor = readcolor(input); public static Color readcolor( Scanner input )

Complex Input files Requirements: Read in a file of the following format width height bkcolor red green blue color red green blue point x y Anything coded in green are integer values The width/height must be the first two integers in the file After that the bkcolor, color, and point values can be in any order The keywords must be followed by their values integer values Bring up a drawing panel, draw lines between the points, with the background color and line color last specified. Be robust in handling problems in the file.

Two Designs Design 1: Use a combination of Scanner.next(), Scanner.nextInt(), etc. See drawthingfromfile.java Design 2: Use Scanner.nextLine() to get each line of text, then tokenize each line of text. See drawthingfromfile2.java

Design 1 Getting Width and Height Scanner input = new Scanner( fobjectfile ); int width = 400; int height = 400; if ( input.hasnextint() ) width = input.nextint(); if ( input.hasnextint() ) height = input.nextint(); // just chewing a new line to // get to the next line... input.nextline();

Design 2 Getting Width and Height Scanner input = new Scanner( fobjectfile ); String sline = ""; int width = 400; int height = 400; if ( input.hasnextline() ) sline = input.nextline(); Scanner linescanner = new Scanner( sline ); if ( linescanner.hasnextint() ) width = linescanner.nextint(); if ( input.hasnextint() ) height = input.nextint();

Design 1 Reading Attributes while ( input.hasnext() ) // first get and compare the // keyword, then get the // information based on the keyword slabel = input.next(); if ( slabel!= null ) if ( slabel.equalsignorecase("color") ) linecolor = readcolor(input); if ( linecolor!= null ) g.setcolor(linecolor); else if ( slabel.equalsignorecase("bkcolor") ) bkcolor = readcolor(input); if ( bkcolor!= null ) panel.setbackground(bkcolor); else if ( slabel.equalsignorecase("point") ) if ( pold!= null ) pold.setlocation(p); if ( readpoint(input, p) ) if ( pold!= null ) g.drawline(pold.x, pold.y, else else p.x, p.y); pold = new Point(p); // error, just dump the line. input.nextline(); else input.nextline();

Design 2 Reading attributes while ( input.hasnextline() ) sline = input.nextline(); if ( sline == null sline.trim().equals("") ) continue; Scanner linescanner = new Scanner( sline ); // first get and compare the keyword, then // get the information based on the keyword slabel = linescanner.next(); if ( slabel.equalsignorecase("color") ) linecolor = readcolor(linescanner); if ( linecolor!= null ) g.setcolor(linecolor); else if ( slabel.equalsignorecase("bkcolor") ) bkcolor = readcolor(linescanner); if ( bkcolor!= null ) panel.setbackground(bkcolor); else if ( slabel.equalsignorecase("point") ) if ( pold!= null ) pold.setlocation(p); if ( readpoint(linescanner, p) ) if ( pold!= null ) g.drawline(pold.x, pold.y, p.x, p.y); else pold = new Point(p);

New Things Two new Java keywords were in Design 2: null and continue null means that there is no object. Example: String foo; If foo == null that means there is no object associated with the variable foo

The null Keyword null means that there is no object. Example: String foo; If foo == null that means there is no object associated with the variable foo if ( sline == null sline.trim().equals("") ) continue;

The continue Keyword Continue is used in loops. It means skip the rest of the controlled statements in the loop and go back to the test statement and start again. Example: while ( input.hasnextline() ) if ( sline == null sline.trim().equals("") ) continue; <skipped statements>

Input/Output and Graphics Problems that have input/output and graphical output, break the problem into pieces: Text input/output, file I/O first Welcome message, get the file name if necessary Open the file, and get the data. Process the data Produce text output Do the graphical output. Annoying but true: You can do I/O interactively with the console, but the DrawingPanel won t stay on top - even if you use DrawingPanel.toFront()