简体   繁体   English

Java-计数器的无限While循环

[英]Java - Infinite While Loop for counter

I am working in Java using Twitter4J to get a list of Twitter Followers. 我正在使用Twitter4J使用Java来获取Twitter关注者列表。 I am able to obtain and place the ID's into an array, but I only want to get 20 of them, and my while loop seems to go on infinitely to negative numbers. 我可以获取ID并将其放置到数组中,但是我只想获取20个ID,而我的while循环似乎无限地继续为负数。

I've tried debugging, printlns, and code refactoring. 我已经尝试了调试,printlns和代码重构。 I've also tried searches on stackoverflow but don't understand where I am going wrong? 我也尝试过在stackoverflow上搜索,但是不明白我要去哪里错了?

public class Followers {

    public static void main(String[] a) throws InterruptedException 
    {
        long[] tempids = null;
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
        .setOAuthConsumerKey(key)
        .setOAuthConsumerSecret(key)
        .setOAuthAccessToken(key)
        .setOAuthAccessTokenSecret(key);

        List ids = new ArrayList(); 

        long cursor = -1L; 
        Twitter twitter1 = new TwitterFactory(cb.build()).getInstance();
        int count = 20;
        while(count>0)
        {
            try
            {               
                IDs temp    = twitter1.friendsFollowers().getFollowersIDs("cnn", cursor);
                System.out.println("TEMP: " + temp);
                cursor      = temp.getNextCursor();
                tempids = temp.getIDs();
                System.out.println("Long[] TempIDS: " + tempids.toString());
                System.out.println("count INSIDE: " + count); 
            } 

            catch (twitter4j.TwitterException e) 
            {
                System.out.println("twitter: failed");
                e.printStackTrace();
                //return null;
            }

            if(tempids != null) 
            {               
                    for (long id : tempids) 
                    {
                        ids.add(id);
                        System.out.println("Inside FOR LOOP adding IDS to list ");
                        System.out.println("followerID: " + ids.toString());
                        count--;
                        System.out.println("count AFTER DECREMENT: " + count); 
                        Thread.sleep(1000);                     
                    }
                }                  
        }        
    }
}

There could be multiple reasons for this. 可能有多种原因。 Assume there is a problem and your code ends up in catch (twitter4j.TwitterException e) exception block repeatedly then it would be an infinite loop. 假设有问题,并且您的代码反复出现在catch (twitter4j.TwitterException e)异常块中,那么它将是一个无限循环。

If tempids is null or empty then count will never be decremented 如果tempids为null或为空,则计数将永远不会减少

There seems to be another logical error too. 似乎也有另一个逻辑错误。 Your are trying to get followers for 'cnn' 20 number of times(repeatedly with same data). 您正在尝试获得20次“ cnn”关注者(重复使用相同的数据)。 This check for 20(while loop) should be moved to for (long id : tempids) . 检查20(while循环)应该移到for (long id : tempids) May be add a condition in the for loop to break once 20 results are extracted. 可以在for循环中添加一个条件,以在提取20个结果后中断。 I am not very aware of the Twitter lib so I could be wrong here! 我不是很了解Twitter lib,所以我在这里可能是错的! May be post some of the logs which are repeating and readers will get better insight into the problem. 可能会发布一些重复的日志,读者可以更好地了解问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM