Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java I/O question Change the code to read from a text file (create the file firs

ID: 3847936 • Letter: J

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 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

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author Namburi Ramesh
*/
import com.sun.org.apache.bcel.internal.generic.F2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TryCustomerArray {
public static Scanner scanner1,scanner2;
public static void main( String [] args ){
try {
scanner1=new Scanner(new File("C:\Users\Namburi Ramesh\Desktop\input.txt")); //for counting number of lines in input file
scanner2=new Scanner(new File("C:\Users\Namburi Ramesh\Desktop\input.txt")); //for iterating through the file
} catch (FileNotFoundException ex) {
System.out.println(ex.toString());;
}
Customer [] custArray;

custArray = makeCustArray();

// etc.
} // end main

public static Customer [] makeCustArray() {
  
Customer [] custArray;   
int numElems;
String name;
long acct;
double bal;
int counter=0;
while(scanner1.hasNext()){
counter++;
scanner1.next();
  
}
custArray=new Customer[counter];
int i=0;
while(scanner2.hasNext()){
  
StringBuilder line=new StringBuilder(scanner2.nextLine());
name=line.substring(0, line.indexOf(","));
line.replace(0, line.indexOf(",")+1, "");
acct=Long.valueOf(line.substring(0, line.indexOf(",")));
line.replace(0, line.indexOf(",")+1, "");
bal=Double.valueOf(line.toString());
custArray[i]=new Customer(name, acct, bal);
i++;
}
  
return custArray;
}
}// end makeCustArray

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote