views:

3618

answers:

4

I'm pretty new at this, and this is probably a pretty obvious answer. In my csv script, i'm trying to figure out how to read a csv file using the CSVReader class and write to a new csv file. The error is telling me writeNext(java.lang.String[]) cannot be applied to (java.lang.String). I've tried using a direct assignment and getString() method however nothing works. Any ideas?

I have the following code: UPDATED

public static void main(String[] args) throws IOException {
 //read csv file
 CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));

 //write to csv file 
 CSVWriter writer2 = new CSVWriter(new FileWriter("output.csv"), '\t');

 String [] nextLine;
 while ((nextLine = reader.readNext()) != null) {
  System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");
  String[] newRow = new String[] {nextLine[0],nextLine[2], nextLine[1]};
 }

 //writer2.writeNext(entries);
 writer2.writeAll(newRow);
 writer2.close();

}

Error: c:\java\examples\ETHImport.java:46: cannot find symbo symbol : variable newRow location: class ETHImport writer2.writeAll(newRow);

1 error

Thanks in advance.

+2  A: 
String[] entries = new String[] { "entry1", "entry2"};

It is expecting an array of entries rather than just one String. If you are following this example http://opencsv.sourceforge.net/ notice that

String[] entries = "first#second#third".split("#");

the split call at the end results in a String[].

for your code:

public static void main(String[] args) throws IOException {
        //read csv file
        CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));

        //write to csv file 
        CSVWriter writer2 = new CSVWriter(new FileWriter("output.csv"), '\t');

        String [] nextLine;
        while ((nextLine = reader.readNext()) != null) {
                writer2.writeNext( new String[] {nextLine[0],nextLine[2], nextLine[1]});
        }


        writer2.close();

}
Clint
what i'm struggling with is just building a string with selected columns for each recordset so i can write it to a new file
phill
You shouldn't be building a string. You should be building a string *array* with the selected columns.
Jon Skeet
awesome! I didn't know i could declare something in a method. thanks!
phill
+1  A: 

To easily put a String into a String[] use:

String[] entriesArr = new String[]{ entries };

or the shortcut

String[] entriesArr = { entries };

Now entriesArr is a String[] containing one element, entries.

jjnguy
Or the shortcut, as this is a declaration: String[] strs = { str };
Tom Hawtin - tackline
Good call !
jjnguy
+3  A: 

You need a string array, not a single string. Are you really sure you want to pull all the values into a single string to start with? I'd expect writeNext to write a single line based on the string values you give it, with it putting in the commas etc. After all, it's a CsvWriter - it should be doing the comma separating.

It looks to me like the writeAll method you're already using does what you want it to, and writeNext will just write a single line... what are you trying to achieve that writeAll doesn't do for you? Why do you need to read it line by line and construct entries in the first place? Can't you just call:

writer2.writeAll(allElements);

My guess is that's going to write a tab-separated file though, given how writer2 is constructed.

EDIT: Given the additional information in the comments, I suspect you want to:

  • Open a CsvReader
  • Open a CsvWriter (with an appropriate Writer and ; as the constructor arguments)
  • Loop reading a line at a time as a string array (using CsvReader.readNext()), creating a new string array for the output, and then calling CsvWriter.writeNext() to write that string array out to the file.
  • Close the CsvReader and CsvWriter in finally blocks (so they'll still get closed if anything goes wrong)

Building the new string array from the existing one would be something like (following your example):

String[] newRow = new String[] { oldRow[0], oldRow[2], oldRow[1] };

That would basically just keep the first three columns, switching round the second and third ones. If you need other columns, just change it in the same kind of way.

Note that I wouldn't recommend using FileWriter to write to a file - use a FileOutputStream and an OutputStreamWriter chained onto it, as this lets you set the character encoding instead of using the platform default one.

Jon Skeet
I'm pretty sure you're right. The Javadoc for CsvWriter agrees with your expectation, but I haven't ever used it.
Matt Kane
the goal is to extract a select number of columns in the csv file, manipulate/format some of them and then insert them into an output file with commas and using a semicolon as a line terminator. The writeall just dumps everything out rather than just outputing the columns i need
phill
I updated it with the suggested changes however i'm resulting in a different error now. It is saying '.class' is expected and there is an carrot pointing to nextLine.. Is it correct syntax to declare a string like this in a while loop?
phill
I'm not sure why you've got an error like that, but you're trying to use newRow *outside* the loop. You should be calling writer2.writeNext(newRow); *inside* the loop.
Jon Skeet
A: 

writer.writeNext(String[]) is looking for an array that it will then write as "[0],[1],[2]"

Since you are storing the entire csv file as one string, you need to split that string backup. Really you should instead not combine every item into one string but instead store it as an array of strings representing each individual line in the csv file.

Stephan