java - Arrays are null. Not getting information from while loop -
the following snippet of code supposed read text file, check how many words in file, , how many lines there are, , information it's supports sort nth word it's appropriate array.
protected static void loadaccountinformationfromfile() throws exception { // these variables control logic needed put words in right array int accountnumbercount = 1; int firstnamecount = 2; int lastnamecount = 3; int balancecount = 4; int lastvariablecount = 5; int sortcount = 1; scanner account = new scanner(new file(input_account_file)).usedelimiter(","); // using word count nth value later int wordcount = 0; while(account.hasnext()) { account.next(); wordcount++; } // if count 0 means account list empty if (wordcount == 0) { system.err.println("error: account list empty."); system.exit(1); } // used test word count feature: // system.out.println("number of words: " + wordcount); int linecount = wordcount / max_values_per_line; // these arrays used put account information in correct place // using linecount create array of large needs string[] accountnumber = new string[linecount - 1]; string[] firstname = new string[linecount - 1]; string[] lastname = new string[linecount - 1]; string[] balance = new string[linecount - 1]; string[] lastvariable = new string[linecount - 1]; // if don't close , open new scanner compiler error account.close(); scanner account2 = new scanner(new file(input_account_file)).usedelimiter(","); { string[] temp1 = account2.next().split(","); string temp2 = "" + temp1; if (sortcount == accountnumbercount) { accountnumber[sortcount] = temp2; } if (sortcount == firstnamecount) { firstname[sortcount] = temp2; } if (sortcount == lastnamecount) { lastname[sortcount] = temp2; } if (sortcount == balancecount) { balance[sortcount] = temp2; } if (sortcount == lastvariablecount) { lastvariable[sortcount] = temp2; } accountnumbercount += 5; firstnamecount += 5; lastnamecount += 5; balancecount += 5; lastvariablecount += 5; sortcount++; { } } while (account2.hasnext()); // testing see if works, returns: // [null, [ljava.lang.string;@55f96302, null, null, null] system.out.println(arrays.tostring(accountnumber)); // 1 returns [ljava.lang.string;@55f96302 system.out.println(accountnumber[1]); account2.close(); }
it has no problem opening file , counting words properly. however, when came time put words appropriate array doesn't work, indicated in commenting out text.
my question is: why? , how can work without using bufferedwriter / bufferedreader?
based on answer, corrected flaw in logic end
run: error: 6 java result: 1 build successful (total time: 0 seconds)
here's modified code:
public class main { private final static int account_number_index = 0; private final static int first_index = 1; private final static int last_index = 2; private final static int balance_index = 3; private final static int free_checks_index = 4; private final static int interest_index = 4; private final static int max_values_per_line = 5; private final static string input_account_file = "accountinfo.txt"; protected static string[] fields; protected static void loadaccountinformationfromfile() throws exception { // these variables control logic needed put words in right array int accountnumbercount = 0; int firstnamecount = 1; int lastnamecount = 2; int balancecount = 3; int lastvariablecount = 4; int sortcount = 1; scanner account = new scanner(new file(input_account_file)).usedelimiter(","); // using word count nth value later int wordcount = 0; while(account.hasnext()) { account.next(); wordcount++; } // if count 0 means account list empty if (wordcount == 0) { system.err.println("error: account list empty."); system.exit(1); } // used test word count feature: // system.out.println("number of words: " + wordcount); int linecount = wordcount / max_values_per_line; // these arrays used put account information in correct place // using linecount create array of large needs string[] accountnumber = new string[linecount]; string[] firstname = new string[linecount]; string[] lastname = new string[linecount]; string[] balance = new string[linecount]; string[] lastvariable = new string[linecount]; // if don't close , open new scanner compiler error account.close(); scanner account2 = new scanner(new file(input_account_file)).usedelimiter(","); { string[] temp1 = account2.next().split(","); string temp2 = "" + temp1; if (sortcount == accountnumbercount) { accountnumber[sortcount] = temp2; accountnumbercount += max_values_per_line; } if (sortcount == firstnamecount) { firstname[sortcount] = temp2; firstnamecount += max_values_per_line; } if (sortcount == lastnamecount) { lastname[sortcount] = temp2; lastnamecount += max_values_per_line; } if (sortcount == balancecount) { balance[sortcount] = temp2; balancecount += max_values_per_line; } if (sortcount == lastvariablecount) { lastvariable[sortcount] = temp2; lastvariablecount += max_values_per_line; } sortcount++; } while (account2.hasnext()); // testing see if works, returns: // [null, [ljava.lang.string;@55f96302, null, null, null] system.out.println(arrays.tostring(accountnumber)); // 1 returns [ljava.lang.string;@55f96302 system.out.println(accountnumber[1]); account2.close();
this happens because on every iteration of loop, increase count variables.
when coming loop first time have following variables:
accountnumbercount == 1 firstnamecount == 2 lastnamecount == 3 balancecount == 4 lastvariablecount == 5 sortcount == 1
so sortcount == accountnumbercount
, means put word accountnumber
array second position (index starts 0 on arrays).
then increase above variables , go round two, having following situation:
accountnumbercount == 6 firstnamecount == 7 lastnamecount == 8 balancecount == 9 lastvariablecount == 10 sortcount == 2
thus on second iteration sortcount
no longer equals of other variables. same thing happens on further iterations , end 1 word inserted 1 of 5 arrays.
if want make work, i'd suggest using arraylists
(so don't have worry array indexes or array sizes) instead of arrays , change 1 variable per each iteration.
private static final int account_number_count = 1; private static final int first_name_count = 2; private static final int last_name_count = 3; private static final int balance_count = 4; private static final int last_variable_count = 5; list<string> accountnumbers = new arraylist<string>(); list<string> firstnames = new arraylist<string>(); list<string> lastnames = new arraylist<string>(); list<string> balances = new arraylist<string>(); list<string> lastvariables = new arraylist<string>(); int sortcount = 1; // other things... { if (sortcount == account_number_count) { accountnumbers.add(temp2); } else if (sortcount == first_name_count) { firstnames.add(temp2); } else if (sortcount == last_name_count) { lastnames.add(temp2); } else if (sortcount == balance_count) { balances.add(temp2); } else if (sortcount == last_variable_count) { lastvariables.add(temp2); } if (sortcount < 5) { sortcount++; } else { sortcount = 1; } } while (account2.hasnext());
Comments
Post a Comment