繁体   English   中英

如何获取用户在 java 中输入的字符串的第一个字符

[英]How can I obtain the first character of a string that is given by a user input in java

我希望用户输入一个字符串,让我们说出他或她的名字。 该名称可以是 Jessica 或 Steve。 我想让程序识别字符串但只识别 output 的前三个字母。 它真的可以是我想要的任意数量的字母 output(在本例中为 3),是的,我试过了

字符();

但是,我不想在程序中硬编码一个字符串,我想要一个用户输入。 所以它给我一个错误。 下面的代码是我所拥有的。

        public static void main(String args[]){

         Scanner Name = new Scanner(System.in);
        
        
        System.out.print("Insert Name here ");
        System.out.print(Name.nextLine());
        System.out.println();
        
        
        for(int i=0; i<=2; i++){

            System.out.println(Name.next(i));
            
        }
        
        }

错误发生在

System.out.println(Name.next(i)); 它强调了.next区域,它给了我一个错误,指出,

“扫描仪类型中的方法 next(String) 不适用于 arguments (int)”

现在我知道我的 output 应该是每次迭代的字符串类型,它应该是一个 int,这样 0 是字符串的第一个索引 1 应该是第二个索引,2 应该是第三个索引,但它是一个 char创建一个字符串,我感到困惑。

 System.out.println("Enter string");
            Scanner name = new Scanner(System.in);
            String str= name.next();
            System.out.println("Enter number of chars to be displayed");
            Scanner chars = new Scanner(System.in);
            int a = chars.nextInt();
            System.out.println(str.substring(0, Math.min(str.length(), a)));

char类型自 Java 2 以来基本上已被破坏,自 Java 5 以来已成为传统。作为 16 位值, char在物理上无法表示大多数字符。

相反,使用代码点 integer 数字来处理单个字符。

调用String#codePoints以获取每个字符的代码点的IntStream

通过在传递所需字符数的同时调用limit来截断 stream。

通过传递对StringBuilder class 上找到的方法的引用,使用生成的文本构建一个新String

int limit = 3 ;  // How many characters to pull from each name. 
String output = 
    "Jessica"
    .codePoints() 
    .limit( limit ) 
    .collect( 
        StringBuilder::new, 
        StringBuilder::appendCodePoint, 
        StringBuilder::append     
    )                                        
    .toString()
;

杰斯

当您从用户那里获取条目时,验证输入以确保它符合您的代码规则以避免启动异常(错误)始终是一个好主意。 如果发现用户的输入无效,则为用户提供输入正确响应的机会,例如:

Scanner userInput = new Scanner(System.in);
    
String name = "";
// Prompt loop....
while (name.isEmpty()) {
    System.out.print("Please enter Name here: --> ");
    /* Get the name entry from User and trim the entry
       of any possible leading or triling whitespaces.  */
    name = userInput.nextLine().trim();
        
    /* Validate Entry...
       If the entry is blank, just one or more whitespaces, 
       or is less than 3 characters in length then inform
       the User of an invalid entry an to try again.     */
    if (name.isEmpty() || name.length() < 3) {
        System.out.println("Invalid Entry (" + name + ")!\n"
                + "Name must be at least 3 characters in length!\n"
                + "Try Again...\n");
        name = "";
    }
}
    
/* If we get to this point then the entry meets our
   validation rules. Now we get the first three 
   characters from the input name and display it.  */
String shortName = name.substring(0, 3);
System.out.println();
System.out.println("Name supplied: --> " + name);
System.out.println("Short Name:    --> " + shortName);

正如您在上面的代码中看到的, String#substring()方法用于获取用户输入的字符串(名称)的前三个字符。

暂无
暂无

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

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