如何修复 Java 中的 NumberFormatException?

How to fix NumberFormatException in Java?

我正在尝试为 T-Spin 找到俄罗斯方块的中心。

编辑:错误日志:

04-08 22:53:13.078 8438-8438/com.example.tetris000 E/MYAPP: exception
                                                            java.lang.NumberFormatException: For input string: "4.0"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.tetris000.MainActivity.getCenter(MainActivity.java:1457)
at com.example.tetris000.MainActivity.checkLine(MainActivity.java:1500)
at com.example.tetris000.MainActivity.lockPiece(MainActivity.java:1704)
at com.example.tetris000.MainActivity.hardDrop(MainActivity.java:1242)
at com.example.tetris000.MainActivity.onDropClick(MainActivity.java:1209)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6891)
at android.widget.TextView.performClick(TextView.java:12651)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
04-08 22:53:15.909 8438-8438/com.example.tetris000 E/MYAPP: exception
java.lang.NumberFormatException: For input string: "4.0"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.tetris000.MainActivity.getCenter(MainActivity.java:1457)
at com.example.tetris000.MainActivity.checkLine(MainActivity.java:1500)
at com.example.tetris000.MainActivity.lockPiece(MainActivity.java:1704)
at com.example.tetris000.MainActivity.handleMessage(MainActivity.java:1732)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

最后getCenter()函数:

public void getCenter(Boolean rotateRight){;
        double placeholder;
        double rounded = (rotation.point1.x + rotation.point2.x + rotation.point3.x + rotation.point4.x);
        double roundedX = rounded / 4;
        rounded = (rotation.point1.y + rotation.point2.y + rotation.point3.y + rotation.point4.y); // Divison error fixed
        double roundedY = rounded / 4; //Problem is that it will sometimes be roundedY = 12.25 and we need to fix it so it rounds down to 12 or up to 13, but increment is only 0.1

        if(rotation.rotateAmount == 0){
            roundedY *= 100;
            placeholder = roundedY;
            roundedY -= placeholder % 100;
            roundedY /= 100;
            roundedX *= 100;
            if(roundedX % 100 != 0) {
                placeholder = roundedX;
                if(rotateRight) {
                    if (roundedX % 100 >= 50) {
                        roundedX += 100 - (placeholder % 100);
                    } else {
                        roundedX -= placeholder % 100;
                    }
                } else {
                    if (roundedX % 100 > 50) {
                        roundedX += 100 - (placeholder % 100);
                    } else {
                        roundedX -= placeholder % 100;
                    }
                }
            }
            roundedX /= 100;
        } else if(rotation.rotateAmount == 1){
            roundedX *= 100;
            placeholder = roundedX;
            roundedX -= placeholder % 100;
            roundedX /= 100;
            roundedY *= 100;
            if(roundedY % 100 != 0) {
                placeholder = roundedY;
                if(rotateRight) {
                    if (roundedY % 100 <= 50) {
                        roundedY -= placeholder % 100;
                    } else {
                        roundedY += 100 - (placeholder % 100);
                    }
                } else {
                    if (roundedY % 100 < 50) {
                        roundedY -= placeholder % 100;
                    } else {
                        roundedY += 100 - (placeholder % 100);
                    }
                }
            }
            roundedY /= 100;
        } else if(rotation.rotateAmount == 2){
            roundedY *= 100;
            placeholder = roundedY;
            if(100 - (placeholder % 100) == 100 || 100 - (placeholder % 100) == 0){} else {
                roundedY += 100 - (placeholder % 100);
            }
            roundedY /= 100;
            roundedX *= 100;
            if(roundedX % 100 != 0) {
                placeholder = roundedX;
                if(rotateRight) {
                    if (roundedX % 100 <= 50) {
                        roundedX -= placeholder % 100;
                    } else {
                        roundedX += 100 - (placeholder % 100);
                    }
                } else {
                    if (roundedX % 100 < 50) {
                        roundedX -= placeholder % 100;
                    } else {
                        roundedX += 100 - (placeholder % 100);
                    }
                }
            }
            roundedX /= 100;
        } else if(rotation.rotateAmount == 3){
            roundedX *= 100;
            placeholder = roundedX;
            if(100 - (placeholder % 100) == 100 || 100 - (placeholder % 100) == 0){} else {
                roundedX += 100 - (placeholder % 100);
            }
            roundedX /= 100;
            roundedY *= 100;
            if(roundedY % 100 != 0) {
                placeholder = roundedY;
                if(rotateRight) {
                    if (roundedY % 100 >= 50) {
                        roundedY += 100 - (placeholder % 100);
                    } else {
                        roundedY -= placeholder % 100;
                    }
                } else {
                    if (roundedY % 100 > 50) {
                        roundedY += 100 - (placeholder % 100);
                    } else {
                        roundedY -= placeholder % 100;
                    }
                }
            }
            roundedY /= 100;
        }
        centerX = Integer.parseInt(Double.toString(roundedX));
        centerY = Integer.parseInt(Double.toString(roundedY));
    }

错误消息似乎很容易解释:

java.lang.NumberFormatException: For input string: "4.0"
       at java.lang.Integer.parseInt()

您的字符串看起来像浮点值,而不是整数。

编辑

其实这个解析

centerX = Integer.parseInt(Double.toString(roundedX));

根本不需要,因为您可以直接将 double 转换为 int:

centerX = (int)roundedX;

type casting