Ahmed Talha
Ahmed Talha

Reputation: 306

Avoiding comma at the start of .CSV file in java

I am scrapping data from a website and store it in CSV file. When the data gets in the CSV file it was getting the comma at the last place of every line. Somehow I manage to handle it. But, now I am getting that comma at the very start of every line which is creating another column. Following is my code.

for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
                    if (it.hasNext()) {
                        sb.append("  \n  ");
                    }
                    for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) {
                        Element tdElement = it.next();
                        final String content = tdElement.text();

                        if (it2.hasNext()) {
                            sb.append("   ,   ");
                            sb.append(formatData(content));
                        }

                        if (!it2.hasNext()) {
                            String content1 = content.replaceAll(",$", " ");
                            sb.append(formatData(content1));
                            break;
                        } //to remove last placed Commas.

                    }

                    System.out.println(sb.toString());
                    sb.flush();
                    sb.close();

Result which I want e.g: a,b,c,d,e
Result which I am getting e.g: ,a,b,c,d,e

Upvotes: 0

Views: 107

Answers (2)

Priyamal
Priyamal

Reputation: 2989

StringBuffer sb = new StringBuffer(" ");
    for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
                    if (it.hasNext()) {
                        sb.deleteCharAt(sb.length() - 1);
                        sb.append("  \n  ");
                    }
                    for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) {
                        Element tdElement = it.next();
                        final String content = tdElement.text();

                        if (it2.hasNext()) {
                            sb.append(formatData(content));
                            sb.append(",");

                        }

                        if (!it2.hasNext()) {
                            String content1 = content.replaceAll(",$", " ");
                            sb.append(formatData(content1));
                            break;
                        } //to remove last placed Commas.

                    }

                    System.out.println(sb.toString());
                    sb.flush();
                    sb.close();


}

im trying to remove the last character which in your case is a , at the instance where it is trying to move to a new line try replacing with my code and make sure to instantiate stringbuffer with a space passed as a string.

Upvotes: 0

joel314
joel314

Reputation: 1080

If you're developing in Java 8, I suggest that you use StringJoiner. With this new class, you don't have to build the string yourself. You can find an example to create a CSV with StringJoiner here.

I hope it helps.

Upvotes: 3

Related Questions