Print Elements of ArrayList in order of input string

What I am trying to do is, get input string from user. Add it into arraylist since i have to play with characters later. But first i want to print contents of ArrayList in order to check what is exactly in their. Here is my code

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    List <String> Vowels = new ArrayList<String>();
    System.out.println("What is your string? ");
    String abc = sc.nextLine();
    int n = abc.length();
    for(int i=0; i <= n-1 ; i++)
    {
        Vowels.add(sc.nextLine());
    }

    for(int j=0; j< Vowels.size();j++)
        System.out.println(Vowels.get(j));

}

It is not printing anything or not showing up any error. Your help will be highly appreciated.

Answers


here is your problem:

for(int i=0; i <= n-1 ; i++)
{
    Vowels.add(sc.nextLine());
               ^
}

"It is not printing anything" because it waiting for your input

What you need is:

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    List <String> Vowels = new ArrayList<String>();
    System.out.println("What is your string? ");
    String abc = sc.nextLine();
    int n = abc.length();
    for(int i=0; i < n ; i++)
    {
        Vowels.add(new StringBuilder().append("").append(abc.charAt(i)).toString());
    }

    for(int j=0; j< Vowels.size();j++)
        System.out.print(Vowels.get(j));
}

Input : Burger

Output: Burger

If you want your output as [B, u, r, g, e, r], then just do:

System.out.println(Vowels); //without for loop

Based upon your new requirements as in Rafa El's comments, try

abc = "Burger";          // use abc = sc.nextLine();
int n = abc.length();
for(int i=0; i < n ; i++)
{
    Vowels.add("" + abc.charAt(i));
}

Need Your Help

Inconsistent behavior: The ntext and image data types cannot be used in WHERE, HAVING, GROUP BY, ON, or IN clauses

c# sql sql-server linq sql-server-ce

I am using SQL CE version 3.5.1. On my PC everything works fine but on the clients PC it generates the following error. I'm doing model first.

Cakephp localization routes

cakephp localization routes multilingual

My localization files (.po) work if I change the default language, but I can't make the routes working, here's what I've got atm: