A.Bohlund
A.Bohlund

Reputation: 195

Trying to repeat until condition is false

I am trying to make a loop that repeats until the user input correct information (no whitespaces or blank). This is what I have so far but it only repeats one time. I want it to repeat until user fills in a correct name, e.g. Oskar/OSKAR/oskar

    System.out.print("Name: ");
    String name = scanner.nextLine();
    name = name == null ? "" : name.trim();
    while(name.isEmpty()) {
        System.out.println("Wrong data");
        System.out.print("Name: ");
        String name1 = scanner.nextLine();
        name = name1;
    }

Upvotes: 2

Views: 8150

Answers (6)

Mathews Mathai
Mathews Mathai

Reputation: 1707

You could start accepting inside the loop instead of accepting it outside:

String name="";
while(true )
{
     System.out.println("Enter the name");
     Scanner s=new Scanner(System.in);
     name=s.nextLine().trim();
     if(name.isEmpty())
     {
           System.out.println("Invalid.enter again");

      }
      else
      break;
}

Upvotes: 0

WalterM
WalterM

Reputation: 2706

You need to use a do/while loop so you don't write the same code twice:

Scanner scanner = new Scanner(System.in);

String name = null;
do { //use do while loop for this
    if (name != null)
        System.out.println("Invalid Name \"" + name + "\" Try again");

    System.out.print("Enter Your Name: ");
    name = scanner.nextLine();

    //keep looping until name is only letters
} while (!name.matches("[A-Za-z]+"));

System.out.println("Welcome " + name);

Output:

Enter Your Name:
Invalid Name "" Try again
Enter Your Name: a1
Invalid Name "a1" Try again
Enter Your Name: 1
Invalid Name "1" Try again
Enter Your Name: Oskar
Welcome Oskar

Upvotes: 6

wanglijie
wanglijie

Reputation: 1

Maybe the second line, name1 is not empty anymore.

PS:You should also eliminate the whitespaces or blank. the loop body is should like below:

System.out.println("Wrong data");
System.out.print("Name: ");
String name1 = scanner.nextLine();
name1 = name1 == null ? "" : name1.trim();
name = name1;

Upvotes: 0

Aaditya Gavandalkar
Aaditya Gavandalkar

Reputation: 824

As others stated, your code will exit the while loop once there is some non-empty string input. To do more validation (where there are multiple desired inputs), please follow below approach:

    Set<String> desiredSet = new HashSet<String>(Arrays.asList("XYZ", "OSKAR"));
    System.out.print("Name: ");
    String name = scanner.nextLine();
    name = name == null ? "" : name.trim();
    while(name.isEmpty() && !desiredSet.contains(name.toUpperCase())) {
        System.out.println("Wrong data");
        System.out.print("Name: ");
        name = scanner.nextLine().trim();
    }

No need of extra variable name1

Upvotes: 0

Adrian Duta
Adrian Duta

Reputation: 81

Maybe you need something like:

 System.out.print("Name: ");
        String name = scanner.nextLine();
        name = name == null ? "" : name.trim();
        while(!"desiredName".equals(name)) {
            System.out.println("Wrong data");
            System.out.print("Name: ");
            String name1 = scanner.nextLine();
            name = name1;
        }

Upvotes: 0

npinti
npinti

Reputation: 52185

When you do this: name = name1;, you are throwing anything which the user gave you into name, thus while(name.isEmpty()) will no longer be true.

To fix this, you would need to do some extra validation on the name1 variable, for instance:

if("Oskar".equals(name1))    //Instead of name = name1
    name = name1;

The code above would overwrite the name variable if and only if the user provides Oskar as name.

Upvotes: 2

Related Questions