Dan
Dan

Reputation: 2098

Printing data in a tabular form

I have wrote code that prints up a list of numbers that I receive from a JSON url. There are about 5 different urls used and the list is quiet long as each url contains over 100 numbers each.

What I would like to do is print off each of the url in tabular form i.e. if there is 5 urls, there will be 5 sets of printed results, all beside each other, insted of having one massive list.

Here is the code I have used so far;

public static void runTimestampCheck (String url) throws IOException
{
    //String jsonString = callURL("http://localhost:8000/eem/api/v1/metrics/temperature/288");
    String jsonString = callURL(url);
    Data data = new Gson().fromJson(jsonString, Data.class);
    //System.out.println(data.getRecords());
    long previousTimestamp = 01;
    long result;
    for(Data dataRecord : data.getRecords())
    {
        if(previousTimestamp > 0)
        {
            //Compare previous to current, should be diff of 60000
            long x = (Long)dataRecord.getTimestamp();
            result = x - previousTimestamp;
            //System.out.println(result);
            if(result != 30000)
            {
                System.out.println(result);
            }
        }
        System.out.println(dataRecord.getTimestamp() + "\n");
        previousTimestamp = dataRecord.getTimestamp();
    }
}

and this is called in;

    CheckTimestamp.runTimestampCheck("specific url");
    CheckTimestamp.runTimestampCheck("specific url");
    CheckTimestamp.runTimestampCheck("specific url");
    CheckTimestamp.runTimestampCheck("specific url");
    CheckTimestamp.runTimestampCheck("specific url");

As I said the result is 1 big list of numbers but I would like to have each url post results on its own line, if thats possible?

Any help is much appreciated.

Upvotes: 0

Views: 495

Answers (1)

Sorter
Sorter

Reputation: 10240

First Print the headers for the table.

URL1 URL2 URL3 URL4 URL5

Create a seperate method to fetch the json data in a list.

List getJsonDataFromURL(String url){

    //use Gson to fetch from url  
    // return list
}

You have to then create 5 ArrayLists after calling this function for different urls. Lets call them a,b,c,d,e

Use 5 Iterators to iterate through the lists and print the values.

Iterator<Data> aIt = a.iterator();
Iterator<Data> bIt = b.iterator();
Iterator<Data> cIt = c.iterator();
Iterator<Data> cIt = d.iterator();
Iterator<Data> cIt = e.iterator();

// assumes all the lists have the same size
while(aIt.hasNext())
{
    System.out.println(aIt.next().data +"  "+bIt.next().data+"  " cIt.next().data+" "+ dIt.next().data, +" "+eIt.next().data );
}

Upvotes: 1

Related Questions