//Java// Convert a file that requires user input, to read input from a text file
ID: 3723158 • Letter: #
Question
//Java// Convert a file that requires user input, to read input from a text file.
input.txt file
12345
Kate Stephens
100.00
25.00
28.42
15.50
1.29
18.2
10.5
89.55
25.55
21.57
18.52
--------------------------------------------------------------
java program
------------------------------------------------------------
import java.util.Scanner;
/**
This program is used to calculate a users Account Balance after withdrawals and deposits.
*/
public class AccountBal
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // scanner to read input
//declare variables
int accountNumber; //Account Number Place holder
String fname; //First name Place holder
String lname; //Last name Place holder
float balance; //Balance Place holder
float withdrawlTotal=0; //Total withdrawals
int withdraws=0; //withdraw
float depositTotal=0; //total deposits
int deposits=0; //Deposits
// get account number
System.out.println("Enter Account Number:");
accountNumber = sc.nextInt();
// get first name
System.out.println("Enter Customer First Name");
fname = sc.next();
//get last name
System.out.println("Enter Customer Last Name:");
lname = sc.next();
//get original account balance
System.out.println("Enter Original Account balance:");
balance = sc.nextFloat();
// 5 withdrawals from account balance from user (or exit with -1)
for(int i=0;i<5;i++)
{
System.out.println("Enter withdrawl amount, or type -1 to exit:");
float temp = sc.nextFloat();
if(temp==-1)
{
break;
}
withdraws++;
withdrawlTotal += temp;
}
// get 5 deposits from account balance from user (or exit with -1)
for(int i=0;i<5;i++)
{
System.out.println("Enter deposit amount, or type -1 to exit:");
float temp = sc.nextFloat();
if(temp==-1)
{
break;
}
deposits++;
depositTotal += temp;
}
// output results on screen for user to see
System.out.printf("%d - %s %s ",accountNumber,fname,lname); //Displays Account number, first and last name
System.out.printf("Original Balance: $%.2f ",balance); //Displays account balance
System.out.println("Withdrawls:"+withdraws); //Displays withdrawals
System.out.printf("withdrawl Total:$%.2f ",withdrawlTotal); //Displays withdraw totals
System.out.println("Deposits:"+deposits); //Displays deposits
System.out.printf("Deposit Total:$%.2f ",depositTotal); //Displays deposit totals
System.out.printf("Balance:$%.2f ",(balance-withdrawlTotal+depositTotal)); //Displays end balance
System.exit(0); //Program end
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
public class AccountBal
{
public static void main(String[] args)
{
try{
// create a FileReader oject
FileReader fr = new FileReader("input.txt");
Scanner sc = new Scanner(fr); // scanner to read file
//declare variables
int accountNumber; //Account Number Place holder
String fname; //First name Place holder
String lname; //Last name Place holder
float balance; //Balance Place holder
float withdrawlTotal=0; //Total withdrawals
int withdraws=0; //withdraw
float depositTotal=0; //total deposits
int deposits=0; //Deposits
// get account number
System.out.println("Enter Account Number:");
// read input fom file and convert to integer
accountNumber = Integer.parseInt(sc.nextLine());
System.out.println(accountNumber);
// get first name
System.out.println("Enter Customer First Name");
fname = sc.next();
//get last name
System.out.println("Enter Customer Last Name:");
lname = sc.next();
//get original account balance
System.out.println("Enter Original Account balance:");
balance = Float.parseFloat(sc.next());
// 5 withdrawals from account balance from user (or exit with -1)
for(int i=0;i<5;i++)
{
System.out.println("Enter withdrawl amount, or type -1 to exit:");
float temp = Float.parseFloat(sc.next());
if(temp==-1)
{
break;
}
withdraws++;
withdrawlTotal += temp;
}
// get 5 deposits from account balance from user (or exit with -1)
for(int i=0;i<5;i++)
{
System.out.println("Enter deposit amount, or type -1 to exit:");
float temp = Float.parseFloat(sc.next());
if(temp==-1)
{
break;
}
deposits++;
depositTotal += temp;
}
// output results on screen for user to see
System.out.printf("%d - %s %s ",accountNumber,fname,lname); //Displays Account number, first and last name
System.out.printf("Original Balance: $%.2f ",balance); //Displays account balance
System.out.println("Withdrawls:"+withdraws); //Displays withdrawals
System.out.printf("withdrawl Total:$%.2f ",withdrawlTotal); //Displays withdraw totals
System.out.println("Deposits:"+deposits); //Displays deposits
System.out.printf("Deposit Total:$%.2f ",depositTotal); //Displays deposit totals
System.out.printf("Balance:$%.2f ",(balance-withdrawlTotal+depositTotal)); //Displays end balance
System.exit(0); //Program end
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.