I am passing an object of class "String" to hello function and i want to pribt all the chars so i am using foreach loop

My main motive is to use foreach so please help me to type cast it

follwoing is the code i have

int i=0 ;
public void hello( String st)
{

for(int i:collection((String) st))
{
System.out.println(st.charAt(i));
i++;
}

The loop you are using is not actually a valid foreach. The foreach construct is used for iterating over a Collection or array.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

Here is one example to print each char in a String:

public void hello( String st)
    {
        for(char c : st.toCharArray())
        {
            System.out.println(c);
        }    
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.