Java 合并来自用户输入的两组数组 - 如何处理空值?
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您调用 parseInt 时,请将调用包装在 try/catch 块中。如果捕获 NumberFormatException,则丢弃该字符串并减少值的数量。
编辑:你是怎么做到的?像这样的事情:
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:
您需要处理那里没有整数的事件。看来您只是盲目地循环输入而不检查其完整性,这就是您收到异常的原因。
正确的方法是建立一个检查机制,确保您正在解析的当前数据有效(在本例中,意味着它是一个整数)。
我的答案非常模糊,因为您明确表示这是一项学校作业,所以您应该接受这个答案并用它来实验并学习实施正确的解决方案。
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.
我发现你可以将 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.
StringUtils.split 应该为您提供大约十种方法来执行此操作。
StringUtils.split should give you about ten ways to do this.