I am trying to find the center of a Tetris piece for a T-Spin.
EDIT: Error log:
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)
Finally getCenter()
function:
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));
}
Error message seems pretty self explanatory:
java.lang.NumberFormatException: For input string: "4.0"
at java.lang.Integer.parseInt()
Your string looks like floating point value, not integer.
EDIT
In fact this parsing
centerX = Integer.parseInt(Double.toString(roundedX));
is not needed at all as you can cast double to int directly:
centerX = (int)roundedX;
See type casting