1 / 12

More API

More API. CSCE 190 – Java Instructor: Joel Gompert July 23, 2004. Converting From Strings to numbers. String s1 = "3546"; int i=0; try { i = Integer.parseInt(s1); } catch(NumberFormatException e) { System.out.println("Not a valid number."); }. Exception Handling. try {

liora
Download Presentation

More API

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. More API CSCE 190 – Java Instructor: Joel Gompert July 23, 2004

  2. Converting From Strings to numbers String s1 = "3546"; int i=0; try { i = Integer.parseInt(s1); } catch(NumberFormatException e) { System.out.println("Not a valid number."); }

  3. Exception Handling try { // code that may throw exceptions }

  4. Exception Handling try { // code that may throw exceptions } catch(exception_type e) { // code to handle an exception }

  5. Exception Handling try { // code that may throw exceptions } catch(exception_type e) { // code to handle an exception } finally { // code always to be executed after the try block }

  6. Converting from String to float String s2 = "3485.934"; float f=0; try { f = Float.parseFloat(s2); } catch(NumberFormatException e) { System.out.println("Not a valid number."); }

  7. File Input/Output • File I/O classes in the API • Characters • java.io.FileReader • java.io.FileWriter • Bytes • java.io.FileInputStream • java.io.FileOutputStream • java.io.PrintWriter • Can make outputting text easier • java.io.StreamTokenizer • Can make inputting text eaiser

  8. File Output Example FileWriter fwriter; try { fwriter = new FileWriter("test.txt"); } catch(IOException e) { System.out.println("Unable to open file."); return; } PrintWriter pwriter = new PrintWriter(fwriter); int x = 42; pwriter.println("This is a test"); pwriter.println("Let's output a number: " + x); try { fwriter.close(); } catch(IOException e) { System.out.println("Unable to close file."); }

  9. File Input Example FileReader freader; try { freader = new FileReader("test2.txt"); } catch(IOException e) { System.out.println("Unable to open file."); return; } StreamTokenizer tokenizer = new StreamTokenizer(freader); tokenizer.parseNumbers();

  10. File Input Example try { int type = tokenizer.nextToken(); while(type != StreamTokenizer.TT_EOF) { switch(type) { case StreamTokenizer.TT_WORD: System.out.println(tokenizer.sval); break; case StreamTokenizer.TT_NUMBER: System.out.println(tokenizer.nval); break; } type = tokenizer.nextToken(); } }catch(IOException e) {System.out.println("Error reading file.");} try{ freader.close(); } catch(IOException e) { System.out.println("Unable to close file."); }

  11. File class • java.io.File • Used to refer to files and directories • java.awt.FileDialog • Used to ask the user to choose a file

  12. File Dialog Example Frame frame = new Frame(); frame.show(); FileDialog fd = new FileDialog(frame); fd.show(); String filename = fd.getFile(); String directory = fd.getDirectory(); System.out.println("You chose the file: " + filename); System.out.println("In the directory: " + directory);

More Related