java multidimensional string array

G

geletine

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[0]);
}
}

when i run it it prints
player1
name1
if i change the line System.out.println(strings[0]); to
System.out.println(strings[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[0]); to
System.out.println(strings[0]+ strings[1]);
prints this
player1player2
name1name2

which is not what i would like to see

could anyone help?

thanks in advance
 
S

Simon

geletine said:
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[0]);
}
}


[...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] and strings[1].

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
 
G

geletine

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.
 
O

Oliver Wong

geletine said:
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
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.

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
 
S

Simon

geletine said:
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
 
G

geletine

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.
 
O

Oliver Wong

geletine said:
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
 
G

geletine

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] + " " + strings[1]);
outfile.close();
// System.out.println(strings[0] + " " + strings[1]);
}
}

The output error is

javac filearray.java
filearray.java:22: cannot find symbol
symbol : variable i
location: class filearray
outfile.println(strings[0] + " " + strings[1]);
^
filearray.java:22: cannot find symbol
symbol : variable i
location: class filearray
outfile.println(strings[0] + " " + strings[1]);
^
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[0]+ " " + strings[1]);
outfile.close();
// System.out.println(strings[0]+ " " + strings[1]);
}
}

filearray2.java:16: cannot find symbol
symbol : variable i
location: class filearray2
outfile.println(strings[0]+ " " + strings[1]);
^
filearray2.java:16: cannot find symbol
symbol : variable i
location: class filearray2
outfile.println(strings[0]+ " " + strings[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
 
M

Mark Thomas

geletine said:
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] + " " + strings[1]);
outfile.close();
// System.out.println(strings[0] + " " + strings[1]);
}
}

The output error is

javac filearray.java
filearray.java:22: cannot find symbol
symbol : variable i
location: class filearray
outfile.println(strings[0] + " " + strings[1]);
^
filearray.java:22: cannot find symbol
symbol : variable i
location: class filearray
outfile.println(strings[0] + " " + strings[1]);
^
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[0]+ " " + strings[1]);
outfile.close();
// System.out.println(strings[0]+ " " + strings[1]);
}
}

filearray2.java:16: cannot find symbol
symbol : variable i
location: class filearray2
outfile.println(strings[0]+ " " + strings[1]);
^
filearray2.java:16: cannot find symbol
symbol : variable i
location: class filearray2
outfile.println(strings[0]+ " " + strings[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
 
G

geletine

i have these lines
for (int i=0; i<strings.length; i++){
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0]+ " " + strings[1]);
outfile.close();
}
and

for (int i=0; i<strings[0].length; i++){
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0] + " " + strings[1]);
outfile.close();
}

and both just print to the text file

player6 name6

i want all players and names to print to the file
 
O

Oliver Wong

geletine said:
i have these lines
for (int i=0; i<strings.length; i++){
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0]+ " " + strings[1]);
outfile.close();
}
and

for (int i=0; i<strings[0].length; i++){
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0] + " " + strings[1]);
outfile.close();
}

and both just print to the text file

player6 name6

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
 
G

geletine

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] + " " + strings[1]);
}
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...
 
F

Fred Kleinschmidt

geletine said:
i have these lines
for (int i=0; i<strings.length; i++){
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0]+ " " + strings[1]);
outfile.close();
}
and

for (int i=0; i<strings[0].length; i++){
outfile = new PrintWriter (new FileWriter (filename),true );
outfile.println(strings[0] + " " + strings[1]);
outfile.close();
}

and both just print to the text file

player6 name6

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top