简体   繁体   English

并行数组和从文件读取整数/双精度的问题

[英]Issue with parallel arrays and reading integers / doubles from file

I have an issue with parallel arrays and getting the program to read integers and doubles. 我对并行数组有问题,并且让程序读取整数和双精度数。 For example, I have a text file with these values: 例如,我有一个具有以下值的文本文件:

1234 99.58 1234 99.58

5678 1854.99 5678 1854.99

The first number being an account number and the second number being the balance of the account. 第一个数字是一个帐号,第二个数字是该账户的余额。 I'm not sure how to get them into a parallel array (int[] accountnumber, double[] balance) while going down the list of ideally 10+ accounts and balances. 我不确定如何将它们放入并行数组(int []帐号,double []余额),同时查看理想情况下10多个帐户和余额的列表。

I've tried filling the arrays separately without success, which doesn't feel like the most efficient method possible. 我尝试过分别填充数组,但没有成功,这似乎并不是最有效的方法。 I've tried breaking down the "(int = 0; i < maxAccts; i++)" so I could use the "i" variable for both without resetting it. 我尝试分解“((int = 0; i <maxAccts; i ++)””,这样我就可以将这两个变量都使用“ i”而不重置它。

    public static int readAccts(int[] acctNum, double[] balance, int maxAccts, File myinput, Scanner inputFile) throws IOException {
    maxAccts = 0;
    while(inputFile.hasNextInt()) {
        //Test for reading integers accurately
        //System.out.println(inputFile.nextInt());
        maxAccts++;
        inputFile.nextLine();}

    //Test for maxAccts
    System.out.println(maxAccts);

    acctNum = new int[maxAccts];
    balance = new double[maxAccts];
    Scanner AccountFiller = new Scanner(myinput);
    while(inputFile.hasNext());{            
    int i = 0;
    while (i < maxAccts) {
            acctNum[i] = AccountFiller.nextInt();
            balance[i] = inputFile.nextDouble();
    i++;}
    //for (int i = 0; i < maxAccts; i++)

    System.out.println(acctNum[1]);}
    return maxAccts;
}

I keep getting this error below: 我一直在下面收到此错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)

At this point, I don't know why it's going wrong. 在这一点上,我不知道为什么会出错。 In my head the cursor in the document should be right after the integer (account number), and I'm not getting an issue with that part. 在我的脑海中,文档中的光标应该在整数(帐号)之后,而我对该部分没有任何疑问。

You might want to fix your while loop 您可能需要修复while循环

Your second while loop looks like this: 您的第二个while循环如下所示:

while(inputFile.hasNext());{

Which has a semicolon between the while and the curly brace, which means that the body of the loop is empty and after it is done looping, you run the code inside the curly braces. 其中while和花括号之间用分号表示,这意味着循环的主体为空,并且在完成循环之后,您可以在花括号内运行代码。 The loop should be like this: 循环应如下所示:

while(inputFile.hasNext()) {

This causes the error of reading the nextDouble since we just consumed the scanner's source until there is no next. 这会导致读取nextDouble的错误,因为我们只是消耗了扫描仪的源,直到没有下一个。

You might also want to check again how you want to read the file, since the first part doesnt really make much sense to me, it appears you just skip over all ints found in the file (while counting how many times you skip) 您可能还想再次检查一下如何读取文件,因为第一部分对我而言意义不大,看来您只是跳过了文件中找到的所有整数(同时计算了跳过的次数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM