简体   繁体   English

我只能输入字母而不是数字时遇到麻烦

[英]I'm having trouble only inputting letters, not numbers

For my project I need to input a first and last name with no numbers but I simply can't find anything online. 对于我的项目,我需要输入没有数字的名字和姓氏,但是我根本无法在线找到任何东西。 If you could help, that would be terrific. 如果您能提供帮助,那将是非常棒的。

Also if you have the time, I need to flip the first and last name with a comma when the user inputs them. 另外,如果您有时间,我需要在用户输入名字和姓氏时用逗号将其翻转。

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;

    public class PhoneListr {

public static void main (String[] args) throws IOException {
    String firstName;
    String lastName;
    System.out.println ("Please enter your first name:");
    firstName = PhoneList ("");
    System.out.println ("Please enter your last name:");
    lastName = PhoneList ("");
    System.out.println ("Your full name is: " + lastName + "," + firstName);
}

public static String PhoneList (String input) throws IOException {
    boolean continueInput = false;

    while (continueInput == false) {

            BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (System.in));
            input = bufferedReader.readLine();
            continueInput = true;

            if(input.matches("[a-zA-Z]+")) {
                continueInput = true;
                    }
                  else {
                 System.out.println ("Error, you may only use the alphabet");
                 continueInput = false;
        }
    }
    return input;
}

} }

use String.matches(regex) : 使用String.matches(regex)

    if(input.matches("[a-zA-Z]+")) {
     System.out.println("your input contains no numerics");
        }
      else {
     System.out.println("only alphabets allowed");
}

the above regex checks a through z, or A through Z, inclusive (range). 上面的正则表达式检查a到z或A到Z(含)(范围)。

For this type of string matching you need to use Regular Expressions, or "RegEx"es. 对于这种类型的字符串匹配,您需要使用正则表达式或“ RegEx”。 It is a very big topic to cover but here's an introduction . 这是一个非常大的话题,但这是一个介绍 RegExes are tools used to test whether strings match certain criteria, and/or to pull certain patterns out of that string or replace certain parts that match those patterns with something else. RegExes是用于测试字符串是否符合特定条件和/或从该字符串中拉出某些模式或将与这些模式匹配的某些部分替换为其他内容的工具。

Here is an example using a RegEx to test whether your input contains a digit: 这是一个使用RegEx来测试您的输入是否包含数字的示例:

if(input.matches("\\d")) {
 // matched a digit, do something
}

Based on OP problem and clarification here are few suggestions. 基于OP问题,这里有一些建议。

Chaitanya solution handles the check for only alphabets perfectly. Chaitanya解决方案可以完美地处理仅字母检查

About the neglected areas of problem : 关于被忽视的问题领域:

i would advice you to make two variable firstName and lastName inside the main() 我建议您在main()创建两个变量firstNamelastName

String firstName;
String lastName;

Change retun type of method phoneList() to String 将方法phoneList()重新调整类型更改为String

return the entered name input insted of number inside the method phoneList ( dont actually see why you are returning number) and store it inside the firstName and lastName 返回在方法phoneList inputnumber输入名称(实际上看不到为什么返回数字),并将其存储在firstNamelastName

System.out.println ("Please enter your first name:");
firstName = PhoneList (0);
System.out.println ("Please input your last name:");
lastNamr =PhoneList (0);

now to print it in the "comma format" use 现在以“逗号格式”使用打印

  System.out.println("full name is: " +lastName+ "," +firstName);

As i read your program again , its a mess!! 当我再次阅读您的程序时,一团糟!

About the method phoneList() 关于方法phoneList()

  1. Use regex condition to set continueInput to true/flase and not exploit execptions. 使用正则表达式条件将continueInput设置为true / flase,而不利用执行。

Ps I would appreciate "editing" to post , if any fellow member find any mistakes above, using a mobile, not sure about formatting etc. Thanks. 附言:如果有任何其他成员发现上述任何错误,请使用移动设备,不确定格式等,然后通过“编辑”发布。 :-) (y) :-)(y)

You can also use 您也可以使用

if(input.matches("[^0-9]"))
{
System.out.println("Don't input numbers!");
continueInput = false;}

Can't understand what 2nd question asks. 无法理解第二个问题的要求。 For answer what I get from your 2nd question is like this. 为了回答我从你的第二个问题中得到的是这样的。 In main function change the code like this 在主函数中更改代码如下

String first_name = null;
String last_name = null;
System.out.println ("Please enter your first name:");
first_name = PhoneList();
System.out.println ("Please input your last name:");
second_name = PhoneList();
System.out.println (second_name+","+first_name);

then in PhoneList function last line should be changed to 然后在PhoneList函数的最后一行应更改为

return input;

Please check for a link ! 请检查链接 for more info 了解更多信息

You can add a check by passing the argument to method StringUtils.isNumeric 您可以通过将参数传递给方法StringUtils.isNumeric来添加检查

This returns true if the String entered is numeric 如果输入的字符串是数字,则返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM