•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,911 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 983 | Replies: 1
![]() |
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 ;
publicvoid hello( String st)
{
for(int i:collection((String) st))
{
System.out.println(st.charAt(i));
i++;
}
My main motive is to use foreach so please help me to type cast it
follwoing is the code i have
int i=0 ;
publicvoid 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/tutor...bolts/for.html
Here is one example to print each char in a String:
http://java.sun.com/docs/books/tutor...bolts/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);
}
}![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Problem while Type Casting (C++)
- Type casting and type conversions (C)
- Type casting (C++)
- type casting (C++)
- conversion type (C++)
Other Threads in the Java Forum
- Previous Thread: Need Help, Getting Exceptions
- Next Thread: Thread accessing from two different JVM's



Linear Mode