We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › What does the colon mean in this for statement
Page Index Toggle Pages: 1
What does the colon mean in this for statement (Read 1167 times)
What does the colon mean in this for statement
Oct 9th, 2009, 11:57pm
 
In a section of code from an example Program I am trying to run I encounter an error at this FOR statement.
 

for (User user : users) {
       System.out.println(user.getName());
     }

I am unfamiliar of what the colon is being  used for and why it is throwing an error.

The source of this example program is
http://straylink.wordpress.com/2008/06/03/logging-in-with-facebook-java-api/


            The Full code for the sketch is as follows
//---------------------------------------------------------------------------
import com.google.code.facebookapi.*;

public class FacebookClient {    
 public static String API_KEY = "ENTER YOUR API KEY";
 public static String SECRET = "ENTER YOUR SECRET";

 public static void main(String args[]) {

   // Create the client instance

   FacebookRestClient client =       new FacebookRestClient(API_KEY, SECRET);

   client.setIsDesktop(true); // is this a desktop app

   try {

     String token = client.auth_createToken();

     // Build the authentication URL for the user to fill out

     String url = "http://www.facebook.com/login.php?api_key="

               + API_KEY + "&v=1.0"

               + "&auth_token=" + token;

     // Open an external browser to login to your application

     Runtime.getRuntime().exec("open " + url); // OS X only!

     // Wait until the login process is completed

     System.out.println("Use browser to login then press return");

     System.in.read();


     // fetch session key

     String session = client.auth_getSession(token,true );

     // obtain temp secret

     String tempSecret = client.getSessionSecret();

     // new facebook client object

     client = new FacebookJaxbRestClient(API_KEY, tempSecret, sessionKey);


     System.out.println("Session key is " + session);


     // keep track of the logged in user id

     Long userId = client.users_getLoggedInUser();

     System.out.println("Fetching friends for user " + userId);


     // Get friends list

     client.friends_get();

     FriendsGetResponse response = (FriendsGetResponse) client.getResponsePOJO();

     List<Long> friends = response.getUid();


     // Go fetch the information for the user list of user ids

    client.users_getInfo(friends, EnumSet.of(ProfileField.NAME));


    UsersGetInfoResponse userResponse = (UsersGetInfoResponse) client.getRepsonsePOJO();

     // Print out the user information

     List<User> users = userResponse.getUser();

     for (User user : users) {

       System.out.println(user.getName());

     }

   } catch (FacebookException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

   } catch (IOException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

   }

 }

}
Re: What does the colon mean in this for statement
Reply #1 - Oct 10th, 2009, 12:28am
 
that be a for each loop:

http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

try replacing it with the standard syntax:

for (int i=0; i<users.length  [or users.size(), if users is a Vector or Set] ; i++){
 User user = users[i];
 etc();
}

--Ben
Re: What does the colon mean in this for statement
Reply #2 - Oct 10th, 2009, 1:45am
 
Or put such code in a .java tab.
Page Index Toggle Pages: 1