简体   繁体   English

将数据文件中的整数存储到数组中 Java

[英]Storing integers inside a data file into an array Java

I am trying to store integers from a data file into an array.我正在尝试将数据文件中的整数存储到数组中。 I am using Java Eclipse IDE.我正在使用 Java Eclipse IDE。

Here is my data file:这是我的数据文件:

(oddsAndEvens.dat) (oddsAndEvens.dat)

2 4 6 8 10 12 14
1 2 3 4 5 6 7 8 9
2 10 20 21 23 24 40 55 60 61

Here is my code:这是我的代码:

import java.io.File;
import java.util.Arrays;
import java.io.IOException;
import java.util.Scanner;

public class OddsAndEvens {
    public static void main(String[] args) throws IOException {
        Scanner file = new Scanner(new File("oddsAndEvens.dat"));
        int count, num = 0;
        int[] newRay = null;
        while (file.hasNext()) {
            String line = file.nextLine();
            Scanner chop = new Scanner(line);
            count = 0;
            while (chop.hasNextInt()) {
                num = chop.nextInt();
                count++;

                newRay = new int[count];
                int j = 0;
                for (int i = 0; i < count; i++) {
                    newRay[j] = num;
                    j++;
                }
            }
            System.out.println(Arrays.toString(newRay));
        }
    }
}

My output is coming out as this:我的 output 是这样出来的:


[14, 14, 14, 14, 14, 14, 14]
[9, 9, 9, 9, 9, 9, 9, 9, 9]
[61, 61, 61, 61, 61, 61, 61, 61, 61, 61]

What I am looking for is this:我正在寻找的是:

[2, 4, 6, 8, 10, 12, 14]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 10, 20, 21, 23, 24, 40, 55, 60, 61]

How would I turn these sets of numbers on each line from my data file into an array?如何将数据文件中每一行的这些数字集转换为数组? Is there an easier way to do it?有更简单的方法吗?

Your problem is that you have one loop reading all the numbers and assigning them to num , and another loop storing num at every index in the array.您的问题是您有一个循环读取所有数字并将它们分配给num ,另一个循环将num存储在数组中的每个索引处。 Each array component can only hold one number at a time, so each time they're assigned, their previous values are overwritten (ie lost), so finally the array holds only the last number that was read from the scanner.每个数组组件一次只能保存一个数字,因此每次分配它们时,它们以前的值都会被覆盖(即丢失),因此最终数组只保存从扫描仪读取的最后一个数字。

To fix this, you should use just one loop, which reads one number and stores it in the array before the next iteration of the loop reads the next number.要解决此问题,您应该只使用一个循环,该循环读取一个数字并将其存储在数组中,然后循环的下一次迭代读取下一个数字。 You should not have a loop which writes the same value at every index in the array.您不应该有一个循环在数组中的每个索引处写入相同的值。

you can read each line and store it in a string array by splitting it with space and then convert it to integer, here's how you can do it.\您可以读取每一行并将其存储在字符串数组中,方法是用空格将其拆分,然后将其转换为 integer,这是您的操作方法。\

import java.io.File;
import java.util.Arrays;
import java.io.IOException;
import java.util.Scanner;

public class OddsAndEvens {
    public static void main(String[] args) throws IOException {
        Scanner file = new Scanner(new File("oddsAndEvens.dat"));
        int[] newRay = null;
        while (file.hasNext()) {
            String line = file.nextLine();
            String[] str = line.split("\\s+");
            newRay = new int[str.length];
            for (int i = 0; i < str.length; i++) {
                newRay[i] = Integer.valueOf(str[i]);
            }
            System.out.println(Arrays.toString(newRay));
        }
    }
}

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

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