Как исправить NumberFormatException в Java? - PullRequest
0 голосов
/ 09 апреля 2019

Я пытаюсь найти центр фигуры Тетриса для 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$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 голосов
/ 09 апреля 2019

Сообщение об ошибке кажется довольно понятным:

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

Ваша строка выглядит как значение с плавающей запятой, а не целое число.

РЕДАКТИРОВАТЬ

На самом деле этот синтаксический анализ

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

вообще не нужен, так как вы можете приводить double к int напрямую:

centerX = (int)roundedX;

См. тип кастинга

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...