Solved

Modifing code to write files from arraylist

Posted on 2004-04-14
22
394 Views
Last Modified: 2010-03-31
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
Comment
Question by:Drop_of_Rain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 5
22 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 10826854
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10826861
...then of course you would do

AudioConcat.main(callArgs);
0
 

Author Comment

by:Drop_of_Rain
ID: 10827055
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10827083
>>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
 

Author Comment

by:Drop_of_Rain
ID: 10827207
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827237
>>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
 

Author Comment

by:Drop_of_Rain
ID: 10827284
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827322
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10827400
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827415
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827424
>>That simply duplicates everything i've already said.

Apart from step 5), which is redundant
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10827520
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827554
>>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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 200 total points
ID: 10827562
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
 

Author Comment

by:Drop_of_Rain
ID: 10827589
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10827597
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827622
>>He was still asking another question.

What question?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827647
>>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
 

Author Comment

by:Drop_of_Rain
ID: 10827654
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10827683
8-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10830511
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
 

Author Comment

by:Drop_of_Rain
ID: 10830617
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 288
Error in @AspectJ Based AOP with Spring 2 23
hibernate example using maven 12 77
Eclipse Java import and method not resolved 4 73
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question