SlideServe
Menu
Préférences
© SlideServe · v2026

Converting Strings to Numbers in Java: Exception Handling and File I/O Basics

liora avatar
12 diapositives · 264 Views · Sep 16, 2014

This tutorial explores essential Java concepts, including converting strings to integers and floats, handling exceptions, and performing file input/output operations. You'll learn how to safely parse numerical strings using `Integer.parseInt` and `Float.parseFloat`, with exception handling to manage invalid inputs. Furthermore, we delve into file operations, utilizing `FileReader`, `FileWriter`, and `PrintWriter` to read from and write to text files while handling potential I/O exceptions. Enhance your Java programming skills with practical examples and techniques.

Converting Strings to Numbers in Java: Exception Handling and File I/O Basics - slide 1
01 / 12Converting Strings to Numbers in Java: Exception Handling and File I/O Basics
1 / 17

Télécharger la présentation

Converting Strings to Numbers in Java: Exception Handling and File I/O Basics
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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);