将输入作为字符串的正确方法是什么?我想在用户输入的“i”处打印一个字符 [duplicate]

What is the correct way to take input as a string. I want to print a character at 'i' which is entered by user [duplicate]

提问人:Varun Tripathti 提问时间:10/22/2023 最后编辑:James ZVarun Tripathti 更新时间:10/27/2023 访问量:83

问:

我的代码显示的输出和错误:

Enter string : hii'

8
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 4
    at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
    at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
    at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
    at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
    at java.base/java.lang.String.checkIndex(String.java:4828)
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46)
    at java.base/java.lang.String.charAt(String.java:1555)
    at string_methods.main(string_methods.java:9)

Process finished with exit code 1

建议我更正,并请解释为什么会发生此错误。我的代码:

import java.util.Scanner;

public class string_methods {

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

        System.out.print("Enter string : ");
        String str = sc.next();
        int i = sc.nextInt();
        System.out.println("Char at "+i+" is "+str.charAt(i));
    }
}
爪哇岛

评论

4赞 Jon Skeet 10/22/2023
好吧,您正在尝试从只有 4 个字符 (hii') 的字符串中打印第 8 个字符(从 0 开始,所以实际上是第 9 个字符)。我不清楚你期望的输出是什么。
1赞 Andy Turner 10/22/2023
检查是否在字符串中的字符范围内。如果不是,则循环,并且您希望用户重试。i

答:

1赞 AMZ 10/22/2023 #1

你可以这样做:

Scanner sc = new Scanner(System.in);

    System.out.print("Enter string : ");
    String str = sc.next();
    int i;
    while (true) {
        i = sc.nextInt();
    //you can handle floating point and very big numbers as well 
        if (i >= str.length() && i > -1)
            System.out.println("Enter a valid index : ");
        else break;
    }
    System.out.println("Char at " + i + " is " + str.charAt(i));

评论

2赞 user85421 10/22/2023
顺便说一句,负数也不是有效的指数......
1赞 Basil Bourque 10/22/2023 #2

Answer by AMZ 中的代码对于某些字符串是正确的,但对于大多数字符都失败了。例如,尝试 ."😷".length()

不要使用 ,而是使用代码点整数。char

解决问题的关键是验证用户的输入:用户输入的仓位号是否不太小或太大?

System.out.print("Enter string : ");
String s = sc.next();
System.out.print("Enter the position of desired character : ");
int position ;
while (true) {
    i = sc.nextInt();
    if ( ( position < 1 ) || ( position > s.codePoints().count() ) )
        System.out.println( "Enter a valid position number : " );
    else break;
}
int codePoint = s.codePoints().toArray()[ position - 1 ] ;  // Subtract one for annoying zero-based index number. 
String result = Character.toString( codePoint ) ;
System.out.println("Character at position " + position + " is " + result );

另外,请注意,看似单个字符的内容实际上可能是多个字符的复合体。例如,看似人手的单个表情符号字符实际上可能由多个字符组成,这些字符充当修饰符以指示特定的肤色。文本处理是一个棘手的话题。

评论

0赞 Basil Bourque 10/22/2023
@user85421我的意思是这里的逻辑使用基于常识的 1 序数,而不是基于 0 的索引。现在修好了,谢谢。
0赞 Basil Bourque 10/23/2023
@user85421 是的!谢谢。固定。我需要咖啡☕️。