Question: Java help Ask the user for an account balance. Show, in descending order, all the accounts that have a balance greater than what the user
Java help
Ask the user for an account balance. Show, in descending order, all the accounts that have a balance greater than what the user input. Each entry is int, string, long, double, boolean (name length, name, credit card number, balance, cashback).
this is my required output(in this format)
Enter a balance 9000ENTER Accounts with a balance of at least $9000.00 (sorted by balance) Name Account Number Balance Cash Back Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes 7 results
-------
Enter a balance 8000ENTER Accounts with a balance of at least $8000.00 (sorted by balance) Name Account Number Balance Cash Back Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes Anjela Himsworth 3573904891259172 8985.27 Yes Howie Royson 3581572129932389 8965.07 Yes Blinni Mattke 3549214734886202 8960.76 No Dorotea Nolli 6396392530990977 8790.59 Yes Carita Savill 6767642427889745 8738.77 No Mateo Mollene 5100174906912671 8659.35 Yes Cathleen Schurcke 4041598930132416 8596.39 Yes Adriana Bru 3574931681854879 8482.46 Yes Orlando Nutbeem 6372756913380048 8346.07 No Leland Vasilyev 6394213548410265 8249.76 No Ambrosi Fussie 3581429661693202 8207.40 Yes Valentine Montford 3533184590527943 8176.80 Yes Sarette Springell 5100146117467372 8161.69 Yes Rich Yakovl 490337929898976334 8099.58 Yes Conney Sizeland 3588215263928408 8036.12 Yes 22 results
--------
Enter a balance 8500ENTER Accounts with a balance of at least $8500.00 (sorted by balance) Name Account Number Balance Cash Back Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes Anjela Himsworth 3573904891259172 8985.27 Yes Howie Royson 3581572129932389 8965.07 Yes Blinni Mattke 3549214734886202 8960.76 No Dorotea Nolli 6396392530990977 8790.59 Yes Carita Savill 6767642427889745 8738.77 No Mateo Mollene 5100174906912671 8659.35 Yes Cathleen Schurcke 4041598930132416 8596.39 Yes 14 results
------
I have this code, however it is not producing the list of names, account number, balance, and cash back or results which I need.
i.e
Brand Hallam 3573877643495486 9985.21 No Paco Verty 4508271490627227 9890.51 No Stanislaw Dhenin 4405942746261912 9869.27 No Eachelle Balderstone 30526110612015 9866.30 No Reube Worsnop 3551244602153760 9409.97 Yes Tiphanie Oland 5100172198301454 9315.15 No Jordan Rylstone 201715141501700 9135.90 Yes 7 results ...........
can someone fix my coding so that it displays this required list of output, I believe my error is this part of my code
byte[] nameBytes = new byte[nameLength]; inputFile.read(nameBytes); String name = new String(nameBytes, "UTF-8");
I don't believe this part it working, you might need to use a read char method instead to be able to read the list of output that I need
import java.io.*; public class AccountBalance{ public static void main(String[] args) { try { FileInputStream basic = new FileInputStream("accounts-with-names.dat"); DataInputStream inputFile = new DataInputStream(basic); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a balance"); String input = br.readLine(); double minBalance = Double.parseDouble(input); int count = 0; String header = String.format("%20s%20s%10s%10s%n", "Name", "Account Number", "Balance", "Cash Back"); String format = "%20s%20s%10.2f%6s%n"; System.out.printf("Accounts with a balance of at least $%.2f (sorted by balance)%n", minBalance); System.out.print(header); while (inputFile.available() > 0) { int nameLength; try { nameLength = inputFile.readInt(); } catch (EOFException ex) { break; // End of file } byte[] nameBytes = new byte[nameLength]; inputFile.read(nameBytes); String name = new String(nameBytes, "UTF-8"); long accountNum = inputFile.readLong(); double balance = inputFile.readDouble(); boolean cashback = inputFile.readBoolean(); if (balance >= minBalance) { System.out.printf(format, name, Long.toString(accountNum), balance, cashback ? "Yes" : "No"); count++; } } System.out.printf("%d results%n", count); inputFile.close(); } catch (IOException ex) { System.out.println("An error occurred while reading the file: " + ex.getMessage()); } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
