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.

5
  • Can you pass these values as array or list? Like public void createFile(String[] names) { for (String name : names) {...} } Commented Dec 12, 2015 at 17:32
  • check this:stackoverflow.com/questions/4408059/… Commented Dec 12, 2015 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? Commented Dec 12, 2015 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. Commented Dec 12, 2015 at 17:43
  • @JBNizet That is a pretty brilliant idea that makes my life a lot easier! Thanks! Commented Dec 12, 2015 at 17:44

2 Answers 2

5

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

it is not an answer, but a comment, and also a judgement

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.