2016-03-15 19 views
2

我被告知這段代碼在處理中不起作用,因爲它是java 8語法,在處理中不支持。我需要幫助將其轉換爲處理中的工作。 Processing 2可以處理什麼語法?有沒有辦法將Java 8語法轉換爲Processing 2?java 8語法轉換進行處理2(twitter4j)

public static void main (String args[]) throws TwitterException { 
    Twitter twitter = new TwitterFactory().getInstance(); 
    List<Status> statuses = twitter.getUserTimeline("google"); 
    String hashtag = "#AlphaGo"; 
    System.out.println("The Twitter page contains " 
         + countTweets(hashtag, statuses) 
         + " tweets with the hashtag : " + hashtag); 

} 

public static int countTweets(String hashtag, List<Status> statuses){ 
    return (int) statuses.stream() 
         .filter(x -> x.getText().contains(hashtag)) 
         .count(); 
} 

由於具有與聲明變量的問題林: CURRENT VERSION

ConfigurationBuilder cb = new ConfigurationBuilder(); 
Twitter twitterInstance; 
Query queryForTwitter; 
ArrayList<Status> tweets = new ArrayList<Status>(); 

//ArrayList tweets; 

void setup() { 
    cb.setOAuthConsumerKey("xxx"); 
    cb.setOAuthConsumerSecret("xxx"); 
    cb.setOAuthAccessToken("xxx"); 
    cb.setOAuthAccessTokenSecret("xxx"); 
    cb.setUseSSL(true); 

    size(640,440); 

int countTweets(ArrayList<Status> statuses, String hashtag){ 
    int total = 0; 
    for(Status tweet : statuses){ 
    if(tweet.getText().contains(hashtag)){ 
     total++; 
    } 
    } 
    return total; 
} 

    int numGood = 50; 
    int numBad = 50; 
    for (int i = 0; i < numGood; i++) { 
    tweets.add("#good"); 
    } 
    for (int i = 0; i < numBad; i++) { 
    tweets.add("#bad"); 
    } 

} //setup 

//create a function that counts the tweets 
//that contain a certain hashtag 
int countTweets(String hashtag){ 
    int total = 0; 
    for(String tweet : tweets){ 
    if(tweet.contains(hashtag)){ 
     total++; 
    } 
    } 
    return total; 
} 

void draw(){ 

    //count the good and bad tweets 
    int goodTweets = countTweets("#good"); 
    int badTweets = countTweets("#bad"); 

    //calculate color based on tweet counts 
    float r = badTweets/100.0 * 255; 
    float g = goodTweets/100.0 * 255; 
    float b = 0; 

    background(r, g, b); 

} 
+0

如果處理可以將外部JAR和編譯爲Java 8,爲什麼不導入包含此代碼的JAR? – Makoto

+0

這就是我爲twitter4j庫做的。我對編程還很陌生。你能指出我的JAR方向嗎? @Makoto –

回答

0

this post,我發現你的語法,這樣不帶Java 8語法:

int countTweets(ArrayList<String> statuses, String hashtag){ 
    int total = 0; 
    for(String tweet : statuses){ 
    if(tweet.contains(hashtag)){ 
     total++; 
    } 
    } 
    return total; 
} 

你可能有困難,因爲我的示例使用String值而不是Status值。你必須從Status實例轉換爲String值:

int countTweets(ArrayList<Status> statuses, String hashtag){ 
    int total = 0; 
    for(Status tweet : statuses){ 
    if(tweet.getText().contains(hashtag)){ 
     total++; 
    } 
    } 
    return total; 
} 
+0

過去幾周你是我的救世主,我無法強調這一點。我不相信這是很簡單的將字符串轉換爲狀態。 我在上面添加了你的修改,但仍然得到int錯誤。我認爲這是來自缺乏聲明變量。我需要聲明什麼,並且我只聲明像'ArrayList tweets = new ArrayList ();' 我添加我的當前版本@ kevin-workman –

+0

@ zack.gray.ou沒問題。如果您針對任何後續問題發佈單獨問題,您可能會有更好的運氣,但看起來您正在** setup()函數內聲明一個函數**。這不是有效的語法。更一般地說,現在你已經得到了**兩個** countTweets()函數 - 你真的需要它們嗎?請注意,您在此處獲得的任何代碼都將用作**示例**,因此您無法期望只複製粘貼答案並使其自動工作 - 關鍵是瞭解這些答案正在做什麼,然後執行操作它與你自己的代碼。 –

0

您可以使用一個簡單的循環和計數器

public static int countTweets (String hashtag, List<Status> statuses) { 
    int counter = 0; 
    for (int i = 0 ; i < statuses.size() ; i++) { 
     if (statuses.get(i).getText().contains(hashtag)) counter++; 
    } 
    return counter; 
}