简体   繁体   中英

How to return the correct value from an int[] array?

I'm new to Java (and programming in general), so apologies if some of this doesn't make sense and/or if the code is really bad:

I am trying get a unique four digit code from a user input (that is what the Keyboard.readInput line does), and I have put in a couple of conditional statements to ensure that the code that is entered can only be 4 digits and that each digit must be different to the other three.

The if statements work as intended in the sense that they output the error message and prompt them to re-enter (ie the method getPlayerGuess() gets called). For example, if the user enters 123 (ie userGuess.length() != 4 ), it will prompt them to enter again. If they then enter 1234, the method will complete.

However, the problem I have is that when I call this method in another class that I have, the code that is being pulled through is the first code entered (ie 123) and not the four digit one I want (ie 1234) - which is resulting in an array IndexOutOfBoundsException .

Any ideas on how I can ensure that the code that is being returned is the four digit one that passes the conditionals, and not the first one entered?

public int[] getPlayerGuess() {

    System.out.print("Guess a four digit code: ");
    String userGuess = Keyboard.readInput();

    if (userGuess.length() != 4) {
        System.out.print("Your code must be 4 digits - ");
        getPlayerGuess();
    }

    int[] userCode = createArrayFromGuess(userGuess);

    for (int i = 0; i < userCode.length-1; i++){
        for (int j = i+1; j < userCode.length; j++){
            if (userCode[i] == userCode[j]) {
                System.out.print("Code must have four unique digits - ");
                getPlayerGuess();
            }
        }
    }
    return userCode;
}

Your problem lies here:

if (userGuess.length() != 4) {
      System.out.print("Your code must be 4 digits - ");
      getPlayerGuess();
    }

Yes, you call the method a second time, but you completely ignore that, and don't do anything with the result of it.

Change it to this:

if (userGuess.length() != 4) {
      System.out.print("Your code must be 4 digits - ");
      return getPlayerGuess();
    }

So you'll return the result of the new call, instead of completing the code after the if block.

EDIT: A better approach would be:

System.out.print("Guess a four digit code: ");
String userGuess = Keyboard.readInput();

  while(userGuess.length() != 4) {
      System.out.print("Your code must be 4 digits - ");
      System.out.print("Guess a four digit code: ");
      userGuess = Keyboard.readInput();
    }

You call getPlayerGuess() which returns an int[] but you do not collect, nor assign the return value. Nor do you return it... so something like the following may work for you:

public int[] getPlayerGuess() {

  System.out.print("Guess a four digit code: ");
  String userGuess = Keyboard.readInput();

  if (userGuess.length() != 4) {
    System.out.print("Your code must be 4 digits - ");
    return getPlayerGuess(); // either assign or return the value that is actually calculated within that call...
  }

  int[] userCode = createArrayFromGuess(userGuess);

  for (int i = 0; i < userCode.length-1; i++){
      for (int j = i+1; j < userCode.length; j++){
          if (userCode[i] == userCode[j]) {
              System.out.print("Code must have four unique digits - ");
              return getPlayerGuess(); // either assign or return the value that is actually calculated within that call...
          }
      }
  }
  return userCode;
}

Simplifying your code a bit (not too much ;-)):

public int[] getPlayerGuess(String msg) {
    System.out.print(msg);
    String userGuess = Keyboard.readLine();
    if (userGuess.length() != 4) {
        return getPlayerGuess("\nYour code must be 4 digits - ");
    }
    if (userGuess.chars().distinct().count() != 4) {
        return getPlayerGuess("\nCode must have four unique digits - ");
    }
    return createArrayFromGuess(userGuess);
}

with the initial call being:

getPlayerGuess("Guess a four digit code: ");

Note that you can write this code also using an iterative approach. But I will leave that up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM