Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures."— Presentation transcript:

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

2 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

3 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

4 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.

5 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);

6 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); }

7 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); }

8 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();

9 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); }

10 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); }

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

12 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")); }

13 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); }

14 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); }

15 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

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


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

Similar presentations


Ads by Google