160 likes | 283 Views
CM0551 Week 3. The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures. Scanner class. A standard Java class Can be used for keyboard input Part of JDK 1.5 import java.util.Scanner;
E N D
CM0551 Week 3 • The scanner class – file input • A Spelling Checker • Time and space requirements for various dictionary structures
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
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
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.
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); } }
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); } }
Scanner class and file input import java.util.*; import java.io.*; public class ReadAccs { public static void main(String [] args) { TreeSet accs = new TreeSet();
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); }
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); } } }
Data and results • File studentAccs contains p01234 p01235 p01236 p01237 p01238 p01239 p01240 Program output [p01234,p01235,p01236,p01237,p01238,p01239,p01240]
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")); }
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); }
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); } } }
Time and space requirements for various dictionary structures time (sec) space Array and sequential search 57 714K Linked list and sequential search 57 1.3M Array (sorted) and binary search .04 714K Binary search tree .04 1M Two level packed tree .08 360K Good data structure design is important
For next week Complete this weeks exercises. Next week we cover algorithms, so read up about them.