2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Files and Streams CS 21a Chapter 11 of Horstmann.
Lecture 3 Java Basics Lecture3.ppt.
18 File handling1June File handling CE : Fundamental Programming Techniques.
1 Tirgul no. 13 Topics covered: H String parsing. H Text file I/O. H Extending Filters.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 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.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files Section 2.13, Section 8.8.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Announcements Quiz 2 Grades Posted on blackboard.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
ADSA: Maps/ Advanced Data Structures and Algorithms Objectives – –examples of maps, and introduce map collection views Semester 2,
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
BHCSI Programming Contests. Three contests Mock Contest #1  Friday, July 16 th  2:15 – 4:45pm UCF High School Online Contest  Thursday, July 22 nd.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Lab 04-2 Objectives:  Understand what a file is.  Learn how to use the File class  Learn how to use the Scanner class to read from files  Learn about.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
File Input / Output.
Streams & File Input/Output (I/O)
Introduction to programming in java
Reading from a file and Writing to a file
Input/Output.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Strings and File I/O.
Maha AlSaif Maryam AlQattan
COMPUTER 2430 Object Oriented Programming and Data Structures I
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Computer Programming Methodology File Input
INPUT STATEMENTS GC 201.
שימוש במחלקות קיימות מחרוזות, קבצים, וקבלת קלט מהמשתמש
הרצאה 12: קבצים וחריגות (Exceptions)
שימוש במחלקות קיימות מחרוזות, קבצים, וקבלת קלט מהמשתמש
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Introduction to Classes and Methods
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Optional Topic: User Input with Scanner
Presentation transcript:

2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures

2-2 Scanner class A standard Java class Can be used for keyboard input Part of JDK 1.5 import java.util.Scanner; A simple text scanner which parses primitive types and strings Uses a delimiter pattern of whitespace to identify tokens

2-3 Example Program 1 import java.util.Scanner; public class Example1 { public static void main (String [] args) { Scanner myScanner = new Scanner( System.in ); String aLine = myScanner.nextLine(); System.out.println( aLine); } keyboard

2-4 Example Program 1 cont’ import java.util.Scanner; // Tells Java to look for the Scanner class Scanner myScanner = new Scanner( System.in ); // Declares a Scanner object called myScanner String aLine = myScanner.nextLine(); // nextLine() is a method of the Scanner class // it reads a line of text from the keyboard.

2-5 Some Scanner Methods To read this Use this A number with no decimal point nextInt(); A number with a decimal pointnextDouble(); A word ending in a blank spacenext(); A line ( or what remains of the line) nextLine(); A single characterfindInLine(“.”).charAT(0);

2-6 Example Program 2 import java.util.Scanner; public class Example2 { public static void main (String [] args) { Scanner myScanner = new Scanner( System.in ); int anInt = myScanner.nextInt(); System.out.println( anInt); }

2-7 Example Program 3 import java.util.Scanner; public class Example3 { public static void main (String [] args) { Scanner myScanner = new Scanner( System.in ); int anInt = myScanner.nextInt(); double aDouble = myScanner.nextDouble(); System.out.println( “The int was “+anInt); System.out.println( “The double was “+aDouble); }

2-8 Scanner class and file input import java.util.*; import java.io.*; public class ReadAccs { public static void main(String [] args) { TreeSet accs = new TreeSet();

2-9 Scanner class and file input (2) try { readAccounts ("D:\\DSA&DP\\w3\\studentAccs.txt", accs); } catch(IOException ioe) { System.out.println("Cannot open file"); System.exit(1); } System.out.println(accs); }

2-10 Scanner class and file input (3) public static void readAccounts(String filename, TreeSet accs) throws IOException { Scanner sc = new Scanner(new FileReader(filename)); String accName; while (sc.hasNext()) { accName = sc.next(); accs.add(accName); }

2-11 Data and results File studentAccs contains p01234 p01235 p01236 p01237 p01238 p01239 p01240 Program output [p01234,p01235,p01236,p01237,p01238,p01239,p01240]

2-12 A simple spell checker method public static void spellChecker() { TreeSet dictionary = new TreeSet(); Scanner dictFile = null, docFile = null; String word; try { dictFile = new Scanner(new FileReader ("D:\\DSA&DP\\w3\\dict.txt")); docFile = new Scanner(new FileReader ("D:\\DSA&DP\\w3\\doc.txt")); }

2-13 A simple spell checker method (2) catch(IOException ioe) { System.out.println("Cannot open a file"); System.exit(1); } while (dictFile.hasNext()) { word = dictFile.next(); dictionary.add(word); }

2-14 A simple spell checker method (3) System.out.println ("The following words are not in the dictionary!"); while (docFile.hasNext()) { word = docFile.next(); if (!dictionary.contains(word)) { System.out.println(word); }

2-15 Time and space requirements for various dictionary structures time (sec)space Array and sequential search57714K Linked list and sequential search571.3M Array (sorted) and binary search.04714K Binary search tree.041M Two level packed tree.08360K Good data structure design is important

2-16 For next week Complete this weeks exercises. Next week we cover algorithms, so read up about them.