简体   繁体   中英

How to add each value to a separate line?

My code does read and write the file, but it is not on a new line for every value and instead prints every value in one line.

// 2 points
static void Q1(String inputFilename, String outputFilename) {
    // You are given a csv file (inputFilename) with all the data on a single line. Separate the
    // values by commas and write each value on a separate line in a new file (outputFilename)

        String data = "";
        try {
            for(String s :Files.readAllLines(Paths.get(inputFilename))){
                data = data + s;
            }
            Files.write(Paths.get(outputFilename), data.getBytes());
        } catch (IOException e) {

            e.printStackTrace();
        }
}

As such the grader says:

Incorrect on input: [data/oneLine0.csv, output0.txt]
Expected output : overwrought plastic bomb
wrapped litter basket
obstetric matter of law
diabetic stretching
spatial marathi
continental prescott
reproductive john henry o'hara
hollow beta blocker
stereotyped national aeronautics and space administration
irremediable st. olaf
brunet fibrosis
embarrassed dwarf elm
superficial harrier
disparaging whetstone
consecrate agony
impacted lampoon
nefarious textile
some other organisation
Your output     : overwrought plastic bomb,wrapped litter basket,obstetric matter of law,diabetic stretching,spatial marathi,continental prescott,reproductive john henry o'hara,hollow beta blocker,stereotyped national aeronautics and space administration,irremediable st. olaf,brunet fibrosis,embarrassed dwarf elm,superficial harrier,disparaging whetstone,consecrate agony,impacted lampoon,nefarious textile,some other organisation

First of all, you need to remove the comma from the CSV file. I'd suggest using
s = s.replace(",",""); Additionally, you must append a \\n to each string to make it appear on a new line. So, you should add s += "\\n"; This yields the code:

// 2 points
static void Q1(String inputFilename, String outputFilename) {
// You are given a csv file (inputFilename) with all the data on a single line. Separate the
// values by commas and write each value on a separate line in a new file (outputFilename)

    String data = "";
    try {
        for(String s :Files.readAllLines(Paths.get(inputFilename))){
            s.replace(",","");
            s += "\n";
            data = data + s;
        }
        Files.write(Paths.get(outputFilename), data.getBytes());
    } catch (IOException e) {

        e.printStackTrace();
    }
}
String data = "";
try {
    // input file has all data on one line, for loop isn't necessary here
    // input file has elements separated by comma characters
    for(String s : Files.readAllLines(Paths.get(inputFilename))){
        data = data + s;
    }
    String[] separated = data.split(",");// does not handle embedded commas well
    data = "";
    // output file should have each comma separated value on its own line
    for (String t : separated) {
        data = data + t + System.getProperty("line.separator");
    }
    Files.write(Paths.get(outputFilename), data.getBytes());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM