在输入中加倍时,仅在整数变量中存储整数值

发布于 2025-02-03 13:49:10 字数 1949 浏览 2 评论 0原文

因此,基本上,我试图从读取的文件中获取整数值,我想将每个值存储在其自己的arraylist中。

从文件获取读取的文件格式如下

1002    53  1   82  169 120 80  237.6   239.0   177.6   42.5    885.8   7.4
1004    53  1   83.7    169 110 70  160.2   173.4   115.8   44.0    73.5    8.2
1006    48  1   81.1    158 130 70  102.6   173.4   100.4   61.8    73.5    5.2

等...

这是我的代码:

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> integerArrayList = new ArrayList();
        ArrayList<Double> doubleArrayList = new ArrayList();
        String filePath = "records.txt";
        try { // Methodology Store in string, split on spaces, covert to int or double,
            // add to the variables directly from the Arraylist.
            Scanner input = new Scanner(new File(filePath));
            Integer integerVal = 0;
            Double doubleVal = 0.0;


            while (input.hasNextLine() ) {
                integerVal = input.nextInt();
                if (integerVal instanceof Integer) {
                    integerArrayList.add(integerVal);
                }
                doubleVal = input.nextDouble();
                if (doubleVal instanceof Double) {
                    doubleArrayList.add(doubleVal);
                }
            }
            System.out.println(integerArrayList);
            System.out.println(doubleArrayList);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我试图逐步调试,问题是它将每个值存储在两个变量中,如预期,直到达到双值...

最后可执行步骤:

输出异常:

如果您还有其他解决方案/方法,请告诉我,谢谢。

So basically I am trying to get only Integer values from the file being read, I want to store the values each in its own ArrayList.

The data formatting from the file getting read is as following

1002    53  1   82  169 120 80  237.6   239.0   177.6   42.5    885.8   7.4
1004    53  1   83.7    169 110 70  160.2   173.4   115.8   44.0    73.5    8.2
1006    48  1   81.1    158 130 70  102.6   173.4   100.4   61.8    73.5    5.2

etc...

Here is my chuck of code:

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> integerArrayList = new ArrayList();
        ArrayList<Double> doubleArrayList = new ArrayList();
        String filePath = "records.txt";
        try { // Methodology Store in string, split on spaces, covert to int or double,
            // add to the variables directly from the Arraylist.
            Scanner input = new Scanner(new File(filePath));
            Integer integerVal = 0;
            Double doubleVal = 0.0;


            while (input.hasNextLine() ) {
                integerVal = input.nextInt();
                if (integerVal instanceof Integer) {
                    integerArrayList.add(integerVal);
                }
                doubleVal = input.nextDouble();
                if (doubleVal instanceof Double) {
                    doubleArrayList.add(doubleVal);
                }
            }
            System.out.println(integerArrayList);
            System.out.println(doubleArrayList);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I tried to debug step by step, the problem is it stores every value in both variables as expected until it reaches the double values...

Last executable step:
Last executable step

Output Exception:
Error message

If you have any other solution/ approach please let me know, Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素手挽清风 2025-02-10 13:49:10

在食用之前,测试下一个令牌是int(或double)。如果不是跳过令牌。像

while (input.hasNext() ) {
    if (input.hasNextInt()) {
        integerArrayList.add(input.nextInt());
    } else if (input.hasNextDouble()) {
        doubleArrayList.add(input.nextDouble());
    } else {
        System.out.printf("Skipping non-numeric token: %s%n", input.next());
    }
}

Test if the next token is an int (or a double) before you consume it. If it isn't skip the token. Something like,

while (input.hasNext() ) {
    if (input.hasNextInt()) {
        integerArrayList.add(input.nextInt());
    } else if (input.hasNextDouble()) {
        doubleArrayList.add(input.nextDouble());
    } else {
        System.out.printf("Skipping non-numeric token: %s%n", input.next());
    }
}
话少心凉 2025-02-10 13:49:10

您是说您只想提取237而不是237.6?

int val = Math.round(Math.floor(input.nextDouble()));

您也可以使用此功能检查下一个值

 if (scanner.hasNextDouble()) { /*do double conversion*/ } else { /*just read int*/ }

Do you mean you just wanted to extract 237 and not 237.6?

int val = Math.round(Math.floor(input.nextDouble()));

Also you can use this function to check the next value

 if (scanner.hasNextDouble()) { /*do double conversion*/ } else { /*just read int*/ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文