Dan Smith Dan Smith - 3 months ago 15
Java Question

Use a while loop to display letters entered by a user in vertical order?

I'm trying to create a script that will ask a user to input a word and then display back the letters entered by the user in vertical order. The problem is that I'm required to use a while loop, any ideas??????

import java.util.Scanner;
public class VerticalWords {

public static void main(String[] args) {


System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);


String word = scan.nextLine();

for(char a : word.toCharArray())
{
System.out.println("Letter: " + a);
}
}
}


I have tried that code, and it works but its not a while loop ^

Answer

Try this:

System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);
int a = 0;

String word = scan.nextLine();

while(a < word.length){
    System.out.println(word.charAt(a));
    a++;
}
Comments