How can I concatenate the next two characters from a user input and print them?

时间:2019-04-08 12:46:56

标签: java concatenation character increment

I need to write a method nextCharacters that asks the user to input a character. The program should respond by printing the next two characters. For example, if the user enters the character e, the program should respond by printing fg to the console. You can assume that the character entered is not a whitespace character.

So far I have managed to get this way around it. Is there a more efficient way to do it? Also why can't I simply increment by doing firstChar++ and firstChar+=2;

    public static void nextCharacters() {

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter a character: ");
    String first = input.next();
    char firstChar = first.charAt(0);
    char secondChar = (char) (firstChar + 1);
    char thirdChar = (char) (firstChar + 2);


    System.out.println("" + secondChar + thirdChar);

}

0 个答案:

没有答案