gpt4 book ai didi

java - 数组为空。未从 while 循环获取信息

转载 作者:行者123 更新时间:2023-12-01 11:06:55 25 4
gpt4 key购买 nike

下面的代码片段应该从文本文件中读取,检查文件中有多少个单词,有多少行,并利用这些信息支持将第 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 的情况下如何让它正常工作?

<小时/>

根据答案,我纠正了逻辑中的缺陷,但最终得到了

运行:错误:6Java 结果: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();

最佳答案

发生这种情况是因为在循环的每次迭代中,您都会增加所有计数变量。

第一次进入循环时,您有以下变量:

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());

关于java - 数组为空。未从 while 循环获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32856875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com