Starting Out with Java: Early Objects (6th Edition)
6th Edition
ISBN: 9780134462011
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 10, Problem 4AW
Program Plan Intro
Exception:
An exception is an event which takes place during the execution of a
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In Java
Create a class for a runtime exception called InvalidRangeException. Then, re-write the constructor for the Airplane class so that it throws the InvalidRangeException if the range is a negative number.
Correct the code:
1. import java.io.File; // Import the File classimport java.io.IOException; // Import the IOException class to handle errorspublic class CreateFile {public static void main(String[] args) {try {File myObj = File("filename.txt");if (myObj.createNewFile() {System.out.println("File created: " + getName());} else {System.out.println("File already exists.");}} catch (IOException) {System.out.println("An error occurred.");}}}Output:File created: filename.txt
2. import java.io.File; // Import the File classimport java.io.FileNotFoundException; // Import this class to handle errorsimport java.util.Scanner; // Import the Scanner class to read text filespublic class ReadFile {public static void main(String[] args) {myObj = new File("filename.txt");Scanner myReader = Scanner(myObj);while (myReader.hasNextLine()) {String data =nextLine();System.out.println(data);
4}myReader.close();catch (e) {System.out.println("An error occurred.");e.printStackTrace();}}}Output:Files in…
public static void connectToInputFile() {Scanner inputStream = null;String inputFileName = getFileName("Enter name of input file: ");try{inputStream = new Scanner(new File("SortedA.txt"));}catch(FileNotFoundException e){System.out.println("File " + inputFileName + " not found. ");}catch(IOException e){System.out.println("Error opening input file: " + inputFileName);}}private static String getFileName(String prompt){String fileName = null;System.out.println(prompt);Scanner keyboard = new Scanner (System.in);fileName = keyboard.next();return fileName;}public static void connectToOutputFile(){String outputFileName = getFileName("Enter name of output file: ");try{outputStream = new ObjectOutputStream(new FileOutputStream("SortedArray.csv"));}catch(IOException e){System.out.println("Error opening output file" + outputFileName);System.out.println(e.getMessage());}}public static void closeFiles(){try{inputStream.close();outputStream.close();}catch(IOException e){System.out.println("Error…
Chapter 10 Solutions
Starting Out with Java: Early Objects (6th Edition)
Ch. 10.1 - Prob. 10.1CPCh. 10.1 - Prob. 10.2CPCh. 10.1 - Prob. 10.3CPCh. 10.1 - Prob. 10.4CPCh. 10.1 - Prob. 10.5CPCh. 10.1 - Prob. 10.6CPCh. 10.1 - Prob. 10.7CPCh. 10.1 - Prob. 10.8CPCh. 10.1 - Prob. 10.9CPCh. 10.1 - When does the code in a finally block execute?
Ch. 10.1 - What is the call stack? What is a stack trace?Ch. 10.1 - Prob. 10.12CPCh. 10.1 - Prob. 10.13CPCh. 10.1 - Prob. 10.14CPCh. 10.2 - What does the throw statement do?Ch. 10.2 - Prob. 10.16CPCh. 10.2 - Prob. 10.17CPCh. 10.2 - Prob. 10.18CPCh. 10.2 - Prob. 10.19CPCh. 10.3 - What is the difference between a text file and a...Ch. 10.3 - What classes do you use to write output to a...Ch. 10.3 - Prob. 10.22CPCh. 10.3 - What class do you use to work with random access...Ch. 10.3 - What are the two modes that a random access file...Ch. 10.3 - Prob. 10.25CPCh. 10 - Prob. 1MCCh. 10 - Prob. 2MCCh. 10 - Prob. 3MCCh. 10 - Prob. 4MCCh. 10 - FileNotFoundException inherits from __________. a....Ch. 10 - Prob. 6MCCh. 10 - Prob. 7MCCh. 10 - Prob. 8MCCh. 10 - Prob. 9MCCh. 10 - Prob. 10MCCh. 10 - Prob. 11MCCh. 10 - Prob. 12MCCh. 10 - Prob. 13MCCh. 10 - Prob. 14MCCh. 10 - Prob. 15MCCh. 10 - This is the process of converting an object to a...Ch. 10 - Prob. 17TFCh. 10 - Prob. 18TFCh. 10 - Prob. 19TFCh. 10 - True or False: You cannot have more than one catch...Ch. 10 - Prob. 21TFCh. 10 - Prob. 22TFCh. 10 - Prob. 23TFCh. 10 - Prob. 24TFCh. 10 - Find the error in each of the following code...Ch. 10 - // Assume inputFile references a Scanner object,...Ch. 10 - Prob. 3FTECh. 10 - Prob. 1AWCh. 10 - Prob. 2AWCh. 10 - Prob. 3AWCh. 10 - Prob. 4AWCh. 10 - Prob. 5AWCh. 10 - Prob. 6AWCh. 10 - The method getValueFromFile is public and returns...Ch. 10 - Prob. 8AWCh. 10 - Write a statement that creates an object that can...Ch. 10 - Assume that the reference variable r refers to a...Ch. 10 - Prob. 1SACh. 10 - Prob. 2SACh. 10 - Prob. 3SACh. 10 - Prob. 4SACh. 10 - Prob. 5SACh. 10 - Prob. 6SACh. 10 - What types of objects can be thrown?Ch. 10 - Prob. 8SACh. 10 - Prob. 9SACh. 10 - Prob. 10SACh. 10 - What is the difference between a text file and a...Ch. 10 - What is the difference between a sequential access...Ch. 10 - What happens when you serialize an object? What...Ch. 10 - TestScores Class Write a class named TestScores....Ch. 10 - Prob. 2PCCh. 10 - Prob. 3PCCh. 10 - Prob. 4PCCh. 10 - Prob. 5PCCh. 10 - FileArray Class Design a class that has a static...Ch. 10 - File Encryption Filter File encryption is the...Ch. 10 - File Decryption Filter Write a program that...Ch. 10 - TestScores Modification for Serialization Modify...Ch. 10 - Prob. 11PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Output to a file can be achieved with a _____ object, which is constructed with a File and has the same methods as System.out (the class name is case sensitive)(Just Words)arrow_forwardplease edit this code so it does not contain breaks, while(true) and BufferedReader. Also keep getting this error " *** IOException *** phonebook.text (No such file or directory) the desired output has been given in the picture. here is the code to edit: import java.io.*; import java.util.Scanner; public class Phonebook { public static void main(String[] args) { int lookupCount = 0; int revLookupCount = 0; int phoneEntryCount = 0; Scanner inputReader = new Scanner(System.in); try { File file = new File("phonebook.txt"); BufferedReader phonebookReader; PhonebookEntry phonebookEntry[] = new PhonebookEntry[100]; phonebookReader = new BufferedReader(new FileReader(file)); String line = phonebookReader.readLine(); while (line != null) { if (phoneEntryCount >= 100) { throw new Exception( "Phonebook capacity exceeded - increase size of underlying array"); } phonebookEntry[phoneEntryCount++] = new PhonebookEntry(line); line = phonebookReader.readLine(); } phonebookReader.close(); mainLoop:…arrow_forwardJava Program ASAP Modify this program so it passes the test cases in Hypergrade becauses it says 5 out of 7 passed. import java.io.*;import java.util.Scanner;public class FileSorting { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("Please enter the file name or type QUIT to exit:"); while (true) { String input = sc.next(); if (input.equalsIgnoreCase("QUIT")) { break; // Exit the program } else { String filePath = new File("").getAbsolutePath() + "/" + input; File file = new File(filePath); if (file.exists() && !file.isDirectory()) { try (BufferedReader br = new BufferedReader(new FileReader(file))) { String st; StringBuilder formattedText = new StringBuilder(); while ((st = br.readLine()) != null) {…arrow_forward
- The previous program works well as longas the user enters an integer. Otherwise, you may get another kind ofexception. For instance, if you use nextInt() of Scanner, you could have anInputMismatchException.Modify it to prevent users entering anythingother than an integer.arrow_forwardI want to throw an IllegalArgumentException type exception in my code even if it does not arise on its own. Provide a line of code that could instantiate and throw such exception. Make sure to provide a complete compilable line of code, including the semicolon.arrow_forwardWrite a program that will create and handle NoSuchMethodException. Your method must compile and run successfully. You must explain at the end of the program how did you make the program compile and run successfullyarrow_forward
- can i have output pleaseimport java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ScannerFromFileWithCatch { public static void main(String[] args) { try { Scanner in = new Scanner(new File("test.in")); // do somthing if no exception } catch (FileNotFoundException ex) { ex.printStackTrace(); // System.out.println("Hello Exception"); } finally { System.out.println("Thank You !"); } } }arrow_forwardpublic static void doSomething(String str) { System.out.println(str); } public static void main(String[] args) { try { Scanner fileHandler = new Scanner(new File("file.txt")); while(fileHandler.hasNext()) { doSomething(fileHandler.nextLine()); } }catch(IOException io) { // what happens if the file is not there?? } } Contents of file.txt are: Hello CS 1 I hope you are enjoying, yourself!arrow_forwardDoes the class PrintWriter have a constructor that accepts a string (for a filename) as an argument, so that the following code would be legal?PrintWriter outputStream =new PrintWriter("stuff.txt");arrow_forward
- Java Program ASAP Modify this program so it passes the test cases in Hypergrade. Also change the public class to FileSorting import java.io.*;import java.util.Scanner;public class ConvertText { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("Please enter the file name or type QUIT to exit:"); while (true) { String input = sc.next(); if (input.equalsIgnoreCase("QUIT")) { break; // Exit the program } else { String filePath = new File("").getAbsolutePath() + "/" + input; File file = new File(filePath); if (file.exists() && !file.isDirectory()) { try (BufferedReader br = new BufferedReader(new FileReader(file))) { String st; StringBuilder formattedText = new StringBuilder(); while ((st = br.readLine()) != null)…arrow_forwardExpectedException is found under which package? a) org.junit.ExpectedException b) org.junit.Test.ExpectedException c) org.junit.Rule.ExpectedException d) org.junit.rules.ExpectedExceptionarrow_forwardedit and finish class authenticate below do not give a solution (example copying from another source and giving it as a solution) that is not part of my code below. Also provided is user class. This is what I have so far please help me finish it. The task is listed below //Authenticate.java import java.util.Scanner;import java.io.File; Class Authenticate {private final int SIZE = 100;private User() users = new User[SIZE];public Authenticator (String fileName) throws Exception;Scanner sc = new Scanner(new File(fileName));int i = 0;While(sc.hasNext() && i < SIZE) {users[i] = Users.read(sc);i++}} public void authenticate(String username, String password) throws Exception{try {User u = null;for(User X : users) {if(x.getUsername().equals(username) && x.verifyPassword(password){ return ; _________________________________________________________________ //User.java import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner; public class User{private…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- COMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE LEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT