Java 合并来自用户输入的两组数组 - 如何处理空值?

发布于 2024-10-14 18:07:04 字数 979 浏览 5 评论 0原文

我正在为 CS 课作业编写一个程序。

基本上它是一种接受命令行参数的方法。像 CSV 之类的东西,因此要调用我执行的调用,请先合并,然后是 csv。

例如 merge 1,2,3,4 5,6,7,8

这会做两件事。 1) 它将每个列表作为数组参数,然后合并为 1 个大数组,2) 对该数组进行排序。

这里有一个问题,我们需要从命令行处理空值。因此用户可以输入:

merge 1,2,,3,4 5,6

我该如何处理这个问题?

错误输出示例:

Enter commands:
merge 12,,2 43
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:493)
    at java.lang.Integer.parseInt(Integer.java:514)
    at CmdInterpreter.strArrayToIntArray(CmdInterpreter.java:143)
    at CmdInterpreter.getIntArray(CmdInterpreter.java:130)
    at Assign1.processOneCommand(Assign1.java:99)
    at CmdInterpreter.processCommands(CmdInterpreter.java:198)
    at CmdInterpreter.processCommands(CmdInterpreter.java:230)
    at CmdInterpreter.ooMain(CmdInterpreter.java:243)
    at MyAssign1.main(MyAssign1.java:20)

I'm writing a program for CS class assignment.

Basically it's a method that takes to command line arguments. Something like a CSV, so to call the call I do merge followed be the csv's.

eg merge 1,2,3,4 5,6,7,8

This will do two things. 1) it takes each list as an array argument then merges into 1 big array, 2) it sorts that array.

Here's the catch, from the command line we need to deal with null values. So a user could feed in:

merge 1,2,,3,4 5,6

How do I deal with this?

example of the error output:

Enter commands:
merge 12,,2 43
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:493)
    at java.lang.Integer.parseInt(Integer.java:514)
    at CmdInterpreter.strArrayToIntArray(CmdInterpreter.java:143)
    at CmdInterpreter.getIntArray(CmdInterpreter.java:130)
    at Assign1.processOneCommand(Assign1.java:99)
    at CmdInterpreter.processCommands(CmdInterpreter.java:198)
    at CmdInterpreter.processCommands(CmdInterpreter.java:230)
    at CmdInterpreter.ooMain(CmdInterpreter.java:243)
    at MyAssign1.main(MyAssign1.java:20)

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

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

发布评论

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

评论(4

给不了的爱 2024-10-21 18:07:04

当您调用 parseInt 时,请将调用包装在 try/catch 块中。如果捕获 NumberFormatException,则丢弃该字符串并减少值的数量。

编辑:你是怎么做到的?像这样的事情:

int[] extractValues(String arg) {
    String[] sValues = arg.split(",");
    int n = values.length;
    int[] values = new int[n];
    for (int i = 0; i < n; ++i) {
        try {
            values[i] = Integer.parseInt(sValues[i]);
        } catch (NumberFormatException e) {
            --n;
        }
    }
    // reallocate values array if there were problems
    if (n < values.length) {
        int[] tmp = new int[n];
        System.arraycopy(values, 0, tmp, 0, n);
        values = tmp;
    }
    return values;
}

When you call parseInt, wrap the call in a try/catch block. If you catch a NumberFormatException, throw away the string and decrement the number of values.

Edit: How do you do that? Something like this:

int[] extractValues(String arg) {
    String[] sValues = arg.split(",");
    int n = values.length;
    int[] values = new int[n];
    for (int i = 0; i < n; ++i) {
        try {
            values[i] = Integer.parseInt(sValues[i]);
        } catch (NumberFormatException e) {
            --n;
        }
    }
    // reallocate values array if there were problems
    if (n < values.length) {
        int[] tmp = new int[n];
        System.arraycopy(values, 0, tmp, 0, n);
        values = tmp;
    }
    return values;
}
梅窗月明清似水 2024-10-21 18:07:04

您需要处理那里没有整数的事件。看来您只是盲目地循环输入而不检查其完整性,这就是您收到异常的原因。

正确的方法是建立一个检查机制,确保您正在解析的当前数据有效(在本例中,意味着它是一个整数)。

我的答案非常模糊,因为您明确表示这是一项学校作业,所以您应该接受这个答案并用它来实验并学习实施正确的解决方案。

You need to handle the event that there is no integer there. It appears that you are simply blindly looping through the input without checking for it's integrity, which is why you are getting an exception.

The proper way to do this would be to set up a checking mechanism which ensures that the current data you are parsing is valid (in this case, meaning it is an Integer).

I am leaving my answer very vague because you explicitly stated that this is for a school assignment, so you should take this answer and use it to experiment and learn to implement a proper solution.

夜吻♂芭芘 2024-10-21 18:07:04

我发现你可以将 list1 作为 null 或 list2 或两者都输入。但不是这些数组的元素......这意味着我正在尝试一些对于这个作业来说不会有问题的东西。

I found out you can feed in either list1 as null or list2 or both. But not elements of those arrays... Meaning I was trying something that would not be a problem for this assignment.

你是我的挚爱i 2024-10-21 18:07:04

StringUtils.split 应该为您提供大约十种方法来执行此操作。

StringUtils.split should give you about ten ways to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文