r/JavaProgramming • u/Affectionate-Bat5261 • Oct 08 '26
How do I code a program that turns entered letters into a word?
If I were to use a scanner to let a user enter one letter at a time, what do I use to make a program that turns these letters in order into one word?
(For example, a user enters “ h e l l o” separately and the program prints “hello”)
Additionally how can I get it to reverse the word so it prints “olleh”?
3
Upvotes
1
u/BanaTibor Oct 08 '26
Read one char at a time and collect them in a char array. You can instantiate a new string from a char array. To reverse it, well you have an array already.
1
u/W31337 Oct 10 '26
Look up arrays. A string is an array of characters. So basically the letters are added to a buffer in memory.
1
u/Specific-Housing905 Oct 08 '26
Use the String class. The Scanner class can read a string so you need to take the first character.
To reverse the string use the StringBuilder class or a for loop from the end.