Write a program that prompts the user for a name and prints each letter of the n
ID: 3723595 • Letter: W
Question
Write a program that prompts the user for a name and prints each letter of the name on separate lines. For example, inputting "Bob Smith" would produce the following output
Please enter your name: Bob Smith
The methods available from the String class are charAt() and length(). Also, the Scanner class will be needed (import java.util.* and construct a new object using "new"). You will need the nextLine() method for user input. It is ok to put all of the program in the main method for this question.
Explanation / Answer
ReadText.java
import java.util.Scanner;
public class ReadText {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name: ");
String name = scan.nextLine();
for(int i=0;i<name.length();i++) {
System.out.println(name.charAt(i));
}
}
}
Output:
Enter the name:
Bob Smith
B
o
b
S
m
i
t
h
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.