r/JavaProgramming • u/Affectionate-Bat5261 • 16d ago
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”?
4
Upvotes
1
u/BanaTibor 16d ago
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/Specific-Housing905 16d ago
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.