2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements...
-
Upload
phoebe-carroll -
Category
Documents
-
view
213 -
download
1
Transcript of 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements...
![Page 1: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/1.jpg)
2-1
CM0551 Week 3
• The scanner class – file input
• A Spelling Checker
• Time and space requirements for various dictionary structures
![Page 2: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/2.jpg)
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
![Page 3: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/3.jpg)
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
![Page 4: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/4.jpg)
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.
![Page 5: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/5.jpg)
2-5
Some Scanner Methods
To read this Use this
A number with no decimal point
nextInt();
A number with a decimal point nextDouble();
A word ending in a blank space next();
A line ( or what remains of the line)
nextLine();
A single character findInLine(“.”).charAT(0);
![Page 6: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/6.jpg)
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);
}
}
![Page 7: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/7.jpg)
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);
}
}
![Page 8: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/8.jpg)
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();
![Page 9: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/9.jpg)
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);
}
![Page 10: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/10.jpg)
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);
}
}
}
![Page 11: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/11.jpg)
2-11
Data and results
• File studentAccs contains
p01234
p01235
p01236
p01237
p01238
p01239
p01240
Program output[p01234,p01235,p01236,p01237,p01238,p01239,p01240]
![Page 12: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/12.jpg)
2-12
A simple spell checker methodpublic 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"));}
![Page 13: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/13.jpg)
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);
}
![Page 14: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/14.jpg)
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);}
}}
![Page 15: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/15.jpg)
2-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
![Page 16: 2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.](https://reader035.fdocuments.in/reader035/viewer/2022072010/56649dc05503460f94ab4914/html5/thumbnails/16.jpg)
2-16
For next week
Complete this weeks exercises.
Next week we cover algorithms, so read up about them.