mardi 24 mars 2015

How do I import multiple columns of data from an url?



I am trying to read data from a specific website. The values are tab separated values. Currently, I am trying to print the values from the last column(temperatures) but I will also be interested in reading values from the first column. I can't seem to get my code to work and actually print the values. Can someone please help? Also, any help on how to get both the first column and last at the same time would be helpful. Here is my code so far:



import java.util.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class InputOutput
{
public static void readTemp()
{

try
{
URL temperature = new URL("http://ift.tt/1DWu71A");
Scanner scan = new Scanner(new InputStreamReader(temperature.openStream())); //scanner to read url
while(scan.hasNext())
{
String data = scan.next(); //returns whole line
String[] nums = data.split(","); //split top array
long tempReading = Integer.parseInt(nums[5]); //read 6th column
System.out.println(tempReading); //print data in 6th column
}//end while statement
scan.close();
}
catch(MalformedURLException e)
{
System.out.println("Malformed URL: " + e.getMessage());
}
catch(IOException e)
{
System.out.println("I/O Error: " + e.getMessage());
}//end try/catch

}//end method readTemp
}//end class InputOutput



Aucun commentaire:

Enregistrer un commentaire