Skip to main content
Code Review

Return to Answer

Explained inappropriate recursion and offered alternative
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In getGuess(), recursing on validation failure is inappropriate. (Hold down the Enter key to overflow the stack!) Use a do-while loop instead.:

public static int[] getGuess(){
  Scanner keyboard = new Scanner(System.in);
 do {
 System.out.print("Please enter your guess: ");
 String input = keyboard.nextLine();
 if (input.matches("\\d{4}")) break;
 System.out.println("Invalid number. You must enter 4 digits between 0-9 only.");
 } while (true);
  int[] guess = new int[4];
  for (int i = 0; i < guess.length; i++) {
    guess[i] = Character.digit(input.charAt(i), 10);
  }
  return guess;
}

In numberGenerator(), change your while-loop to a do-while.

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In getGuess(), recursing on validation failure is inappropriate. Use a do-while loop instead.

In numberGenerator(), change your while-loop to a do-while.

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In getGuess(), recursing on validation failure is inappropriate. (Hold down the Enter key to overflow the stack!) Use a loop instead:

public static int[] getGuess(){
  Scanner keyboard = new Scanner(System.in);
 do {
 System.out.print("Please enter your guess: ");
 String input = keyboard.nextLine();
 if (input.matches("\\d{4}")) break;
 System.out.println("Invalid number. You must enter 4 digits between 0-9 only.");
 } while (true);
  int[] guess = new int[4];
  for (int i = 0; i < guess.length; i++) {
    guess[i] = Character.digit(input.charAt(i), 10);
  }
  return guess;
}

In numberGenerator(), change your while-loop to a do-while.

added 100 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In getGuess(), recursing on validation failure is inappropriate. Use a do-while loop instead.

In numberGenerator(), change your while-loop to a do-while.

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In numberGenerator(), change your while-loop to a do-while.

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In getGuess(), recursing on validation failure is inappropriate. Use a do-while loop instead.

In numberGenerator(), change your while-loop to a do-while.

Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

In getGuess(),

Integer.parseInt(String.valueOf(input.charAt(i)))

could be written as Character.digit(input.charAt(i), 10).

In numberGenerator(), change your while-loop to a do-while.

lang-java

AltStyle によって変換されたページ (->オリジナル) /