Java Forum / General / May 2006
java multidimensional string array
geletine - 05 May 2006 14:00 GMT I have this elementary short piece of code that works
import java.io.*; public class filearray { public static void main(String args [])throws IOException { String [] [] strings = { { "player1", "player2", "player3", "player4", "player5", "player6" }, {"name1", "name2", "name3", "name4", "name5", "name6" } }; for (int i=0; i<strings.length; i++) System.out.println(strings[i][0]); } }
when i run it it prints player1 name1 if i change the line System.out.println(strings[i][0]); to System.out.println(strings[i][5]); it will print player6 name6
and i can change the print line to diffrent values to print different positions in the array, i would like to print all sets in one line so it gives a output like the following.
player1 name1 player2 name2 ..... player6 name6
changing System.out.println(strings[i][0]); to System.out.println(strings[i][0]+ strings[i][1]); prints this player1player2 name1name2
which is not what i would like to see
could anyone help?
thanks in advance
Simon - 05 May 2006 14:19 GMT > I have this elementary short piece of code that works > [quoted text clipped - 18 lines] > } > } [...program supposed to print player 1, name1, player 2, name2, ...]
As you noted, this loop will always be executed only twice, since strings.length == 2. You would have to write "for (int i = 0; i < strings[0].length; i++)" in order to fix this. Then you could print strings[0][i] and strings[1][i].
However, this strange condition in the loop indicates bad design. I would suggest to rearrange your array like this:
String[][] strings = {{ "player1", "name1" }, { "player2", "name2" } , ... };
Then your loop should work. You should also think about creating a class Player that contains the two strings and create a Player[] array instead.
Cheers, Simon
geletine - 05 May 2006 14:51 GMT Thank you , the first design works and the second design works with the original for loop
I have a question,, by creating a player class as you suggest that contains the two strings which then creates the player array, means double work, not just in code but surely in compile/run time?
with all the string values hard-coded in the main program, at compile/run time only one file needs to be read, and it also seems simple...
if on the other hand, you mean that by creating a seperate class with the player array , that class can be recycled again into another program , then i understand.
this is just a throw-away program, it won't be used in the future, hence i kept it simple.
> As you noted, this loop will always be executed only twice, since strings.length > == 2. You would have to write "for (int i = 0; i < strings[0].length; i++)" in [quoted text clipped - 10 lines] > Cheers, > Simon Oliver Wong - 05 May 2006 15:21 GMT > I have a question,, by creating a player class as you suggest that > contains the two strings which then creates the player array, means > double work, not just in code but surely in compile/run time? Yes, but unless you have an unbelievably slow machine, it's better to have a well designed program than a quick to compile program. If a programs is slow to compile, you can just "throw money at the problem" (e.g. buy a faster computer) to fix it. If a program is badly designed, you have to actually spend brain power to fix the design.
> with all the string values hard-coded in the main program, at > compile/run time only one file needs to be read, and it also seems [quoted text clipped - 3 lines] > the player array , that class can be recycled again into another > program , then i understand. You can avoid putting the new class into another file by making it a nested class.
> this is just a throw-away program, it won't be used in the future, > hence i kept it simple. Okay, if it's just to throw-away, then you can do whatever you want with it. Just note that sometimes throw-away programs will turn out to be more useful than you first imagined, and that may come back to haunt you.
- Oliver
geletine - 05 May 2006 15:36 GMT > You can avoid putting the new class into another file by making it a > nested class. ah, i am not awhere of nested classes, i presume its a class in a class.
> Okay, if it's just to throw-away, then you can do whatever you want with > it. Just note that sometimes throw-away programs will turn out to be more > useful than you first imagined, and that may come back to haunt you. i think the perl language created by Larry wall originally produced some reports from a Usenet news-like hierarchy of files for a bug-reporting system and now its a fully pledged scripting language.
Oliver Wong - 05 May 2006 15:42 GMT >> You can avoid putting the new class into another file by making it a >> nested class. > > ah, i am not awhere of nested classes, i presume its a class in a > class. Yes. The syntax is something like:
<untestedCode> public class Foo { static class Player { public String s1, s2; /*Add a constructor here which initializes the 2 strings.*/ }
public void bar() { Player[] p = new Player[10]; p[0] = new Player("Player 1", "Name 1"); } } </untestedCode>
- Oliver
Simon - 05 May 2006 15:34 GMT > I have a question,, by creating a player class as you suggest that > contains the two strings which then creates the player array, means > double work, not just in code but surely in compile/run time? Well, what is the compile time on your computer? I'd bet for this program the time to compile it is still dominated by the time it takes you to type "javac ..." on the command prompt or to click on "Compile" or whatsoever. I don't believe that the running time is affected by this very much.
In general, you can always put all your data in one big Object[] array whose elements may be Object[] arrays again. I doubt that saves you any time, though. :-)
> with all the string values hard-coded in the main program, at > compile/run time only one file needs to be read, and it also seems > simple... What do you mean by "only one file needs to be read"? Do you mean "read by the compiler"?
> if on the other hand, you mean that by creating a seperate class with > the player array , that class can be recycled again into another > program , then i understand. I don't mean a class with a player array but rather a class Player like this:
public class Player { private String id; // for playerN private String name; // for nameN }
Then you can start to add a toString() method which will simplify your main program etc. Since I assume you are doing this as an exercise, I suggest not to practice bad habits, hence my suggestion. :-)
Cheers, Simon
geletine - 05 May 2006 18:19 GMT 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
Mark Thomas - 05 May 2006 18:22 GMT > 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 [quoted text clipped - 84 lines] > > 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
geletine - 05 May 2006 18:33 GMT i have these lines 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(); } and
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(); }
and both just print to the text file
player6 name6
i want all players and names to print to the file
Oliver Wong - 05 May 2006 18:44 GMT >i have these lines > for (int i=0; i<strings.length; i++){ [quoted text clipped - 15 lines] > > i want all players and names to print to the file Assuming you actually want to become a skilled programmer, you need to stop, take a step back, and revisit your programming basics, like how loops work and such. If you have a programming textbook, go through the parts on loops again. Otherwise you'll always be at the mercy of this newsgroup.
- Oliver
geletine - 05 May 2006 21:10 GMT > Assuming you actually want to become a skilled programmer, you need to > stop, take a step back, and revisit your programming basics, like how loops > work and such. If you have a programming textbook, go through the parts on > loops again. Otherwise you'll always be at the mercy of this newsgroup. > > - Oliver i was not looking at the program seriously, i must put PrintWriter before the loop, and close it after the loop. ie..
outfile = new PrintWriter (new FileWriter (filename),true ); for (int i=0; i<strings[0].length; i++){ outfile.println(strings[0][i] + " " + strings[1][i]); } outfile.close();
i thought i put something wrong, and knew it could not be the for loop , as it printed to the screen, i am only redirecting the output to a text file instead of the console...
Fred Kleinschmidt - 05 May 2006 22:14 GMT >i have these lines > for (int i=0; i<strings.length; i++){ [quoted text clipped - 15 lines] > > i want all players and names to print to the file Each time through the loop you open the file, write one line to it (clobbering what used to be there), then close the file.
Open the file before the loop, and close it after the loop.
 Signature Fred L. Kleinschmidt Boeing Associate Technical Fellow Technical Architect, Software Reuse Project
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|