Ask Question
16 October, 19:37

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

+3
Answers (1)
  1. 16 October, 23:17
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers