1 / 16

CM0551 Week 3

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;

zubin
Download Presentation

CM0551 Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. 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. 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. Some Scanner Methods

  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. 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. 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. 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. 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. Data and results • File studentAccs contains p01234 p01235 p01236 p01237 p01238 p01239 p01240 Program output [p01234,p01235,p01236,p01237,p01238,p01239,p01240]

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

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

More Related