2

I wrote a method that asks the user to input a name, but I don't want it equal to the compName and I don't want the user's input to be blank. I have humanName.equals("") but that will only make sure that the name isn't "". It could still be one or more blank spaces. I need there to be atleast one character there. I can't figure out how to go about doing this.

public String getHumanName(String compName){
  System.out.println("Enter a name for the human player: ");
  String humanName = scan.nextLine();
  while(humanName.equals(compName) || humanName.equals("")){
        System.out.println("The human player cannot have the same name as the computer player or be blank. Enter a new name for the human player: ");
        humanName = scan.nextLine();
  }
  return humanName;
}
user1766169
  • 1,932
  • 3
  • 22
  • 44
RJobber
  • 23
  • 4

4 Answers4

3

Use trim() to eliminate extra spaces at the beginning or the end of a string.

while(humanName.equals(compName) || humanName.trim().equals("")){
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    `equals("")` can be replaced by `isEmpty()`. See https://stackoverflow.com/questions/3321526/should-i-use-string-isempty-or-equalsstring. – Justin Albano Apr 09 '18 at 13:17
  • 1
    I would to it this way: `"".equals(input)`, because hardcoded String "" is never NULL. `.equals(null)` doesn´t throw a exception. – LenglBoy Apr 09 '18 at 13:20
  • 1
    @LenglBoy if I wrote `"".equals(humanName.trim())` and `humanName` was null, it would have thrown an NPE anyway. – Federico klez Culloca Apr 09 '18 at 13:21
  • damn - it´s monday. You´re right. But then do first check the input not to be null and then values/empty. `string != null && !string.trim().isEmpty() && !string.equals(computerName)` – LenglBoy Apr 09 '18 at 13:23
  • @LenglBoy by the time `humanName` *may* be null, the scanner would have thrown an exception already and the code would be outta here :) – Federico klez Culloca Apr 09 '18 at 13:25
2

You can use a combination of String#trim and equals.

while (humanName.equals(compName) || humanName == null || "".equals(humanName.trim()))
Magus
  • 14,796
  • 3
  • 36
  • 51
0

You can use the trim() method of strings in Java to remove leading to trailing spaces of a String.

public String getHumanName(String compName){
  System.out.println("Enter a name for the human player: ");
  String humanName = scan.nextLine();

  humanName = humanName.trim();

  while(humanName.equals(compName) || humanName.equals("")){
        System.out.println("The human player cannot have the same name as the computer player or be blank. Enter a new name for the human player: ");
        humanName = scan.nextLine();

        humanName = humanName.trim();
  }
  return humanName;
}
Hussein Fawzy
  • 366
  • 2
  • 16
0

There are different ways to manages it.

  • trim() the input so blanks will be removed and you just need to check against EMPTY
  • Use the hibernate validator with @NotBlank - working for java SE and EE.
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
LenglBoy
  • 1,451
  • 1
  • 10
  • 24