0

I am trying to parse this csv file but when i got to print it I get "Input length =1" as the output. Here is my code: Can anyone provide an explanation as to why this is happening?

try {
        List<String> lines = Files.readAllLines(Paths.get("src\\exam1_tweets.csv"));
        for(String line : lines) {
            line = line.replace("\"", "");
            System.out.println(line);
        }
    }catch (Exception e) {
        System.out.println(e.getMessage());
    }
griplur
  • 15
  • 6
  • What is the expected output? What are you getting ? Do you see only one line of output? Can you show at least a few lines of what the csv looks like ? – SomeDude Mar 02 '18 at 16:27
  • 1
    If you have read the full exception feedback you'd know the problem. https://stackoverflow.com/questions/26268132/all-inclusive-charset-to-avoid-java-nio-charset-malformedinputexception-input – Gabriel Mar 02 '18 at 16:29
  • By doing `System.out.println(e.getMessage());` you lost a lot of information about the exception. Don't do that. – Gabriel Mar 02 '18 at 16:30
  • 1
    To add to what Gabriel said... e.printStackTrace() would provide much more useful information. Can you provide basic contents of the file ? – Max Friederichs Mar 02 '18 at 16:31
  • I'm using a twitter dump.https://drive.google.com/file/d/1-HQ-HaTp80bBiNG8hpLMZqlZriqf7C5l/view – griplur Mar 02 '18 at 16:43

3 Answers3

0

You want this change

List<String> lines = Files.readAllLines(Paths.get("src\\exam1_tweets.csv"),
                StandardCharsets.ISO_8859_1);

It was encoding issue please read this.

To see full cause of errors you should use e.printStackTrace() in catch block.

Polish
  • 554
  • 1
  • 4
  • 18
0

I've made a csv parser/writer , easy to use thanks to its builder pattern

It parses csv file and gives you list of beans

here is the source code https://github.com/i7paradise/CsvUtils-Java8/

I've joined a main class Demo.java to display how it works

let's say your file contains this

Item name;price
"coffe ""Lavazza""";13,99
"tea";0,00
"milk
in three
lines";25,50
riz;130,45
Total;158

and you want to parse it and store it into a collection of

class Item {
    String name;
    double price;
    public Item(String name, double p) {
//      ...
    }
//...
}

you can parse it like this:

List<Item> items = CsvUtils.reader(Item.class)
                //content of file; or you can use content(String) to give the content of csv as a String
                .content(new File("path-to-file.csv"))
                // false to not include the first line; because we don't want to parse the header
                .includeFirstLine(false)
                // false to not include the last line; because we don't want to parse the footer
                .includeLastLine(false)
                // mapper to create the Item instance from the given line, line is ArrayList<String> that returns null if index not found
                .mapper(line -> new Item(
                        line.get(0),
                        Item.parsePrice(line.get(1)))
                        )
                // finally we call read() to parse the file (or the content)
                .read();
0

java code:

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class Sof {

    public static final String USER_DIR = "user.dir";

    public static void main(String[] args) {
        try {
            List<String> lines = Files.readAllLines(
                    Paths.get(System.getProperty(USER_DIR) + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "exam1_tweets.csv"),
                    StandardCharsets.ISO_8859_1);
            for (String line : lines) {
                line = line.replace("\"", "");
                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println("ERROR" + e.getMessage());
        }

    }

}

console:

Handle,Tweet,Favs,RTs,Latitude,Longitude
BillSchulhoff,Wind 3.2 mph NNE. Barometer 30.20 in, Rising slowly. Temperature 49.3 °F. Rain today 0.00 in. Humidity 32%,,,40.76027778,-72.95472221999999
danipolis,Pausa pro café antes de embarcar no próximo vôo. #trippolisontheroad #danipolisviaja Pause for? https://....,,,32.89834949,-97.03919589
KJacobs27,Good. Morning. #morning #Saturday #diner #VT #breakfast #nucorpsofcadetsring #ring #college? https://...,,,44.199476,-72.50417299999999
stncurtis,@gratefuldead recordstoredayus ?????? @ TOMS MUSIC TRADE https://...,,,39.901474,-76.60681700000001
wi_borzo,Egg in a muffin!!! (@ Rocket Baby Bakery - @rocketbabybaker in Wauwatosa, WI) https://...,,,43.06084924,-87.99830888
KirstinMerrell,@lyricwaters should've gave the neighbor  a buzz. Iv got ice cream and moms baked goodies ??,,,36.0419128,-75.68186176
Jkosches86,On the way to CT! (@ Mamaroneck, NY in Mamaroneck, NY) https://.../6rpe6MXDkB,,,40.95034402,-73.74092102
tmj_pa_retail,We're #hiring! Read about our latest #job opening here: Retail Sales Consultant [CWA MOB] Bryn Mawr PA - https://.../bBwxSPsL4f #Retail,,,40.0230237,-75.31517719999999
Vonfandango,Me... @ Montgomery Scrap Corporation https://.../kpt7zM4xbL,,,39.10335,-77.13652 ....
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154