User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2006
Posts: 54
Reputation: staneja is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
staneja's Avatar
staneja staneja is offline Offline
Junior Poster in Training

Help Help me out with Type casting

  #1  
Aug 29th, 2007
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++;
}

AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,826
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 282
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Maven

Re: Help me out with Type casting

  #2  
Aug 29th, 2007
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:
    public void hello( String st)
    {
        for(char c : st.toCharArray())
        {
            System.out.println(c);
        }    
    }
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:09 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC