0

I have to create lots of files to hold outputs for different objects, and I wondered if there was an easy way using loops, rather than typing out the whole thing every time. Currently, I am doing it like this, which quickly becomes unwieldy:

public void createFile(String fn, String fn1, String fn2, String fn3, String fn4, String fn5, String fn6){
    java.io.File filei = new java.io.File("fn.txt" );       
    java.io.PrintWriter earth1 = new PrintWriter(file);
    java.io.File file1 = new java.io.File("fn1.txt" );
    java.io.PrintWriter sun1 = new PrintWriter(file1);
    java.io.File file2 = new java.io.File("fn2.txt" );
    java.io.PrintWriter mercury1 = new PrintWriter(file2);
    java.io.File file3 = new java.io.File("fn3.txt" );
    ...etc
}

So manually writing out the lines for a new file. The string fn will have the value earth, fn1 will be sun, fn2 mercury etc. I want to use a for-loop ideally, but don't know how to loop over arguments! For example I need a way to make a loop recreate this pattern:

java.io.File filei = new java.io.File("fn.txt");
java.io.PrintWriter fn1 = new PrintWriter(filei);

java.io.File filei = new java.io.File("fn1.txt");
java.io.PrintWriter (fn1)1 = new PrintWriter(filei);

I need a printwriter and file for every argument. The printwriter needs have a name which is the argument with the number one appended. The file needs to be named 'argument.txt', so fn.txt, fn1.txt etc, which I'm not sure my code will currently achieve. I suspect it will literally name the files fn, fn1 etc rather than using the string the arguments represent, but can't check until I've completed my changes, for which I need the loop.

This is not concise, and may not be clear. If unclear, let me know, I'll try and word it better.

user13948
  • 443
  • 1
  • 6
  • 14
  • Can you pass these values as array or list? Like public void `createFile(String[] names) { for (String name : names) {...} }` – pzaenger Dec 12 '15 at 17:32
  • check this:http://stackoverflow.com/questions/4408059/iterating-through-method-parameters – Anoop Kanyan Dec 12 '15 at 17:34
  • @pzaenger as it turns out, I hadn't thought this through very well. I could pass an array of strings, but only if I can extract the name of an object as a string somehow. As in, if I create a planet object called earth, how do I get the value earth as a string? So it can be passed to a method? – user13948 Dec 12 '15 at 17:41
  • Why don't you pass the planet object itself? Objects don't have names. Variables do have names. You can have a `name` field of type String in a Planet object, though. – JB Nizet Dec 12 '15 at 17:43
  • @JBNizet That is a pretty brilliant idea that makes my life a lot easier! Thanks! – user13948 Dec 12 '15 at 17:44

2 Answers2

7

Pass a List<String>, or a String[] as argument, rather than passing 6 Strings.

Or put all those Strings in an array at the beginning of the method, and loop over that array.

Or use a vararg parameter, which allows passing as many Strings as argument (from the caller), while receiving an array in the method:

public void createFile(String... fn) {
    for (String f : fn) {
        ...

And in the caller:

createFile("a", "b", "c");
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

As JB Nizet suggested, but the best way that I would recommend is o use List fileNames as input argument.

KarthikRaja
  • 66
  • 1
  • 7