0

I have two array lists like shown below,

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

class Reader {
    public static void main(String args[]) throws FileNotFoundException {

        ArrayList<String> animal = new ArrayList<>();
        animal.add("Dog");
        animal.add("Cat");
        animal.add("Mouse");

        ArrayList<Integer> numbers = new ArrayList<>();
        for (int i = 1; i <= 15; i++) {
            numbers.add(i);
        }

        PrintWriter writer = new PrintWriter("animals.csv");

        for (String ani : animal) {
            writer.println(ani + ",");
        }

        writer.close();    
    }
}

I want the output to be like this in the .csv file.

Dog,1,2,3,4,5,
Cat,6,7,8,9,10,
Mouse,11,12,13,14,15,

When I use for-loops, the output of the array lists gets printed one below the other in the .csv file.

How do I format the array lists the way I desired, so it looks like this in Excel? enter image description here

7
  • writer.println(ani + ","); remove ln and use writer.print(ani + ","); Commented Nov 11, 2016 at 3:47
  • But how do we print the numbers arraylist next to it? Commented Nov 11, 2016 at 3:50
  • @KematRochi: what is the condition for number? Is it fixed 5 numbers for each animal? Commented Nov 11, 2016 at 3:54
  • why not use hashmap? Commented Nov 11, 2016 at 3:54
  • @TanmayBaid - Yes, the five numbers are fixed! Commented Nov 11, 2016 at 3:56

2 Answers 2

1

use writer.print() instead of writer.println()

Notice 'ln' in the method name you are using, which means print line.


Update: To also add numbers along with animal:

public static void main(final String[] args) throws FileNotFoundException {
    ArrayList<String> animal = new ArrayList<>();
    animal.add("Dog");
    animal.add("Cat");
    animal.add("Mouse");

    ArrayList<Integer> numbers = new ArrayList<>();
    for (int i = 1; i <= 15; i++) {
        numbers.add(i);
    }

    PrintWriter writer = new PrintWriter("animals.csv");

    List<String> row = new ArrayList<>();
    int i = 0;
    for (String ani : animal) {
        row.add(ani);
        for (int j = 0; j < 5; j++) {
            row.add(String.valueOf(numbers.get(i++)));
        }
        writer.println(String.join(",", row));
        row.clear();
    }

    writer.close();
}
Sign up to request clarification or add additional context in comments.

2 Comments

stringjoiner is available in java8 too
I tried this code and seems to be working great to meet my requirements.
0

Your writer method is printing a line. You need to use just print.

So writer.println(ani + ","); becomes writer.print(ani + ",");

Also, to add numbers as you asked you can do this as an alternative. This works as you said you have only 5 elements in each row after animal name.

PrintWriter writer = new PrintWriter("animals.csv");
int rowCounter = 0;
for (String ani : animals) {
  writer.print(ani);
  for (int i = rowCounter * 5; i < (rowCounter * 5 + 5); i++) {
    writer.print("," + numbers.get(i));
  }
  writer.println();
  rowCounter++;
}

writer.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.