here is my code and results
what i want is to calculate total balance for each account number, how do i select each account number in distinguish and it's balance to do operations (subtract and add)?
result as follows: Account number , D for debit, C for credit, Balance
For each line that you read in, you could split the string on the comma symbol, eg
String[] transactionLineElements = transactionLine.split(",");
this will give you an array of strings, where the 3rd element (at index 2) is that transaction value/balance - ie transactionLineElements[2]. You can then interpret that transaction value string as a number, eg
BigDecimal balance = new BigDecimal(transactionLineElements[2]);
Similarly, you can parse the account number, eg:
Long accountNumber = Long.valueOf(transactionLineElements[0]);
You have to split the values by a comma, using String.split(String regex)
. For example:
String[] values = transactionLine.split(","); // it can be a regex too
// You should check values.length for if there are less/more values than needed
Then use Long.parseLong(String s)
to parse the account number into a long
. You might want to use BigInteger.valueOf(String s)
instead if your number is very big
long accountNumber = Long.parseLong(values[0]);
// Or use this instead:
BigInteger accountNumber = BigInteger.valueOf(values[0]);
To check if it's credit or debit, remember you must use String.equals(String s)
for comparing strings contents, never ==
:
if (values[1].equals("D")) {
// debit
}
else if (values[1].equals("C") {
// credit
}
else {
// wrong input; you should tell the user here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.