-2

My program is to take in a URL that has a CSV file. I'm trying to pass the this into a file for further querying, but it returns with a NoSuchElementException.

public static void main(String[] args) throws ParseException, IOException, URISyntaxException {
    // TODO Auto-generated method stub

    String web = "https://example.com/data.csv";
    URL content = new URL(web);

    File file = new File(content.getPath());

    try {
        Scanner inputStream = new Scanner(file);    //Input file
        String[] values = data.split(",");
        while (inputStream.hasNext()){

            data = inputStream.next();

            values = data.split(",");
            System.out.println(values[];
        }
        inputStream.close();
   } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
Dallan Byrne
  • 9
  • 1
  • 5

1 Answers1

1

from https://docs.oracle.com/javase/7/docs/api/java/net/URL.html

getPath() Gets the path part of this URL. This is not what you want.

openStream() Opens a connection to this URL and returns an InputStream for reading from that connection. This is what you want.

    URL content = new URL(web);
    InputStream stream = content.openStream();

    Scanner inputStream = new Scanner(stream);
    while (inputStream.hasNext()) {
        String data = inputStream.next();
        String[] values = data.split(",");
        System.out.println(values);
    }
    inputStream.close();

Also your url links to gzipped file, which cannot be read directly you need to either download it first, ungzip and treat like normal local file, or provide a link to plain CSV file.

Community
  • 1
  • 1
Milo Bem
  • 1,033
  • 8
  • 20