How do I print a letter based on the index of the string?

后端 未结 2 1068
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 01:07

I want to print a letter instead of the index position using the indexOf(); method. The requirement is that: Inputs a second string from the user. Outputs the character after t

2条回答
  •  旧巷少年郎
    2021-01-26 01:25

    String.charAt(index)

    You can access a single character, or a letter, by caling método charAt() from String class

    Example

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String phrase = keyboard.nextLine();
        char firstLetter = phrase.charAt(0);
        System.out.println("First Letter : " + firstLetter);
    }
    

    So, running this code, assuming the input is StackOverFlow, the output will be S

    In your code I think doing the follow will work:

    Your Code

     String letter = keyboard.next();
     first = letter.charAt(0);
    

    That might help!

提交回复
热议问题