1 / 19

OOP: Java java8b

StringTokenizer Class. It takes an input string and parses it into "tokens", allowing the tokens to be read one at a time. call the nextToken method for each item you want tokenizedcall the nextToken method for each element and assign to instance variable ex: name = st.nextToken(); ex: qpa=Dou

delano
Download Presentation

OOP: Java java8b

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. OOP: Java (java8b.ppt) String Tokenizing (3.1.1) Streams & File I/O (4.1.1) User-Defined Exceptions (3.3.4) Ex 7 Dow Jones Case Study

    2. StringTokenizer Class It takes an input string and parses it into "tokens", allowing the tokens to be read one at a time. call the nextToken method for each item you want tokenized call the nextToken method for each element and assign to instance variable ex: name = st.nextToken(); ex: qpa=Double.parseDouble(st.nextToken());

    3. StringTokenizer Class Methods stringTokenizer(String str, String DELIM) - constructor nextToken() - Parses the next token from the input stream of this tokenizer. hasMoreTokens() returns true if more tokens exist countTokens() - returns # of tokens in this tokenizer Lets Look at StringTokenizerClassDemo

    4. Streams & File I/O Stream refers to any source of input or output in a Java program. Java has several classes to handle I/O. Check the API under java.io We have used two of these: BufferedReader to handle the input stream PrintWriter to handle the output stream

    5. Review of Input/Output in Java Java uses two constants: in to represent standard input from the keyboard out to represent standard output to the console window in belongs to the InputStream Class System.in out belongs to the OutputStream Class System.out

    6. Review of User Input/Output in Java To handle the input stream we use readLine() Then we can use the wrapper classes for each type To handle the output stream we use several methods println(), print() and flush() files can be handled as input streams as well

    7. Tokenizers in Java Files in general have more than one piece of data per line Joe White 3.45 29 Janet Blue 3.75 31 Each line has more than one item, or token In Java, we can read a line to determine how many tokens it has and what their values are. There are two classes : StringTokenizer and StreamTokenizer.

    8. Tokenizers in Java The tokens are separated by white space characters blank spaces tabs (vertical and horizontal) form feed char new line

    9. StringTokenizer & Exceptions Need to handle exceptions Bad numeric data types Lines with too few or too many elements Can use multiple catch clauses to handle different exceptions

    10. StringTokenizer & Exceptions Need to handle exceptions Bad numeric data types Lines with too few or too many elements Can use multiple catch clauses to handle different exceptions

    11. StringTokenizer & Exceptions NumberFormatException NoSuchElementException Thrown when you attempt to get a token and it does not exist Ex: data required to have 5 tokens It has only 4 when you attempt the get the 5th An exception is thrown

    12. File Input/Output in Java Text file are commonly used for sources of input and output All we need to do is to replace the in and out constants with another object that relates to a file Files have a name and a path (location) a:\mydemos\stocks.txt ==> a:\\mydemos\\stocks.txt on Windows/NT c:\temp\users\data.txt ==> c:\\temp\\users\\data.txt Otherwise, data files must be in the same directory as your .java program!

    13. File Input/Output in Java There are many classes to handle files Disk files are supported by the classes FileReader and FileWriter. Data files are instances of either class. To read from a file (using a file as input source) use FileReader. To write to a file (using a file as output source) use FileWriter.

    14. File Input/Output in Java Files need to be open before you use them instantiate a file Program must make sure that a file has been opened before it tries to read data from it! Files should be closed when we are done using them use the close() method Lets look at examples for reading text files: FileReadStrings, +ReadTextFile, +ReadTextFile2, FileReadAddresses And doing both I/O: Prices, +Copy

    16. Interfaces An interface is not part of the class hierarchy An interface is not a class, and cannot be used to instantiate an object Constants in an interface can be used in the implementing class as if they were declared locally This feature provides a convenient technique for distributing common constant values among multiple classes See online java notes ch 53

    17. Creating Exception Classes You can create your own exception classes to handle exceptions Such a class must be derived from class Throwable You can be more specific and extend classes further down the hierarchy class MyException extends Exception {..} printStackTrace() You can trace the methods the computer uses to find a catch block See Digit, DigitOutOfBoundsException, DigitTester

    18. finally Keyword finally defines a block of code guaranteed to be executed before the computer exits a method regardless of whether an exception was thrown or return statement was executed in a try block syntax: finally { .... statements } See ch9Ex6.java

    19. EX Dow Jones Assignment See Class Handout Case Study Specs Data Files Sample Executable Hw7exe.jar Class Hierarchy UML View Support material on WAN Overall Project Design Design & Test of each class

More Related