0

运行我正在为同级项目开发的绘图程序时出现以下错误:

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 语句传递它们以在屏幕上绘制形状。我猜错误消息非常简单。

4

1 回答 1

2

看起来您需要在分隔符中包含逗号以进行拆分 - 并且可能通过使用将其设为可选逗号?

sut = line.split(",? ");

另一种选择是在解析之前删除逗号:

sut[1] = sut[1].replaceAll(",$", "")
于 2012-10-29T12:50:59.547 回答