Ask Question
8 October, 11:16

Declare a character variable letterStart. Write a statement to read a letter from the user into letterStart, followed by statements that output that letter and the next letter in the alphabet. End with a newline. Hint: A letter is stored as its ASCII number, so adding 1 yields the next letter. Sample output assuming the user enters 'd': de

+1
Answers (1)
  1. 8 October, 15:04
    0
    public class ANot {

    public static void main (String[] args) {

    System. out. println ("Enter a char");

    Scanner in = new Scanner (System. in);

    char letterStart = in. next (). charAt (0);

    char nextCha = (char) (letterStart+1);

    System. out. print (letterStart);

    System. out. println (nextCha);

    }

    }

    Explanation:

    The main logic here is understanding that characters in Java are stored as ASCII numbers and as such some mathematic operations can be carried out on them. So we add 1 to the value of the entered character to get the character next to it, the opposite is also possible (Subtracting 1 to get the value before it)

    Note also that in reading a character using the scanner class Java does not support the nextChar like it does for other primitive data types so we use the method charAt (). To obtain the character at a particular index.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Declare a character variable letterStart. Write a statement to read a letter from the user into letterStart, followed by statements that ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers