数组为空。无法从while循环中获取信息

埃利安德

以下代码段应从文本文件中读取,检查文件中有多少个单词,以及有多少行,并支持使用该信息将第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();
t0mppa

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从while循环中从输入中获取信息?

来自分类Dev

无法在tcl循环中获取数组信息

来自分类Dev

无法从数组获取信息

来自分类Dev

PHP从JSON数组获取信息

来自分类Dev

AngularJS无法从对象中获取信息

来自分类Dev

无法从生成的QRCode中获取信息

来自分类Dev

无法从本地存储获取信息-JavaScript

来自分类Dev

PHP,如何在一个循环中从两个不同的$ post_id获取信息

来自分类Dev

C 无法在 while 循环中获取数组中的搜索编号来工作和“双重释放或损坏”错误

来自分类Dev

while循环中的数组匹配

来自分类Dev

while循环中的Java数组

来自分类Dev

从while循环中获取变量

来自分类Dev

在循环中获取数组的增量

来自分类Dev

使用while循环检查数组是否为空

来自分类Dev

为什么while循环后数组为空?

来自分类Dev

无法从for循环中获取数据

来自分类Dev

从孩子和孩子的孩子获取信息,如果列为空

来自分类Dev

从带有循环的表中获取信息(python)

来自分类Dev

如何在Wordpress中无循环获取信息?

来自分类Dev

从Stormpath获取信息?

来自分类Dev

从URL获取信息

来自分类Dev

从图像获取信息

来自分类Dev

从sysfs获取信息

来自分类Dev

从URL获取信息

来自分类Dev

从TMDB获取信息

来自分类Dev

从Stormpath获取信息?

来自分类Dev

从NSDictionary获取信息

来自分类Dev

在每个循环中填充数组后,为什么数组为空?

来自分类Dev

无法提取信息