2

I have acquired values from a table from a website for a presidential poll app in android that I'm creating. I store four of the columns in four different arraylists. When I try to get the values using arraylist.get(), and store these into an array, arraylist.get() returns null. I displayed my ArrayList on a ListView, and all the values showed up, so I know the values were stored properly. if you look at the website i'm using, it has all the polling data from when polling started. The array will contain 8 consecutive polls, which I'll average, then plot on a graph. I will take the starting date of the first of the eight polls, and the ending date of the last of the eight polls, and find the date exactly in between, and use that date as the x coordinate for my polling point. Then I'll do the same for the next eight polls. That's why I first check whether the total number of polls on the website is divisible by eight or not. If the number of polls on the website is not divisible by 8, I go to the else, and subtract out the remainder. After the loop is done, I will individually average the remaining. Here's my code:

package com.pollapp.presidentialelectionpolls;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    Document doc;
    Element table;
    Elements rows;
    Element row;
    Elements cols;
    Elements link;
    int remainderNum = 0, i = 0;
    String startingDate, endingDate;
    Date start, end;
    DateFormat df = new SimpleDateFormat("MM/dd");
    String[][] average = new String[8][3];
    ArrayList<String> nameOfSrc = new ArrayList<String>();
    ArrayList<String> dateWidth = new ArrayList<String>();
    ArrayList<String> Trump = new ArrayList<String>();
    ArrayList<String> Clinton = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        try {
            ConnectivityManager cm = (ConnectivityManager) this
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            Boolean isConnect = ni == null ? false : ni
                    .isConnectedOrConnecting();
            if (isConnect) {
                doc = Jsoup
                        .connect(
                                "http://www.realclearpolitics.com/epolls/2016/president/us/general_election_trump_vs_clinton-5491.html")
                        .get();
                table = doc.select("table.data.large").get(1);
                rows = table.select("tr");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }


        for (int i = 2; i < rows.size(); i++) {
            row = rows.get(i);
            cols = row.select("td");
            link = cols.get(0).select("a");
            nameOfSrc.add(link.get(0).text());
            dateWidth.add(cols.get(1).text());
            Clinton.add(cols.get(4).text());
            Trump.add(cols.get(5).text());
        }

        if ((dateWidth.size() % 8) == 0) {
            for (i = (dateWidth.size()-1); i <= 7; i = i-8) {
                average[i][0] = dateWidth.get(i);
                average[i][1] = Clinton.get(i);
                average[i][2] = Trump.get(i);

                average[i-1][0] = dateWidth.get(i-1);
                average[i-1][1] = Clinton.get(i-1);
                average[i-1][2] = Trump.get(i-1);

                average[i-2][0] = dateWidth.get(i-2);
                average[i-2][1] = Clinton.get(i-2);
                average[i-2][2] = Trump.get(i-2);

                average[i-3][0] = dateWidth.get(i-3);
                average[i-3][1] = Clinton.get(i-3);
                average[i-3][2] = Trump.get(i-3);

                average[i-4][0] = dateWidth.get(i-4);
                average[i-4][1] = Clinton.get(i-4);
                average[i-4][2] = Trump.get(i-4);

                average[i-5][0] = dateWidth.get(i-5);
                average[i-5][1] = Clinton.get(i-5);
                average[i-5][2] = Trump.get(i-5);

                average[i-6][0] = dateWidth.get(i-6);
                average[i-6][1] = Clinton.get(i-6);
                average[i-6][2] = Trump.get(i-6);

                average[i-7][0] = dateWidth.get(i-7);
                average[i-7][1] = Clinton.get(i-7);
                average[i-7][2] = Trump.get(i-7);

                startingDate = average[i][0].substring(0, average[i][0].indexOf(" "));
                endingDate = average[i-7][0].substring(average[i-7][0].lastIndexOf(" ") + 1);

            }
        } else {
            remainderNum = (dateWidth.size() % 8);

            for (i = (dateWidth.size() - 1); i <= (remainderNum + 7); i = i-8) {
                average[i][0] = dateWidth.get(i);
                average[i][1] = Clinton.get(i);
                average[i][2] = Trump.get(i);

                average[i-1][0] = dateWidth.get(i-1);
                average[i-1][1] = Clinton.get(i-1);
                average[i-1][2] = Trump.get(i-1);

                average[i-2][0] = dateWidth.get(i-2);
                average[i-2][1] = Clinton.get(i-2);
                average[i-2][2] = Trump.get(i-2);

                average[i-3][0] = dateWidth.get(i-3);
                average[i-3][1] = Clinton.get(i-3);
                average[i-3][2] = Trump.get(i-3);

                average[i-4][0] = dateWidth.get(i-4);
                average[i-4][1] = Clinton.get(i-4);
                average[i-4][2] = Trump.get(i-4);

                average[i-5][0] = dateWidth.get(i-5);
                average[i-5][1] = Clinton.get(i-5);
                average[i-5][2] = Trump.get(i-5);

                average[i-6][0] = dateWidth.get(i-6);
                average[i-6][1] = Clinton.get(i-6);
                average[i-6][2] = Trump.get(i-6);

                average[i-7][0] = dateWidth.get(i-7);
                average[i-7][1] = Clinton.get(i-7);
                average[i-7][2] = Trump.get(i-7);

                startingDate = average[i][0].substring(0, average[i][0].indexOf(" "));
                endingDate = average[i-7][0].substring(average[i-7][0].lastIndexOf(" ") + 1);   

            }
        }
        Toast.makeText(getApplicationContext(), startingDate + ", " + endingDate, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
4
  • just a side note, 0%8 is also 0. Commented Aug 17, 2016 at 9:14
  • 1
    What is the point of first using dynamic arraylists, to then put stuff into inflexible arrays? Btw: check java styleguides on naming. Variable names always start lowerCase; even when they denote human beings with such huge egos as Mr. "Dark prince" Trump or Hillary "Rotten" Clinton. Commented Aug 17, 2016 at 9:18
  • @GhostCat if you look at the website i'm using, it has all the polling data from when polling started. The array will contain 8 consecutive polls, which I'll average, then plot on a graph. I will take the starting date of the first of the eight polls, and the ending date of the last of the eight polls, and find the date exactly in between, and use that date as the x coordinate for my polling point. Then I'll do the same for the next eight polls. That's why I first check whether the total number of polls on the website is divisible by eight or not. Commented Aug 17, 2016 at 9:23
  • I believe this is the kind of question that asks for a Minimal, Complete, and Verifiable example. There’s all too much code in the example you have given. Please try to use the link, and it will be a lot easier to help you. Commented Aug 17, 2016 at 9:44

1 Answer 1

3

I want to give a different kind of answer. Your real problem is that your code is lacking reasonable abstractions.

Meaning: your program is about "data" around a "candidate". But you have all that information in many different lists. That is simply wrong.

Instead, you could create a class like Candidate; and a Candidate has a name, and probably data points associated to him.

In other words: you should try hard to get away from the "low level procedural" style of programming, where index 1 means "data point for Clinton" and 2 means "data point for Trump".

Instead, you would have to instances of that Candidate class; and push the data points into them. Doing so might enable you to get rid of a lot of your code; thereby making it much easier for you to work on the real problem that you are interested in solving.

Long story short: your code isn't using reasonable abstractions; thus it is so "abstract" to read, understand, and find bugs in it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.