运行绘图程序时出现以下错误我正在为同类项目工作:
Exception in thread "main" java.lang.NumberFormatException: For input string: "100,"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at InputExample.main(InputExample.java:35)
我知道错误意味着什么,但是因为我刚刚开始学习Java,所以我不是100%如何解决它。到目前为止,这是我的代码:
import java.util.Scanner;
public class Input {
public final static void main(String[] args) {
GraphicsScreen g = new GraphicsScreen();
Scanner s = new Scanner(System.in);
int param1 = -1;
int param2 = -1;
String line;
String command;
String[] sut;
System.out
.println("Please enter your commands here. A list of commands is below to help you get started.");
do {
System.out.println("Circle, Move, Draw");
line = s.nextLine();
} while(line.equalsIgnoreCase("help") == true);
sut = line.split(" ");
command = sut[0];
if(sut.length > 1) {
param1 = Integer.parseInt(sut[1]);
if(sut.length > 2) {
param2 = Integer.parseInt(sut[2]);
}
}
if(command.equals("Move") == true) {
g.move(param1, param2);
}
else if(command.equals("Draw") == true) {
g.draw(param1, param2);
}
else if(command.equals("Circle") == true) {
g.circle(param1);
}
else {
System.out
.println("The commands you have entered are invalid. Please try again.");
}
}
}
所以我将整数转换为数值并通过IF语句传递它们以在屏幕上绘制形状。我猜测错误信息非常简单。
答案 0 :(得分:2)
看起来您需要在分隔符的逗号分隔符中包含逗号 - 并且可以使用?
将其设为可选逗号:
sut = line.split(",? ");
另一种方法是在解析之前删除逗号:
sut[1] = sut[1].replaceAll(",$", "")