• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/4

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

4 Cards in this Set

  • Front
  • Back

Exception Handling

different ways allow the caller to deal with the exceptions that occur within the program instead of allowing the methods to deal with it (which usually involves completely terminating the program).




a throw new statement allows the method to throw an exception if that exception is found, and if not the code runs normally.




the try block contains the code that would run under normal circumstances, while the catch block is the portion where it deals with the exception if there is one after running the try block.




Syntax:




try {


code to run;


a statement or method that may throw an exception;


more code to run;


}


catch(type ex) {


code to express the exception


}











Recursion

the idea of calling one function from another immediately suggests the possibility of a function calling itself. the function-call mechanism in Java supports this possibility, known as recursion.





Files

The File class contains methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory.




The Scanner allows us to read the information inside the file, while the PrintWriter allows us to create a new file and write data to the text file.




Scanner


import java.util.Scanner;




Syntax:


Scanner input = new Scanner (new File(filename));




EXAMPLE


import java.util.Scanner;


Scanner input = new Scanner (System.in);


Scanner output = null; //this reads the file


boolean x = false;


String fileName;


while (x) {


System.our.println("Enter the file's name: ");


fileName = input.next(); //first find your file


try{


File fileN = new File(fileN ".txt");


output = new Scanner(fileN);


boolean existsFile = fileN.exists();


if(existFile == true){


System.out.println("File" + fileName + "successfully opened!");


x = true;


}


catch(FileNotFoundException ex){


System.out.println("FileNotFoundEception: the file cannot be found");


}


Now you can read the file by simply doing:




input.nextLine(); //reads one complete line of the file, an empty space if there is one



PrintWriter

-creates a new file with written information in it


IO: stands for input/output




import java.io.PrintWriter;




Syntax:


PrintWriter output = new PrintWriter (filename);




Example:


System.out.println("Enter the file's name: ");


String fileName = input.next();


File files = new File(fileName + ".txt");


Scanner x = new Scanner(files);


PrintWriter overwrite = nulll;




String name = x.nextLine();


String color = x.nextLine(); //etc...




//then use PrintWriter to create a new file based on the read information and write information on it


overwrite = new PrintWriter(filename + "_revised.txt"); //the new file created




overwrite.println("Files inside of here...");


//what is being written inside of file