以下代码片段应该从文本文件中读取,检查文件中有多少单词,以及有多少行,并且根据该信息,它支持将第n个单词排序到适当的数组中。
protected static void loadAccountInformationFromFile() throws Exception
{
// These variables control the logic needed to put words in the 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 to get the nth value later
int wordCount = 0;
while(account.hasNext())
{
account.next();
wordCount++;
}
// If the count is zero it means the account list is empty
if (wordCount == 0)
{
System.err.println("error: Account list empty.");
System.exit(1);
}
// Used to test word count feature:
// System.out.println("Number of words: " + wordCount);
int lineCount = wordCount / MAX_VALUES_PER_LINE;
// These arrays will be used to put the account information in the correct place
// Using the lineCount to create the Array of however large it needs to be
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 I don't close and open a new Scanner I get a compiler error
account.close();
Scanner account2 = new Scanner(new File(INPUT_ACCOUNT_FILE)).useDelimiter(",");
do
{
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 to see if it works, but it only returns:
// [null, [Ljava.lang.String;@55f96302, null, null, null]
System.out.println(Arrays.toString(accountNumber));
// This one only returns [Ljava.lang.String;@55f96302
System.out.println(accountNumber[1]);
account2.close();
}
打开文件并正确计算单词没有问题。但是,当将文字放入适当的数组时,它就不起作用,如评论文本中所示。
我的问题是:为什么?如何在不使用BufferedWriter / BufferedReader的情况下使其正常工作?
基于答案,我纠正了逻辑中的一个缺陷,最终只有
运行: 错误:6 Java结果:1 建立成功(总时间:0秒)
以下是修改后的代码:
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 the logic needed to put words in the 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 to get the nth value later
int wordCount = 0;
while(account.hasNext())
{
account.next();
wordCount++;
}
// If the count is zero it means the account list is empty
if (wordCount == 0)
{
System.err.println("error: Account list empty.");
System.exit(1);
}
// Used to test word count feature:
// System.out.println("Number of words: " + wordCount);
int lineCount = wordCount / MAX_VALUES_PER_LINE;
// These arrays will be used to put the account information in the correct place
// Using the lineCount to create the Array of however large it needs to be
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 I don't close and open a new Scanner I get a compiler error
account.close();
Scanner account2 = new Scanner(new File(INPUT_ACCOUNT_FILE)).useDelimiter(",");
do
{
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 to see if it works, but it only returns:
// [null, [Ljava.lang.String;@55f96302, null, null, null]
System.out.println(Arrays.toString(accountNumber));
// This one only returns [Ljava.lang.String;@55f96302
System.out.println(accountNumber[1]);
account2.close();
答案 0 :(得分:1)
这是因为在循环的每次迭代中,都会增加所有计数变量。
第一次进入循环时,您有以下变量:
accountNumberCount == 1
firstNameCount == 2
lastNameCount == 3
balanceCount == 4
lastVariableCount == 5
sortCount == 1
所以sortCount == accountNumberCount
这意味着你将单词放入accountNumber
数组到第二个位置(索引从数组上的0开始)。
然后你增加所有上述变量并进入第二轮,具有以下情况:
accountNumberCount == 6
firstNameCount == 7
lastNameCount == 8
balanceCount == 9
lastVariableCount == 10
sortCount == 2
因此,在第二次迭代sortCount
不再等于任何其他变量。在进一步的迭代中也会发生同样的事情,最终只有一个单词插入到五个数组中的一个中。
如果你想让这个工作,我建议使用ArrayLists
(所以你不必担心数组索引或数组大小)而不是数组,并且每次迭代只改变一个变量。
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...
do {
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());
答案 1 :(得分:0)
我个人用BufferedReader逐行阅读,这样你就不需要计算行数了,
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}