Norman Joya
Norman Joya

Reputation: 38

How to avoid/remove writing the last comma in a row?

I'm working with a JTable and CSV files, the thing is I have to save the data inserted in the table, and then reopen if the user needs to make some modifications. So far I can save and load CSV file created from the table I was talking about, but BufferedWriter object is adding a comma at the end of every row, which causes a new blank column when loading an existing CSV file.

Here's my code, I've tried to restrict this matter but it's been unsuccessfully.

if(jTable1.getModel()==this.m){
            File file = new File(this.tfSelectedSpreadsheet.getText().toString());
            if(file.exists() && !file.isDirectory()){
                file.delete();
            }
            BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.tfSelectedSpreadsheet.getText().toString()),"UTF-8"));
            for(int i=0; i<jTable1.getColumnCount();i++){
                bfw.append(jTable1.getColumnName(i));
                if(i<jTable1.getColumnCount()) bfw.append(",");
                else bfw.newLine();
            }
            bfw.newLine();
            for(int i=0;i<jTable1.getRowCount();i++){
                for(int j=0;j<jTable1.getColumnCount();j++){
                    bfw.append((String)jTable1.getValueAt(i,j));
                    if(j<jTable1.getColumnCount()) bfw.append(",");
                    bfw.newLine();
                }
                bfw.newLine();
            }
            bfw.close();
        }

And this is how the CSV file generated by this code looks like:

Link,Long,Lat,CRS,
R011,120.1126,-33.5422,GDA94,
R014,120.1126,-33.5422,GDA94,
R019,120.1126,-33.5422,GDA94,
R011,120.1126,-33.5422,GDA94,
R011,120.1126,-33.5422,GDA94,
R011,120.1126,-33.5422,GDA94,
R015,120.1126,-33.5422,GDA94,
R014,120.1126,-33.5422,GDA94,
WANDA,25.12.1990,-37.0991,GDA90,

What could I do in order to fix this "comma" problem?

Upvotes: 0

Views: 2255

Answers (4)

Norman Joya
Norman Joya

Reputation: 38

thanks to MadProgrammer I solved the problem by using this code, I hope it could help someone out there.

if(jTable1.getModel()==this.m){
            File file = new File(this.tfSelectedSpreadsheet.getText().toString());
            if(file.exists() && !file.isDirectory()){
                file.delete();
            }
            BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.tfSelectedSpreadsheet.getText().toString()),"UTF-8"));
            for(int i=0; i<jTable1.getColumnCount();i++){
                bfw.append(jTable1.getColumnName(i));
                if(i<jTable1.getColumnCount()-1) bfw.append(",");//Here i changed the condition
                else bfw.newLine();
            }
            for(int i=0;i<jTable1.getRowCount();i++){
                for(int j=0;j<jTable1.getColumnCount();j++){
                    bfw.append((String)jTable1.getValueAt(i,j));
                    if(j<jTable1.getColumnCount()-1) bfw.append(",");//Here i changed the condition
                    else bfw.newLine();
                }
            }
            bfw.close();
        }

Upvotes: 0

isnot2bad
isnot2bad

Reputation: 24444

Since Java 8 you can use a StringJoiner:

StringJoiner joiner = new StringJoiner(",");
joiner.add("First");
joiner.add("Second");

String row = joiner.toString(); // produces "First,Second"

You can even define a prefix and suffix:

StringJoiner joiner = new StringJoiner(",", "[", "]");
joiner.add("First");
joiner.add("Second");

String row = joiner.toString(); // produces "[First,Second]"

If your data is already available as collection (or something else that can produce a Stream), you can also use the new stream API instead of writing another loop:

List<String> data = ...
String row = data.stream().collect(Collectors.joining(","));

Upvotes: 10

MadProgrammer
MadProgrammer

Reputation: 347214

if(i<jTable1.getColumnCount()) bfw.append(",");

i will always be less then jTable1.getColumnCount(), remember, Java is 0 indexed, so the number of columns go from 0-jTable1.getColumnCount() - 1

Try using something more like...

if(i< == Table1.getColumnCount() - 1) bfw.append(",");

...instead

Upvotes: 3

John3136
John3136

Reputation: 29266

I always add the comma before the item unless the index is 0.

BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.tfSelectedSpreadsheet.getText().toString()),"UTF-8"));
for(int i=0; i<jTable1.getColumnCount();i++){
    if (i > 0) {
        bfw.append(",");
    }
    bfw.append(jTable1.getColumnName(i));
}
bfw.newLine();

Upvotes: 4

Related Questions