Ask Question

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

+5
Answers (1)
  1. 23 March, 11:28
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers