Reputation: 21
I do have one validation issue and I don't know how to continue. The thing is that I am already checking if a string is empty (I guess) but it does not repeat the action.
I want the user types to be correct but it displays Please try again
and after that it continues to my next statement about the team description. Any ideas?
System.out.println("Enter the name of the team : ");
team1.setTeamName(scanner.nextLine())
System.out.println("Enter the description of the team : ");
public void setTeamName(String teamName) {
if (!isNullOrEmpty(teamName)) {
this.teamName = teamName;
} else {
System.out.println("Team name can't bee empty");
System.out.println("Please try again");
public static boolean isNullOrEmpty(String str) {
if (str != null && !str.trim().isEmpty()) {
return false;
} else {
return true;
}
Upvotes: 2
Views: 90
Reputation: 14958
You could change the method setTeamName(String teamName)
to return a boolean
indicating whether the name was correct or not.
public boolean setTeamName(String teamName) {
if (!isNullOrEmpty(teamName)) {
this.teamName = teamName;
return true;
} else {
System.out.println("Team name can't bee empty");
System.out.println("Please try again");
}
return false;
}
Then check if the name was correct, and if not, repeat the action until it is correct.
System.out.println("Enter the name of the team : ");
boolean valid;
do {
valid = team1.setTeamName(scanner.nextLine())
} while (!valid);
System.out.println("Enter the description of the team : ");
Upvotes: 1