I am really stuck on the assignment of Java. Just a beginner and I need to write a donation management system that needs to read 2 files (donator records & instructions) which I need to read and validate the donator records, then print writes the validated records and print it in certain toString format.
Need to read records like these:(fields may in any order)
phone 02111111
name Posephine Bloggs
birthday 01-06-1980
address 1 Grace Street, Lane Cove, NSW
postcode 2066
recipient dog care, the disabled
donation 100, 300
address 102 Smith St, Summer hill, NSW
postcode 2130
name Posephine Esmerelda Bloggs
birthday 13-05-1960
phone 11222009
recipient animal protection, lifecare
donation 50, 200
I have created 3 Scanner already, 1. Scan the record file 2. Scan through every lines 3. Scan the keywords and values
But then I got the result file with every field created an donator object instead of putting all the fields in an single object.
Output like this:
name: null
birthday: null
address: null
postcode: 0
phone: null
name: Posephine Bloggs
birthday: null
address: null
postcode: 0
phone: null
name: null
birthday: 01-06-1980
address: null
postcode: 0
phone: null
name: null
birthday: null
address: 1 Grace Street, Lane Cove, NSW
postcode: 0
phone: null
name: null
birthday: null
address: null
postcode: 0
phone: null
Your mistake is here:
while(scanRecord.hasNextLine()) {
donator a = new donator();
String line = scanRecord.nextLine();
This means that you will have a new donator created for each line you read, whereas you should create a new donator whenever you read a blank line.
One way to fix this is to move the declaration of a
out of the while loop, and only initialise a
if the line read is blank. You should also add the donator to the list in the else branch instead.
donator a = new donator();
while(scanRecord.hasNextLine()) {
String line = scanRecord.nextLine();
if (!line.isBlank()) {
...
} else {
donatorList.addDonator(a);
a = new donator();
}