cm0551 week 3 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CM0551 Week 3 PowerPoint Presentation
Download Presentation
CM0551 Week 3

Loading in 2 Seconds...

play fullscreen
1 / 16

CM0551 Week 3 - PowerPoint PPT Presentation


  • 103 Views
  • Uploaded on

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;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CM0551 Week 3' - zubin


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cm0551 week 3
CM0551 Week 3
  • The scanner class – file input
  • A Spelling Checker
  • Time and space requirements for various dictionary structures
scanner class
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
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
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
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
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
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
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
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
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
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
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
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 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
For next week

Complete this weeks exercises.

Next week we cover algorithms, so read up about them.