Solved

Modifing code to write files from arraylist

Posted on 2004-04-14
22
390 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
  • 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 67
Running Jira on Raspberry PI 2? 3 121
Java / Linux and Regular Expressions 11 45
Java DateChooser? 2 5
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now