Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:19:24+00:00 2026-05-30T16:19:24+00:00

Suppose I have ArrayList with following contents public void add() { obj.add(1); obj.add(123456789); obj.add(1.222);

  • 0

Suppose I have ArrayList with following contents

public void add() {

    obj.add(1);
    obj.add(123456789);
    obj.add(1.222);
    obj.add(2.333);  

    obj.add(2);
    obj.add(9876541);
    obj.add(4.555);
    obj.add(7.888);



}

I want the contents of the ArrayList to be written in the txt file as follows

1,123456789,1.222,2.333
2,9876541,4.555,7,888 

I have following code to write the contents to the file

  public void createCSV() {

    try {
        FileWriter fos = new FileWriter(
                "/home/data.txt");
        PrintWriter out = new PrintWriter(fos);

        for (int i = 0; i < obj.size(); i++) {

            out.write(String.valueOf(obj.get(i)));
            out.write(",");

        }
        out.close();

    } catch (Exception e) {

    }
}

public static void main(String[] args) {

    JavaOp_CSV x = new JavaOp_CSV();
    x.add();
    x.createCSV();

}

As of now the contents in the file is

1,123456789,1.222,2.333,

Can anybody provide me with a logic to get the o/p as mention above??
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:19:25+00:00Added an answer on May 30, 2026 at 4:19 pm

    Use a StringBuilder to build the string you will later write to file, and keep a count i on the elements.

    if i % 4 == 0 – add a line break

    else: add a ','.

    Note: You need to avoid adding the new line at the first element, and possibly the last as well if total_num_elements % 4 != 0

    It can be easily done duing post processing using StringBuilder.deleteCharAt()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following class: public class Foo { private List<Integer> list =
Suppose I have an object that looks like: public class Obj { String foo;
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
Suppose I have an ArrayList of String and an ArrayList of int. My ArrayList
Suppose you have a Java method void foobar(int id, String ... args) and want
See the following ArrayList: List<Integer> values = new ArrayList<Integer>(); values.add(0); values.add(1); values.add(2); values.add(3); values.add(4);
Suppose I have a class A with property b Class A { public int
Suppose i have a List<Integer> l = new ArrayList<Integer>(); and would like to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.