如何在Java中修复NumberFormatException?

时间:2019-04-09 02:49:22

标签: java android numberformatexception

我正在尝试为T形旋转找到俄罗斯方块的中心。

编辑:错误日志:

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$7.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));
    }

1 个答案:

答案 0 :(得分:-1)

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

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