COMPUTER 2430 Object Oriented Programming and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I 1
Prog 3 • Fixed. Point. List – add – delete – min – max – sum – to. String 2
Prog 3 import java. io. File; import java. util. Scanner; public class Prog 3 { private static Scanner stdin; private static int cur. Q = 12; private static Fixed. Point. List list = new Fixed. Point. List(); public static void main( String args[] ) { try { stdin = new Scanner( new File("Prog 3_1. in") ); } catch (Exception ex) { stdin = new Scanner( System. in ); }. . . } } 3
Prog 3 public class Prog 3 { private static Scanner stdin; private static int cur. Q = 12; private static Fixed. Point. List list = new Fixed. Point. List(); public static void main( String args[] ) {. . . while (stdin. has. Next()) { String cmd = stdin. next(); char cmd. Char = cmd. char. At(0); // procee commands } } } 4
Prog 3 public class Prog 3 { private static Scanner stdin; private static int cur. Q = 12; private static Fixed. Point. List list = new Fixed. Point. List(); public static void main( String args[] ) {. . . while (stdin. has. Next()) { String cmd = stdin. next(); char cmd. Char = cmd. char. At(0); // procee commands } } private static void add. Cmd() { // get command parameter and process the command } } 5
String Method split import java. io. File; Import java. util. Scanner; public static void main( String args[] ) { Scanner stdin; . . . String cmd. Line; String tokens[]; cmd. Line = stdin. next. Line(); // “ 243. 15 5” // space “ ” is the only delimiter tokens = cmd. Line. split(" "); // tokens[0]: “ 243. 15” // tokens[1]: “ 5”. . . } 6
Parse Methods String cmd. Line; String tokens[]; cmd. Line = stdin. next. Line(); // “ 243. 15 5” tokens = cmd. Line. split(" "); // tokens[0]: “ 243. 15” // tokens[1]: “ 5” double dbl = Double. parse. Double(tokens[0]); int q. Val = Integer. parse. Int(tokens[1]); 7
The First Token! import java. io. *; Import java. util. Scanner; public static void main( String args[] ) throws IOException { Scanner stdin = new Scanner( System. in ); . . . String cmd. Line; String tokens[]; cmd. Line = stdin. next. Line(); // “ 243. 15 5” tokens = cmd. Line. split(" "); // tokens[0]: “” // tokens[1]: “ 243. 15” // tokens[2]: “ 5”. . . } 8
Multiple Spaces (delimiters) public static void main( String args[] ) throws IOException { Scanner stdin = new Scanner( System. in ); . . . String cmd. Line; String tokens[]; cmd. Line = stdin. next. Line(); // “ 243. 15 5” tokens = cmd. Line. split(" "); // tokens[0]: “” // tokens[1]: “ 243. 15” // tokens[2]: “” // tokens[3]: “” // tokens[4]: “ 5”. . . } 9
Parsing String for Date • • String to parse: “ 9/30/2009” Delimiter: “/” cmd. Line = stdin. next. Line(); tokens = cmd. Line. split(“/"); // tokens[0] = “ 9” // tokens[1] = “ 30” // tokens[2] = “ 2009” 10
Parsing String for Complex • • String to parse: “(3. 5, 7. 213)” Delimiters: “(, )” cmd. Line = stdin. next. Line(); tokens = cmd. Line. split(“(, )"); // tokens[0] = “(3. 5” // tokens[1] = “ 7. 213)” 11
Parsing String for Complex • • String to parse: “(3. 5, 7. 213)” Delimiters: “(, )” cmd. Line = stdin. next. Line(); tokens = cmd. Line. split(“[(, )]"); // tokens[0] = “” // tokens[1] = “ 3. 5” // tokens[2] = “ 7. 213” 12
Net. Beans 13
Quiz 3 14
- Slides: 14