在forloop下的用户验证输入

时间:2016-12-12 11:33:22

标签: java

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);

    String regex = "[a-zA-Z ]+$";
    String regex1 = "\\d[0-9]|[1-9]";
    String regex2 = "^[a-zA-Z0-9 ]+$";
    String petName;

    StringBuilder output = new StringBuilder();

    do {
        System.out.print("\nHow Many Pet do you have? Give from 1-3:");
        petName = input.nextLine();

        if (petName.isEmpty()) {
            System.out.println("Number field should not be Empty.");
        } else if (!petName.matches(regex1)) {
            System.out.println("Please Enter A Valid Number!");
        }
    } while (!petName.matches(regex1));

    do {
        Integer.parseInt(petName);
        String[] pets = new String[Integer.parseInt(petName)];

        System.out.print("\nList Down All Your Pet Names:\n");

        for (int i = 0; i < pets.length; i++) {
            System.out.print("\nPET" + (i + 1) + ":");
            pets[i] = input.nextLine();

            if (pets[i].isEmpty()) {
                System.out.print("String field should not be Empty.");
            } else if (!pets[i].matches(regex)) {
                System.out.print("Please input a valid String.");
            }
        }
        output.append("\nThese Are The List Of The Pets You Have:");
        for (int i = 0; i < pets.length; i++) {
            output.append("\nPET:").append(i + 1).append(" ").append(pets);
        }
    } while (!petName.matches(regex));

    System.out.println(output);
}

我对上述代码有点问题。

我想要的是如果我输入一个整数然后它会提示我这条消息&#34;请输入一个有效的字符串&#34;或者如果我没有在字段中输入任何内容,那么它会提示我另一条消息&#34;字符串字段不应为空&#34;。但是,即使我在字段中键入字符串值,它仍然会提示消息&#34;请输入有效的字符串&#34;每次按回车键时,循环仍然会一遍又一遍地重复。

1 个答案:

答案 0 :(得分:0)

你的第二个while循环遇到了一些问题。首先,你的循环条件是检查petName,在离开第一个while循环后没有改变。其次,for循环似乎嵌套不正确。由于你想循环获取每个宠物名称的有效输入,你应该将第二个while循环放在for循环中,而不是相反。

使用以下修改后的代码可能更容易看到。另请注意,调用append(pets)会输出toString数组的pets结果,而不是单个宠物名称。为此,您应该使用append(pets[i])

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);

    String regex = "[a-zA-Z ]+$";
    String regex1 = "\\d[0-9]|[1-9]";
    String regex2 = "^[a-zA-Z0-9 ]+$";
    String petName;

    StringBuilder output = new StringBuilder();

    do
    {
        System.out.print("\nHow Many Pet do you have? Give from 1-3:");
        petName = input.nextLine();

        if (petName.isEmpty())
        {
            System.out.println("Number field should not be Empty.");
        }
        else if (!petName.matches(regex1))
        {
            System.out.println("Please Enter A Valid Number!");
        }

    } while (!petName.matches(regex1));

    String[] pets = new String[Integer.parseInt(petName)];

    System.out.print("\nList Down All Your Pet Names:\n");

    for (int i = 0; i < pets.length; i++)
    {
        do
        {
            System.out.print("\nPET" + (i + 1) + ":");
            pets[i] = input.nextLine();

            if (pets[i].isEmpty())
            {
                System.out.print("String field should not be Empty.");
            }
            else if (!pets[i].matches(regex))
            {
                System.out.print("Please input a valid String.");
            }
        } while (!pets[i].matches(regex));
    }

    output.append("\nThese Are The List Of The Pets You Have:");

    for (int i = 0; i < pets.length; i++)
    {
        output.append("\nPET:").append(i + 1).append(" ").append(pets[i]);
    }

    System.out.println(output);
}