Change Code 1 to read from a text file (create the file first) using an Scanner
ID: 3847887 • Letter: C
Question
Change Code 1 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.
Code1:
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
Customer [] custArray;
custArray = makeCustArray();
} catch (FileNotFoundException ex) {
System.out.println(ex.toString());;
}
// 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.