Как мне убедиться, что строковое значение не передается в переменную int?
Я все еще очень плохо знаком с программированием. Я взял только три класса программирования, один на C++ и два на Java.
Я решил, что хочу создать свою собственную версию линкора. Я более или менее закончил весь проект. Моя проблема заключалась в предоставлении средств проверки пользователя, чтобы убедиться, что пользователь действительно вводит достоверную информацию.
У меня есть огромный кусок кода внутри вложенных циклов do-while, каждый из которых повторяется, если определенное условие возвращает false.
Я создал класс Board, представляющий игровые доски. Это код минус методы.
public class Board
{
// Create class-wide arrays to represent each board
private static int p1[][] = new int[10][10];
private static int p2[][] = new int[10][10];
// Create other class-wide variables
private static int p1Targets[][] = new int[10][10];
private static int p2Targets[][] = new int[10][10];
private static int boatOneLocationP1[][] = new int[2][2];
private static boolean boatOneP1Sunk = false;
private static int boatTwoLocationP1[][] = new int[3][2];
private static boolean boatTwoP1Sunk = false;
private static int boatThreeLocationP1[][] = new int[3][2];
private static boolean boatThreeP1Sunk = false;
private static int boatFourLocationP1[][] = new int[4][2];
private static boolean boatFourP1Sunk = false;
private static int boatFiveLocationP1[][] = new int[5][2];
private static boolean boatFiveP1Sunk = false;
private static int boatOneLocationP2[][] = new int[2][2];
private static boolean boatOneP2Sunk = false;
private static int boatTwoLocationP2[][] = new int[3][2];
private static boolean boatTwoP2Sunk = false;
private static int boatThreeLocationP2[][] = new int[3][2];
private static boolean boatThreeP2Sunk = false;
private static int boatFourLocationP2[][] = new int[4][2];
private static boolean boatFourP2Sunk = false;
private static int boatFiveLocationP2[][] = new int[5][2];
private static boolean boatFiveP2Sunk = false;
}
Это весь метод placeBoats()
Это мой метод placeBoats(). Класс Player имеет статическую переменную для общего числа созданных объектов Player, и каждый объект имеет свой уникальный номер игрока.
void placeBoats(Player p, Scanner kb)
{
// Player is shown a menu, he must select all 5 options to place his
// boats.
// Once user has selected which boat he is placing, display the ocean
// and ask the user for start and end coordinates
// Use arrays for Menu Choices and for User Choices
int[] menuChoices = new int[5];
int[] userChoices = new int[5];
// Initialize arrays
for (int i = 0; i < userChoices.length; i++)
{
userChoices[i] = 0;
}
for (int i = 0; i < menuChoices.length; i++)
{
/*
* 1. Two-Length Ship 2. Three-Length Ship 1 3. Three-Length Ship 2
* 4. Four-Length Ship 5. Five-Length Ship
*
* Repeat the do-while loop until userChoices[] == 1.
*/
menuChoices[i] = i + 1;
}
// Begin do-while loop
int num = 0;
int boatLength = 0;
int row1 = 0, col1 = 0, row2 = 0, col2 = 0;
boolean notReady = true;
boolean boatPresent = false;
boolean validCoords = false;
String starting, ending;
do
{ // Jump to end
do
{ // Jump to Place Boats
boatPresent = false;
do
{ // Jump to boat validation (making sure no boats already exist
// in area)
validCoords = false;
// Display Menu
System.out.println("Menu");
if (menuChoices[0] == 1)
{
System.out.println("1. Two-Length Ship");
}
if (menuChoices[1] == 2)
{
System.out.println("2. Three-Length Ship 1");
}
if (menuChoices[2] == 3)
{
System.out.println("3. Three-Length Ship 2");
}
if (menuChoices[3] == 4)
{
System.out.println("4. Four-Length Ship");
}
if (menuChoices[4] == 5)
{
System.out.println("5. Five-Length Ship");
}
// Get user input
System.out.print("\nEnter your menu choice: ");
num = kb.nextInt();
kb.nextLine();
// Input Validation- input must be a valid menu choice
while ((num != 1 && num != 2 && num != 3 && num != 4 && num != 5))
{
System.out.print("Enter your menu choice: ");
num = kb.nextInt();
kb.nextLine();
}
// Further Validation- Make sure number has not been picked
// before yet
while (userChoices[num - 1] == 1)
{
System.out.print("Enter your menu choice: ");
num = kb.nextInt();
kb.nextLine();
}
// Set boat length
if (num == 1)
boatLength = 2;
if (num == 2)
boatLength = 3;
if (num == 3)
boatLength = 3;
if (num == 4)
boatLength = 4;
if (num == 5)
boatLength = 5;
// ////////////////////////////////////////////////////////////////////////////////////////
// Display ocean
if (p.getPlayerNumber() == 1)
{
showP1Board();
System.out.println();
}
if (p.getPlayerNumber() == 2)
{
showP2Board();
System.out.println();
}
// Ask the user for the start and end coordinates (as a
// String)
System.out.print("Enter the starting coordinate for a "
+ boatLength + "-length ship: \n");
starting = kb.nextLine();
// Validate - Make sure the given values are valid
while (starting.charAt(0) != 'A'
&& starting.charAt(0) != 'B'
&& starting.charAt(0) != 'C'
&& starting.charAt(0) != 'D'
&& starting.charAt(0) != 'E'
&& starting.charAt(0) != 'F'
&& starting.charAt(0) != 'G'
&& starting.charAt(0) != 'H'
&& starting.charAt(0) != 'I'
&& starting.charAt(0) != 'J'
&& starting.charAt(0) != 'a'
&& starting.charAt(0) != 'b'
&& starting.charAt(0) != 'c'
&& starting.charAt(0) != 'd'
&& starting.charAt(0) != 'e'
&& starting.charAt(0) != 'f'
&& starting.charAt(0) != 'g'
&& starting.charAt(0) != 'h'
&& starting.charAt(0) != 'i'
&& starting.charAt(0) != 'j')
{
System.out.print("Enter the starting coordinate for a "
+ boatLength + "-length ship: ");
starting = kb.nextLine();
}
while (starting.charAt(1) != '1'
&& starting.charAt(1) != '2'
&& starting.charAt(1) != '3'
&& starting.charAt(1) != '4'
&& starting.charAt(1) != '5'
&& starting.charAt(1) != '6'
&& starting.charAt(1) != '7'
&& starting.charAt(1) != '8'
&& starting.charAt(1) != '9')
{
System.out.print("Enter the starting coordinate for a "
+ boatLength + "-length ship: ");
starting = kb.nextLine();
}
System.out.println("[" + starting.charAt(0) + "]["
+ starting.charAt(1) + "]");
System.out.println();
// Set Row and Column 1
switch (starting.charAt(0))
{
case 'a':
case 'A':
row1 = 0;
break;
case 'b':
case 'B':
row1 = 1;
break;
case 'c':
case 'C':
row1 = 2;
break;
case 'd':
case 'D':
row1 = 3;
break;
case 'e':
case 'E':
row1 = 4;
break;
case 'f':
case 'F':
row1 = 5;
break;
case 'g':
case 'G':
row1 = 6;
break;
case 'h':
case 'H':
row1 = 7;
break;
case 'i':
case 'I':
row1 = 8;
break;
case 'j':
case 'J':
row1 = 9;
break;
} // end switch statement
switch (starting.charAt(1))
{
case 49:
case 1:
col1 = 0;
break;
case 50:
case 2:
col1 = 1;
break;
case 51:
case 3:
col1 = 2;
break;
case 52:
case 4:
col1 = 3;
break;
case 53:
case 5:
col1 = 4;
break;
case 54:
case 6:
col1 = 5;
break;
case 55:
case 7:
col1 = 6;
break;
case 56:
case 8:
col1 = 7;
break;
case 57:
case 9:
col1 = 8;
break;
}
System.out.println(starting.length());
if (starting.length() >= 3)
{
if ((starting.charAt(1) == 1 || starting.charAt(1) == 49)
&& (starting.charAt(2) == 0 || starting
.charAt(2) == 48))
{
col1 = 9;
}
}
System.out.println("Row1 = " + row1 + "\tCol1 = " + col1);
// //////////////////////////////////////////////////////////////////////
System.out.print("Enter the ending coordinate for a "
+ boatLength + "-length ship: \n");
ending = kb.nextLine();
// Validate - Make sure the given values are valid
while (ending.charAt(0) != 'A' && ending.charAt(0) != 'B'
&& ending.charAt(0) != 'C'
&& ending.charAt(0) != 'D'
&& ending.charAt(0) != 'E'
&& ending.charAt(0) != 'F'
&& ending.charAt(0) != 'G'
&& ending.charAt(0) != 'H'
&& ending.charAt(0) != 'I'
&& ending.charAt(0) != 'J'
&& ending.charAt(0) != 'a'
&& ending.charAt(0) != 'b'
&& ending.charAt(0) != 'c'
&& ending.charAt(0) != 'd'
&& ending.charAt(0) != 'e'
&& ending.charAt(0) != 'f'
&& ending.charAt(0) != 'g'
&& ending.charAt(0) != 'h'
&& ending.charAt(0) != 'i'
&& ending.charAt(0) != 'j')
{
System.out.print("Enter the ending coordinate for a "
+ boatLength + "-length ship: ");
ending = kb.nextLine();
}
while (ending.charAt(1) != '1' && ending.charAt(1) != '2'
&& ending.charAt(1) != '3'
&& ending.charAt(1) != '4'
&& ending.charAt(1) != '5'
&& ending.charAt(1) != '6'
&& ending.charAt(1) != '7'
&& ending.charAt(1) != '8'
&& ending.charAt(1) != '9')
{
System.out.print("Enter the ending coordinate for a "
+ boatLength + "-length ship: ");
ending = kb.nextLine();
}
System.out.println("[" + ending.charAt(0) + "]["
+ ending.charAt(1) + "]");
System.out.println();
// Set Row and Column 2
switch (ending.charAt(0))
{
case 'a':
case 'A':
row2 = 0;
break;
case 'b':
case 'B':
row2 = 1;
break;
case 'c':
case 'C':
row2 = 2;
break;
case 'd':
case 'D':
row2 = 3;
break;
case 'e':
case 'E':
row2 = 4;
break;
case 'f':
case 'F':
row2 = 5;
break;
case 'g':
case 'G':
row2 = 6;
break;
case 'h':
case 'H':
row2 = 7;
break;
case 'i':
case 'I':
row2 = 8;
break;
case 'j':
case 'J':
row2 = 9;
break;
} // end switch statement
switch (ending.charAt(1))
{
case 49:
case 1:
col2 = 0;
break;
case 50:
case 2:
col2 = 1;
break;
case 51:
case 3:
col2 = 2;
break;
case 52:
case 4:
col2 = 3;
break;
case 53:
case 5:
col2 = 4;
break;
case 54:
case 6:
col2 = 5;
break;
case 55:
case 7:
col2 = 6;
break;
case 56:
case 8:
col2 = 7;
break;
case 57:
case 9:
col2 = 8;
break;
}
if (ending.length() >= 3)
{
if ((ending.charAt(1) == 1 || ending.charAt(1) == 49)
&& (ending.charAt(2) == 0 || ending.charAt(2) == 48))
{
col2 = 9;
}
}
// end set rows and cols
System.out.println("Row2 = " + row2 + "\tCol2 = " + col2);
// Make sure the coordinates are valid start and end
// coordinates for the given length ship.
if (((row1 == row2) && ((col2 == col1 + (boatLength - 1)) || (col2 == col1
- (boatLength - 1))))
|| ((col1 == col2) && ((row2 == row1
+ (boatLength - 1)) || (row2 == row1
- (boatLength - 1)))))
validCoords = true;
if (!validCoords)
System.out.println("Error: Invalid coordinates for a "
+ boatLength + " -length ship.\n");
} while (!validCoords); // end inner do-while
// Make sure there is not a boat already in that area.
// Loop through part of the array and check for 1's and 2's
// (boat present on coordinate)
// If there are, a boat is present and these coordinates are
// therefore invalid.
if (p.getPlayerNumber() == 1)
{
if (row1 == row2)
{
if (col1 + boatLength > 9)
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1; c > col1 - boatLength; c--)
{
if (p1[r][c] == 1 || p1[r][c] == 2)
boatPresent = true;
}
}
} else
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1; c < col1 + boatLength; c++)
{
if (p1[r][c] == 1 || p1[r][c] == 2)
boatPresent = true;
}
}
}
} else
{
if (row1 + boatLength > 9)
{
for (int r = row1; r > row1 - boatLength; r--)
{
for (int c = col1; c < col1 + 1; c++)
{
if (p1[r][c] == 1 || p1[r][c] == 2)
boatPresent = true;
}
}
} else
{
for (int r = row1; r < row1 + boatLength; r++)
{
for (int c = col1; c < col1 + 1; c++)
{
if (p1[r][c] == 1 || p1[r][c] == 2)
boatPresent = true;
}
}
}
}
}
if (p.getPlayerNumber() == 2)
{
if (row1 == row2)
{
if (col1 + boatLength > 9)
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1; c > col1 - boatLength; c--)
{
if (p2[r][c] == 1 || p2[r][c] == 2)
boatPresent = true;
}
}
} else
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1; c < col1 + boatLength; c++)
{
if (p2[r][c] == 1 || p2[r][c] == 2)
boatPresent = true;
}
}
}
} else
{
if (row1 + boatLength > 9)
{
for (int r = row1; r > row1 - boatLength; r--)
{
for (int c = col1; c < col1 + 1; c++)
{
if (p2[r][c] == 1 || p2[r][c] == 2)
boatPresent = true;
}
}
} else
{
for (int r = row1; r < row1 + boatLength; r++)
{
for (int c = col1; c < col1 + 1; c++)
{
if (p2[r][c] == 1 || p2[r][c] == 2)
boatPresent = true;
}
}
}
}
}
if (boatPresent)
System.out
.println("Error: Invalid coordinates, a boat is present in this location.");
} while (boatPresent); // end outer do-while
// Place the boat on the ocean
if (p.getPlayerNumber() == 1)
{
if (row1 == row2)
{
if (col1 + boatLength - 1 > 9
|| (starting.charAt(1) > ending.charAt(1)))
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1, counter = 0; c > col1
- boatLength; c--, counter++)
{
// place boat
p1[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP1[counter][0] = r;
boatOneLocationP1[counter][1] = c;
break;
case 2:
boatTwoLocationP1[counter][0] = r;
boatTwoLocationP1[counter][1] = c;
break;
case 3:
boatThreeLocationP1[counter][0] = r;
boatThreeLocationP1[counter][1] = c;
break;
case 4:
boatFourLocationP1[counter][0] = r;
boatFourLocationP1[counter][1] = c;
break;
case 5:
boatFiveLocationP1[counter][0] = r;
boatFiveLocationP1[counter][1] = c;
break;
}
}
}
} else
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1, counter = 0; c < col1
+ boatLength; c++, counter++)
{
p1[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP1[counter][0] = r;
boatOneLocationP1[counter][1] = c;
break;
case 2:
boatTwoLocationP1[counter][0] = r;
boatTwoLocationP1[counter][1] = c;
break;
case 3:
boatThreeLocationP1[counter][0] = r;
boatThreeLocationP1[counter][1] = c;
break;
case 4:
boatFourLocationP1[counter][0] = r;
boatFourLocationP1[counter][1] = c;
break;
case 5:
boatFiveLocationP1[counter][0] = r;
boatFiveLocationP1[counter][1] = c;
break;
}
}
}
}
} else
// Columns the same
{
// Going backwards
if (row1 + boatLength - 1 > 9
|| (starting.charAt(0) > ending.charAt(0)))
{
for (int r = row1, counter = 0; r > row1 - boatLength; r--, counter++)
{
for (int c = col1; c < col1 + 1; c++)
{
p1[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP1[counter][0] = r;
boatOneLocationP1[counter][1] = c;
break;
case 2:
boatTwoLocationP1[counter][0] = r;
boatTwoLocationP1[counter][1] = c;
break;
case 3:
boatThreeLocationP1[counter][0] = r;
boatThreeLocationP1[counter][1] = c;
break;
case 4:
boatFourLocationP1[counter][0] = r;
boatFourLocationP1[counter][1] = c;
break;
case 5:
boatFiveLocationP1[counter][0] = r;
boatFiveLocationP1[counter][1] = c;
break;
}
}
}
} else
// going forwards
{
for (int r = row1, counter = 0; r < row1 + boatLength; r++, counter++)
{
for (int c = col1; c < col1 + 1; c++)
{
p1[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP1[counter][0] = r;
boatOneLocationP1[counter][1] = c;
break;
case 2:
boatTwoLocationP1[counter][0] = r;
boatTwoLocationP1[counter][1] = c;
break;
case 3:
boatThreeLocationP1[counter][0] = r;
boatThreeLocationP1[counter][1] = c;
break;
case 4:
boatFourLocationP1[counter][0] = r;
boatFourLocationP1[counter][1] = c;
break;
case 5:
boatFiveLocationP1[counter][0] = r;
boatFiveLocationP1[counter][1] = c;
break;
}
}
}
}
}
}
if (p.getPlayerNumber() == 2)
{
if (row1 == row2)
{
if (col1 + boatLength - 1 > 9
|| (starting.charAt(1) > ending.charAt(1)))
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1, counter = 0; c > col1
- boatLength; c--, counter++)
{
p2[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP2[counter][0] = r;
boatOneLocationP2[counter][1] = c;
break;
case 2:
boatTwoLocationP2[counter][0] = r;
boatTwoLocationP2[counter][1] = c;
break;
case 3:
boatThreeLocationP2[counter][0] = r;
boatThreeLocationP2[counter][1] = c;
break;
case 4:
boatFourLocationP2[counter][0] = r;
boatFourLocationP2[counter][1] = c;
break;
case 5:
boatFiveLocationP2[counter][0] = r;
boatFiveLocationP2[counter][1] = c;
break;
}
}
}
} else
{
for (int r = row1; r < row1 + 1; r++)
{
for (int c = col1, counter = 0; c < col1
+ boatLength; c++, counter++)
{
p2[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP2[counter][0] = r;
boatOneLocationP2[counter][1] = c;
break;
case 2:
boatTwoLocationP2[counter][0] = r;
boatTwoLocationP2[counter][1] = c;
break;
case 3:
boatThreeLocationP2[counter][0] = r;
boatThreeLocationP2[counter][1] = c;
break;
case 4:
boatFourLocationP2[counter][0] = r;
boatFourLocationP2[counter][1] = c;
break;
case 5:
boatFiveLocationP2[counter][0] = r;
boatFiveLocationP2[counter][1] = c;
break;
}
}
}
}
} else
// Columns the same
{
if (row1 + boatLength - 1 > 9
|| (starting.charAt(0) > ending.charAt(0)))
{
// Going backwards
for (int r = row1, counter = 0; r > row1 - boatLength; r--, counter++)
{
for (int c = col1; c < col1 + 1; c++)
{
p2[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP2[counter][0] = r;
boatOneLocationP2[counter][1] = c;
break;
case 2:
boatTwoLocationP2[counter][0] = r;
boatTwoLocationP2[counter][1] = c;
break;
case 3:
boatThreeLocationP2[counter][0] = r;
boatThreeLocationP2[counter][1] = c;
break;
case 4:
boatFourLocationP2[counter][0] = r;
boatFourLocationP2[counter][1] = c;
break;
case 5:
boatFiveLocationP2[counter][0] = r;
boatFiveLocationP2[counter][1] = c;
break;
}
}
}
} else
// going forwards
{
for (int r = row1, counter = 0; r < row1 + boatLength; r++, counter++)
{
for (int c = col1; c < col1 + 1; c++)
{
p2[r][c] = 1;
// Record coordinates in the static arrays for
// boat location
switch (num)
{
case 1:
boatOneLocationP2[counter][0] = r;
boatOneLocationP2[counter][1] = c;
break;
case 2:
boatTwoLocationP2[counter][0] = r;
boatTwoLocationP2[counter][1] = c;
break;
case 3:
boatThreeLocationP2[counter][0] = r;
boatThreeLocationP2[counter][1] = c;
break;
case 4:
boatFourLocationP2[counter][0] = r;
boatFourLocationP2[counter][1] = c;
break;
case 5:
boatFiveLocationP2[counter][0] = r;
boatFiveLocationP2[counter][1] = c;
break;
}
}
}
}
}
}
// userChoices[num-1] = 1; update statements
userChoices[num - 1] = 1;
menuChoices[num - 1] = 0;
// Check to see if we are ready to continue out of the loop.
notReady = true;
if (userChoices[0] == 1 && userChoices[1] == 1
&& userChoices[2] == 1 && userChoices[3] == 1
&& userChoices[4] == 1)
notReady = false;
System.out.println();
} while (notReady);
}
Кажется, что все работает нормально, когда я запускаю программу в классе драйвера, если только во время строк....
// Get user input
System.out.print("\nEnter your menu choice: ");
num = kb.nextInt();
.... пользователь вводит строку. В этом случае компилятор создает исключение InputMismatchException.
Я понимаю, почему я получаю эту ошибку, но как я могу предотвратить ввод строки пользователем? Я думал, что два оператора while(), которые я имею после строки пользовательского ввода, позаботятся об этом, но если получить строку, он все равно выдает исключение...
Любые идеи о том, как это исправить? Кстати, это мой первый пост здесь, так что извините, если я разместил слишком много кода. Подумал, что я был бы более конкретным, чем неопределенным.
РЕДАКТИРОВАТЬ: Спасибо, CB, ccjmne и ryvantage, и все остальные, кто ответил. Весь ваш вклад привел меня к моему окончательному решению, которое я покажу ниже. Если есть лучший способ сделать это, пожалуйста, дайте мне знать.
//Prompt user for menu choice
try
{
// Prompt user
System.out.print("Enter your menu choice: ");
// Get input from user
num = kb.nextInt();
} catch (InputMismatchException e)
{
System.out
.println("Error, invalid input. Try again.\n");
kb.next();
}
kb.nextLine();
// Input Validation- input must be a valid menu choice
while ((num != 1 && num != 2 && num != 3 && num != 4 && num != 5))
{
try
{
// Prompt user
System.out.print("Enter your menu choice: ");
// Get input from user
num = kb.nextInt();
} catch (InputMismatchException e)
{
System.out
.println("Error, invalid input. Try again.\n");
}
kb.nextLine();
}
// Further Validation- Make sure number has not been picked
// before yet
while (userChoices[num - 1] == 1)
{
try
{
// Prompt user
System.out.print("Enter your menu choice: ");
// Get input from user
num = kb.nextInt();
} catch (InputMismatchException e)
{
System.out
.println("Error, invalid input. Try again.\n");
}
kb.nextLine();
}
2 ответа
Вам нужно catch
InputMismatchException
и вернитесь назад, чтобы продолжить. По сути, ваш код должен быть:
while (userChoices[num - 1] == 1)
{
try {
System.out.print("Enter your menu choice: ");
num = kb.nextInt();
kb.nextLine();
} catch (InputMismatchException e) {
System.out.println("Error. Invalid input. Try again."); // Or whatever error message you want.
}
}
Чтобы углубить свои навыки программирования, вы должны прочитать больше об обработке исключений в Java.
РЕДАКТИРОВАТЬ: Как правило, вы хотите избежать перехвата исключений в качестве средства управления потоком программ. Лучшая практика состоит в том, чтобы избегать исключений с помощью лучшей логики, но я действительно не очень хорошо разбираюсь с консольным вводом / выводом, поэтому я не знаю, как вы справитесь с этим. Особенно при разборе чисел из String
s, я обычно просто использую try/catch hehe #guilty
Добро пожаловать в Stackru и в мир программирования!
Вы не можете буквально запретить пользователю вводить [нечисловую] строку. Эти пользователи - невежественные люди, которые просто вводят все, что только могут придумать:)
Но есть кое-что, что вы можете сделать. Вы можете попросить их попробовать снова и снова, пока они не поймут это правильно! Кроме того, чтобы избежать броска и ловли Exception
Вы могли бы использовать Scanner#hasNextInt
метод, цель которого очевидна.
Вы можете попробовать сделать следующее:
while (!kb.hasNextInt()) {
kb.next(); // this will consume the rubbish input
}
num = kb.nextInt();
Ура!