0

I'm writing a program that takes a number of tweets from a user timeline using twitter4j and writes them to a CSV file. At the moment my code returns all the tweets to console but only writes the last value to my .CSV file. I know there's an issue somewhere as in the console it displays "done" after each tweet.

    Twitter twitter1 = TwitterFactory.getSingleton();
    List<Status> statuses = twitter1.getUserTimeline();
    System.out.println("Showing home timeline.");
    for (Status status : statuses) {
        System.out.println(status.getUser().getScreenName() + ":" +
                           status.getText());
            PrintWriter pw = new PrintWriter(new File("/Desktop/cwk/4104/tweets.csv"));
            StringBuilder sb = new StringBuilder ();
            
            sb.append("ID");
            sb.append(",");
            sb.append("UserId");
            sb.append(",");
            sb.append("Username");
            sb.append(",");
            sb.append("Name");
            sb.append(",");
            sb.append("Location");
            sb.append(",");
            sb.append("Tweet");
            sb.append("\n");
            
            
            for( int i = 0; i < 20; i++ ) {
            sb.append(1+i);
            sb.append(",");
            sb.append(status.getUser().getId());
            sb.append(",");
            sb.append(status.getUser().getScreenName());
            sb.append(",");
            sb.append(status.getUser().getName());
            sb.append(",");
            sb.append(status.getUser().getLocation());
            sb.append(",");
            sb.append(status.getText());
            sb.append("\n");
            
            }
            
            pw.write(sb.toString());
            pw.close();
            System.out.println("done");


        } 
Alex
  • 41
  • 5
  • pw.write(sb.toString()); should be outside the loop – Iqbal AISSAOUI May 01 '21 at 23:15
  • yeah it was meant to be outside there sorry. Does the same thing regardless – Alex May 01 '21 at 23:19
  • can you explain what you are trying to do with the loop to 20 ? – Iqbal AISSAOUI May 01 '21 at 23:22
  • 2
    The writer must be created before the loop and closed after the loop. Put only the write inside the loop. – zappee May 01 '21 at 23:27
  • Does this answer your question? [Write to text file without overwriting in Java](https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java) – David Florez May 01 '21 at 23:28
  • @IqbalAISSAOUI it should loop through each tweet and write it to a csv file until there are no more tweets. at the moment i've just set it to stop after 20 tweets. – Alex May 01 '21 at 23:37
  • @zappee this works but the issue now is that it does each tweet 20 times so i think theres an issue in the for( int i = 0; i < 20; i++ ). ive no idea though i'm honestly not the best at java just a student :/ – Alex May 01 '21 at 23:39
  • you should remove the 20 loop altogether, I think the statuses are the tweets and you are already looping throught them. keep the content of the loop except for the line with "1+i" – Iqbal AISSAOUI May 01 '21 at 23:41
  • @IqbalAISSAOUI removing the loop gives the issue of each tweet appearing multiple times after each loop around. for example the CSV rows are looking like: headers tweet 1 headers tweet 1 tweet 2 headers tweet 1 tweet 2 tweet 3 ETC – Alex May 01 '21 at 23:49

0 Answers0