Trying to repeat until condition is false

懵懂的女人 提交于 2020-07-09 05:32:09

问题


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;
    }

回答1:


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




回答2:


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.




回答3:


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;
        }



回答4:


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




回答5:


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;



回答6:


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;
}


来源:https://stackoverflow.com/questions/34494337/trying-to-repeat-until-condition-is-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!