// APS101, Winter 2009: Lecture 18 (Feb. 24) // // Review: // // if-statements // // if (boolean-expression) { // then-part; // } else if (another boolean-expression) { // else if-part; // } else { // else-part; // } // // - can have many "else if" parts, but only one "else" // // while loops // // while (termination condition) { // do something; // } // // two possible problems: // - infinite loops (it never terminates) // - no loop (the termination condition is false from the start) // // PRACTICE WITH IF-STATEMENTS // // we wrote the Grades class, with method passFail Grades.passFail(0.1) Grades.passFail(98) Grades.passFail(100) Grades.passFail(0) Grades.passFail(50) Grades.passFail(49) Grades.passFail(1) Grades.passFail(-1) Grades.passFail(200) // then, we wrote method letterGrade, which prints the letter corresponding to the grade. // here is the letter grade scheme: // g >= 90 -> "A+" // g >= 80 and g < 90 -> "A" // g >= 70 and g < 80 -> "B" // g >= 60 and g < 70 -> "C" // g >= 50 and g < 60 -> "D" // g < 50 -> "F" Grades.letterGrade(55) // what other values should you test? // 0, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100 // // For Assignment 2 you will need to process Strings by splitting them into "tokens" // Tokenization: breaking a String into several parts (words, typically). // Ex. "This is a test." // This // is // a // test // . // this is only if we treat punctuation as separate tokens! (which we usually don't) import java.util.StringTokenizer; StringTokenizer st = new StringTokenizer("This is a test.") st.nextToken() st.nextToken() st.nextToken() st.nextToken() st.nextToken() // error! no more tokens. // delimiter: a character that separates the tokens // default delimiters: " \t\n\r\f" // \t: tab // \n: newline // \r: carriage return (moves cursor to first position on line) // \f: line feed (moves cursor down to next line) StringTokenizer st = new StringTokenizer("This\tis\ta\ttest.") st.nextToken() st.nextToken() st.nextToken() st.nextToken() StringTokenizer st = new StringTokenizer("This\t is a\ttest.") st.nextToken() st.nextToken() st.nextToken() st.nextToken() st.nextToken() // the number of spaces doesn't matter! StringTokenizer st = new StringTokenizer("This is a test.") // what if we want to see the number of available tokens? st.countTokens() // = 4 st.nextToken() st.countTokens() // = 3 StringTokenizer st = new StringTokenizer("This is a test.") st.hasMoreTokens() // true st.nextToken() st.hasMoreTokens() st.nextToken() st.nextToken() st.nextToken() st.hasMoreTokens() // false! st.countTokens() // = 0 StringTokenizer st = new StringTokenizer("This is a test.") // what if we want to print out all the tokens? while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // how about all on one line? Use System.out.print (rather than prinln) while (st.hasMoreTokens()) { System.out.print(st.nextToken()); } // need to put a space in between words! while (st.hasMoreTokens()) { System.out.print(st.nextToken() + " "); } // what if we want to store all the tokens in one String variable? String s = ""; StringTokenizer st = new StringTokenizer("This is a test.") while (st.hasMoreTokens()) { s = s + st.nextToken() + " "; // we "build" the String, token by token } s // what's the problem? (there's an extra space at the end of the String!) // need to remove it... s = s.substring(0, s.length() - 1)