Skip to content
Advertisement

how to get first character of a second word in java

Im currently working through my first java course and trying to write a program that asks the user to type a word then it returns the first letter of that word. then the programs asks the user for a second word and returns the first Charecter of that word. so if the first word is apple it returns “a” and then the if next word is banana it returns “b”.

im using the next char method but once give the user the prompt”write a second word” and call the next char the computer doesn’t wait for the user to type a line it just takes the second character from the first word and prints it. really appreciate any help this one has got me totally baffled.

JavaScript

Advertisement

Answer

I think it’s better to read the whole string first, and then get the first character of it.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement