S
S
Sydora2015-05-26 14:44:36
Exceptions
Sydora, 2015-05-26 14:44:36

Why does the program go into an infinite loop when catching an exception?

When catching an InputMismatchException exception (when checking for input letters), the program goes into an infinite loop. When checking for out-of-bounds array, the program works correctly. Why is this happening? Thanks in advance.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String matrix[][] = new String [3][3];
int i = 0, j = 0;
boolean win = false;
char playerTurn = 'X';
int count = 0;
Scanner input = new Scanner(System.in);
for (i = 0; i < matrix.length; i++)
{
for (j = 0; j < matrix.length; j++)
{
matrix[i][j] = "-";
}
}
&& matrix[2][2] == "X" || matrix[0][0] == "X" && matrix[1][1] == "X" && matrix[2][2] == "X" || matrix[0][2] == "X" && matrix[1][1] == "X" && matrix[2][0] == "X"){ win = true;
for (i = 0; i < matrix.length; i++){
for (j = 0; j < matrix.length; j++){ || matrix[1][0] == "0" && matrix[1][1] == "0" && matrix[1][2] == "0" || matrix[2][0] = = "0" &&
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
System.out.println();
}
System.out.println();
System.out.println("\t\tFirst player win!");
break;
}
/**
Checking if the second player wins
*/
if (matrix[0][0] == "0" && matrix[0][1] == "0" && matrix[0][2] == "0"
|| matrix[0][0] == "0" && matrix[1][0] == "0" && matrix[2][0] == "0"
|| matrix[0][1] == "0" && matrix[1][1] == "0" && matrix[2][1] == "0"
|| matrix[0][2] == "0" && matrix[1][2] == "0" && matrix[2][2] == "0"
|| matrix[0][0] == "0" && matrix[1][1] == "0" && matrix[2][2] == "0" System.out.println(); System.out.println("\t\tSecond player win!");
|| matrix[0][2] == "0" && matrix[1][1] == "0" && matrix[2][0] == "0"){
win = true;
for (i = 0; i < matrix.length; i++){
for (j = 0; j < matrix.length; j++){
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
System.out.println();
}
break;
}
/**
Check for a draw
*/
if(count == 9){
win = true;
for (i = 0; i < matrix.length; i++){
for (j = 0; j < matrix.length; j++){
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
System.out.println();
}
System.out.println();
System.out.println("\t\tDraw!");
break;
}
for (i = 0; i < matrix.length; i++){
for (j = 0; j < matrix.length; j++){
System.out.print(matrix[i][j] + "\t") ;
}
System.out.println();
System.out.println();
}
System.out.println();
try{
System.out.print("Horisontal (from 0 to 2): ");
int horizontal = input.nextInt();
System.out.print("Vertical (from 0 to 2): ");
int vertical = input.nextInt();
if (String.valueOf(horisontal) == "Exit" || String.valueOf(vertical) == "Exit") {
break;
}
if (matrix[horisontal][vertical] == "-"){
if (playerTurn == 'X'){
playerTurn = '0';
matrix[horisontal][vertical] = "X";
count++;
}
else{
playerTurn = 'X';
matrix[horisontal][vertical] = "
}
else
System.out.println("\t\tTry some empty cell");
}catch(InputMismatchException e){
System.out.println("\t\tEntered invalid data");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("\t\tEntered invalid value");
}
}
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tplus, 2015-05-26
@Sydora

It is difficult to read the whole sheet, but most likely, after catching the exception , it
continues to work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question