Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150,

Similar presentations


Presentation on theme: "C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150,"— Presentation transcript:

1

2 C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150, dewan@unc.edu)

3 2 S CANNING P ROBLEM Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program

4 3 S CANNING P ROBLEM Program name (First) Argument or option to the program output

5 4 S CANNING JohnF.Kenndye token Input stream Input stream Token Stream Token Stream token

6 5 A LGORITHM JohnF.Kenndye marker 0 Output: J

7 6 A LGORITHM JohnF.Kenndye marker 1 Output: J String inputLine

8 7 A LGORITHM JohnF.Kenndye marker 2 Output: J

9 8 A LGORITHM JohnF.Kenndye marker 5 Output: JF

10 9 A LGORITHM JohnF.Kenndye marker 6 Output: JF

11 10 A LGORITHM JohnF.Kenndye marker 8 Output: JFK

12 11 A LGORITHM JohnF.Kenndye marker 9 Output: JFK

13 12 A LGORITHM JohnF.Kenndye marker 14 Output: JFK

14 13 S OLUTION ( EDIT IN CLASS ) package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ }

15 14 S OLUTION public class AnUpperCasePrinter { public static void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } System.out.println("Upper Case Letters:"); int index = 0; while (index < args[0].length()) { if (Character.isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index)); index++; } System.out.println(); } Print on new vs previous line

16 15 D ETECTING U PPER C ASE public class AnUpperCasePrinter { public static void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } System.out.println("Upper Case Letters:"); int index = 0; while (index < args[0].length()) { char nextLetter = args[0].charAt(index) ; if (nextLetter >= ‘A’ && nextLetter <= ‘Z’ ) System.out.print(nextLetter); index++; } System.out.println(); } Successive uppercase letters/lowercase letters/digits are successive characters in Java’s ordered character sequence

17 16 D EMO Scanning Algorithm Demo


Download ppt "C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150,"

Similar presentations


Ads by Google