Question: Java I/O question Change the code to read from a text file (create the file first) using an Scanner chained to a File (instead to

Java I/O question

Change the code to read from a text file (create the file first) using an Scanner chained to a File (instead to System.in, but also NOT the FileInputStream/InputStreamReader/BufferedReader example). Instantiate a Scanner in a try block, and in the catch clause, display an error message. Make sure you don't try to call any Scanner methods if the exception was caught. (create the file first).

txt.flie

Code

import java.util.*; public class TryCustomerArray { public static Scanner scanner = new Scanner(System.in); public static void main( String [] args ){ Customer [] custArray; custArray = makeCustArray(); // etc. } // end main public static Customer [] makeCustArray() { Customer [] custArray; int numElems; String name; long acct; double bal; System.out.print("Enter number of Customers: "); numElems = scanner.nextInt(); scanner.nextLine();// clear the newline if( numElems <=0 ) // don't allow <=0 numElems = 1; custArray = new Customer[numElems]; // fill in the custArray for( int i=0; i < custArray.length; ++i ){ System.out.print("Enter Customer #" + (i+1)+ "'s name: "); name = scanner.nextLine(); System.out.print("Enter Customer #" + (i+1)+ "'s acct#: "); acct = scanner.nextLong(); System.out.print("Enter Customer #" + (i+1)+ "'s savings balance: "); bal = scanner.nextDouble(); scanner.nextLine(); // clear newline custArray[i] = new Customer(name, acct, bal); } // end for return custArray; } // end makeCustArray

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!