Re: java multidimensional string array
geletine wrote:
Now i am taking a new step and sending this array to a simpe text file,
i am just missing out on something i am sure , but here is the code in
two diffrent styles..
import java.io.*;
public class filearray {
public static void main(String args [])throws IOException {
PrintWriter outfile;
String filename;
filename="/home/allix/javafiles/filearray.txt";
String [] [] strings = { { "player1",
"player2",
"player3",
"player4",
"player5",
"player6" },
{"name1",
"name2",
"name3",
"name4",
"name5",
"name6" }
};
for (int i=0; i<strings[0].length; i++)
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0][i] + " " + strings[1][i]);
outfile.close();
// System.out.println(strings[0][i] + " " + strings[1][i]);
}
}
The output error is
javac filearray.java
filearray.java:22: cannot find symbol
symbol : variable i
location: class filearray
outfile.println(strings[0][i] + " " + strings[1][i]);
^
filearray.java:22: cannot find symbol
symbol : variable i
location: class filearray
outfile.println(strings[0][i] + " " + strings[1][i]);
^
2 errors
allix@allix:~/javafiles$
the seconds design is like this
import java.io.*;
public class filearray2 {
public static void main(String args [])throws IOException {
PrintWriter outfile;
String filename;
filename="/home/allix/javafiles/filearray2.txt";
String [] [] strings = { { "player1", "name1"},
{ "player2", "name2"},
{"player3", "name3"},
{"player4", "name4"},
{"player5" , "name5"},
{ "player6", "name6"},
};
for (int i=0; i<strings.length; i++)
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[i][0]+ " " + strings[i][1]);
outfile.close();
// System.out.println(strings[i][0]+ " " + strings[i][1]);
}
}
filearray2.java:16: cannot find symbol
symbol : variable i
location: class filearray2
outfile.println(strings[i][0]+ " " + strings[i][1]);
^
filearray2.java:16: cannot find symbol
symbol : variable i
location: class filearray2
outfile.println(strings[i][0]+ " " + strings[i][1]);
^
2 errors
allix@allix:~/javafiles
which is the same error
i am not sure why its not seeing the variable i....
thanks again for everyones help
You need { } around all the statements that you want to include in your
for loop, otherwise it's just the first statement, and i is used in the
second.
Mark