HI, how would I call this method from main? help pls
import java.io.*;
publicclass FileArray{
staticvoid writeArray(String str,char[]anArray)throws IOException{
anArray[0] ='a';
anArray[1] ='b';
anArray[2] ='c';
anArray[3] ='d';
anArray[4] ='e';
anArray[5] ='f';
str ="temp.txt";
DataOutputStream output =new DataOutputStream(new FileOutputStream(str));
for(int i=0; i <anArray.length; i++)
output.writeChar(anArray[i]);
output.close();
}
publicstaticvoid main(String[] args)throws IOException{
writeArray(str, anArray);
}
}
i can't seem to get it right.>
[1491 byte] By [
asskoa] at [2007-11-27 8:41:12]

> whoops, sorry..
>
> i am trying to call it out in main, but i get errors
> using
>
> writeArray(str, anArray);
It's because str and anArray don't exist in the main method.
String myString = "asdf";
char[] myCharArray = new char[6];
writeArray(myString, myCharArray);
Also, what is the point of passing a char array to the writeArray() method if you are just going to change it?
> whoops, sorry..
>
> i am trying to call it out in main, but i get errors
> using
>
> writeArray(str, anArray);
Captain is already helping you, but please, next time be more specific. Exactly what errors are you getting (copy/paste them in) and on which lines?
jverda at 2007-7-12 20:40:03 >

import java.io.*;
public class FileArray {
static void writeArray(String str, char[]anArray) throws IOException{
anArray[0] = 'a';
anArray[1] = 'b';
anArray[2] = 'c';
anArray[3] = 'd';
anArray[4] = 'e';
anArray[5] = 'f';
str = "temp.txt";
DataOutputStream output = new DataOutputStream(new FileOutputStream(str));
for(int i=0; i <anArray.length; i++)
output.writeChar(anArray[i]);
output.close();
}
public static void main(String[] args) throws IOException {
writeArray(str, anArray);
}
}
Why are you passing a char array if you change it?
You will get NullPointerExceptions if you use this code how it is.
Problems would arise from the char array. If you pass in a char array of length 2, how could you modify the elements at 2, 3, 4 and 5?
Below that, you have a for statement. You should code something similar for the area where you assign values to anArray[0] to [5].
Or you could just not require a char array as a parameter at all, seeing as you're just overwriting it. You'd need to define it within the method though, because right now it's being defined as a parameter.>