Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Modifing code to write files from arraylist

How could this class be modified to be able to get the audiofiles from an arraylist, the output file name will come from a variable from another class.
                   

public class AudioTest
{
     public static void main(String[] args)
     {
          AudioConcat.main(new String[] {
                    "-c",  // concatenation mode
                    "-o",  // output file specified
                    "c:/test/test.wav", // output file
                    "c:/test/wav/kp1.wav", // input file 1
                    "c:/test/wav/kp2.wav", // input file 2
                    "c:/test/wav/kp3.wav", // input file 3
                    "c:/test/wav/kp4.wav" // input file 4
                    });
     }
}
0
Drop_of_Rain
Asked:
Drop_of_Rain
  • 11
  • 6
  • 5
2 Solutions
 
CEHJCommented:
You could do something like this:

String[] callArgs = new String[7];
callArgs[0] = "-c";
// set 1 similarly
callArgs[2] = otherClass.getOutputFile();
for(int i = 3;i < callArgs.length;i++) {
      callArgs[i] = (String)arrayList.get(i - 3); // first four in array list
}
0
 
CEHJCommented:
...then of course you would do

AudioConcat.main(callArgs);
0
 
Drop_of_RainAuthor Commented:
It would have to have the -o in there as well. What is the [7] for? The number in the arraylist will not be known until it is finished being created by the user.

String[] callArgs = new String[7];
callArgs[0] = "-c";
// set 1 similarly
callArgs[2] = otherClass.getOutputFile();
for(int i = 3;i < callArgs.length;i++) {
     callArgs[i] = (String)arrayList.get(i - 3); // first four in array list
}
 
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
>>It would have to have the -o in there as well

that's why i said

>>// set 1 similarly

i.e.

callArgs[1] = "-o";

String[7] means the array can hold 7 elements

0
 
Drop_of_RainAuthor Commented:
I though that.  How is this section going to be writen into the file. the number of the files to be writen
will not be known.

"c:/test/wav/kp1.wav",
0
 
CEHJCommented:
>>the number of the files to be writen will not be known.

In that case, you simply need to size your array according to the number when it *is* known

String[] callArgs = new String[3 + numberOfFiles];
0
 
Drop_of_RainAuthor Commented:
OK but I still want to know this:

How is this section going to be writen into the file  "c:/test/wav/kp1.wav",
0
 
CEHJCommented:
I don't understand the question. Currently the hard-coded version you posted uses that file as an argument to the main method of another program. Your question is how can the code be modified so that file and the others can be got from an ArrayList
0
 
zzynxSoftware engineerCommented:
If you don't know the number of wav files beforehand, you better use a List.

1) you add "-c"
2) then you add "-o"
3) then you add the output file (you get from another class)
4) then you add all the elements of the arraylist one by one
5) now you turn the list into an array
6) you pass it to the main of your class
0
 
CEHJCommented:
>>If you don't know the number of wav files beforehand, you better use a List.

That simply duplicates everything i've already said. AND there already is a List so the number of file is know by its size
0
 
CEHJCommented:
>>That simply duplicates everything i've already said.

Apart from step 5), which is redundant
0
 
zzynxSoftware engineerCommented:
>> That simply duplicates everything i've already said
Don't agree. But don't like to argue.

Hi Richard, what about this:

public class AudioTest
{
     List theList = null;
     String theOutputFile = null;

     public AudioTest(List arrayList, String outputFile) {
         theList = arrayList;
         theOutputFile = outputFile;
     }

     public static void main(String[] args)
     {
         String[] callArgs = new String[3+theList.size()];
         callArgs[0] = "-c";
         callArgs[1] = "-o";
         callArgs[2] = theOutputFile;
         for(int i = 3;i < callArgs.length;i++) {
             callArgs[i] = (String)theList.get(i - 3);
         }
     }
}
0
 
CEHJCommented:
>>Don't agree.

Well i'm sorry but the important part of the code you just posted simply copies what i've already said
0
 
zzynxSoftware engineerCommented:
forgot AudioConcat.main(callArgs); of course.

public class AudioTest
{
     List theList = null;
     String theOutputFile = null;

     public AudioTest(List arrayList, String outputFile) {
         theList = arrayList;
         theOutputFile = outputFile;
     }

     public static void main(String[] args)
     {
         String[] callArgs = new String[3+theList.size()];
         callArgs[0] = "-c";
         callArgs[1] = "-o";
         callArgs[2] = theOutputFile;
         for(int i = 3;i < callArgs.length;i++) {
             callArgs[i] = (String)theList.get(i - 3);
         }
         AudioConcat.main(callArgs);
     }
}

Usage:

new AudioTest(<Your array list from the previous Q ;)>, "c:/test/test.wav").main( new String[] {} );
0
 
Drop_of_RainAuthor Commented:
If you don't know the number of wav files beforehand, you better use a List.

1) you add "-c"
2) then you add "-o"
3) then you add the output file (you get from another class)
4) then you add all the elements of the arraylist one by one
5) now you turn the list into an array
6) you pass it to the main of your class

The number will be accessible before it gets to this point, sorry about that. Anything needed will be available at this point in the program At this point it is getting close to being finished.
0
 
zzynxSoftware engineerCommented:
He was still asking another question.
I think I gave the solution to that.
Of course I gave him the whole picture. (which yours is part of)

Anyway, up to him to decide.
0
 
CEHJCommented:
>>He was still asking another question.

What question?
0
 
CEHJCommented:
>>Of course I gave him the whole picture. (which yours is part of)

I'm not objecting to your adding value to the answer by coding the whole thing for Drop_Of_Rain and that can be reflected in points splits, but it would be polite if you were to acknowledge where you are using code already posted.
0
 
Drop_of_RainAuthor Commented:
Well this question got interesting. You both have made a contribution to the answer. I will increase the points 100 points because of this. I thank you both for sharing your knowledge and experience to help me.
0
 
CEHJCommented:
8-)
0
 
zzynxSoftware engineerCommented:
You're welcome.
Thanks

Richard, allow me to give you a tip for future questions:
1) Of course when YOU think a Q is answered perfectly, you can close it. No doubt about that.
2) Nevertheless, it doesn't harm anyone if you leave the question open for at least a day.
    I saw already often that an even better answer came in just after accepting.
    Two or more experts always know more than one.
0
 
Drop_of_RainAuthor Commented:
If you are speaking to Drop of Rain I have found this to be true, but I have also been asked to close my questions. I will from now on leave my questions opened. My name is Christopher
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 11
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now