Array setting values to 0

This is an odd problem. I’m reading values from a text file into different arrays. If I print out the array value right after it has been assigned in the loop, it is correct. However, if I try to access the value after the loop is completed, I get zero - EXCEPT for the values assigned during the final loop!

Here is my code:

for(int f=0; f< mesh[i][m].faces; f+=1)
								{ 
									x.next();
									//System.out.println(x.next());
									mesh[i][m].fmaterial[f] = x.nextInt();
									mesh[i][m].ftype[f] = x.next();
									mesh[i][m].fgeo[f] = x.nextInt();
									mesh[i][m].flight[f] = x.nextInt();
									mesh[i][m].ftex[f] = x.nextInt();
									mesh[i][m].fextralight[f] = x.nextFloat();
									mesh[i][m].fverts[f] = x.nextInt();
									mesh[i][m].setvert(f);
									
										for(int v=0; v<mesh[i][m].fverts[f]; v+=1)
										{
											mesh[i][m].fvert[f][v] = x.nextInt();
											//System.out.println(mesh[i][m].fvert[v][vnum]);
											mesh[i][m].ftexvert[f][v] = x.nextInt();
											//System.out.println(mesh[i][m].ftexvert[v][vnum]);
										}

								}
								for(int f=0; f<mesh[i][m].faces; f+=1)
								{
									for(int v=0; v<mesh[i][m].fverts[f]; v+=1) //only the final loops values are correct??
									{
										System.out.println(mesh[i][m].fvert[f][v]+", "+mesh[i][m].ftexvert[f][v]);
									}
								}

And example output:

0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
0, 0
4, 4
7, 3
6, 2
5, 5

Any thoughts on this?

Some common sub-expression elimination, along with a little bit of encapsulation would go a long way to making that code more readable.

As to your problem; perhaps a bug in whatever your setvert(f); method does? (presumably initialise the fvert array?)
Perhaps it is not using the ‘f’ parameter correctly?

I’m pretty new to java, could you give me an example of common sub-expression elimination?

You are right, setvert() initializes the fvert array:

public void setvert( int num )
	{
		ftexvert = new int[faces][fverts[num]];
		fvert = new int[faces][fverts[num]];
	}

fverts[] is the number of vertices in the face.

It’s nothing specific to Java, try googling it
Ignore the aspects relating to performance; done right it can make code much easier to read.
For example, all the repeated multi-dimensional array dereferences could be eliminated if you did something simple like this:

Mesh currentMesh = mesh[i][m];

Can you see what the problem is now?

In setvert you are reinitialising the ftexvert & fvert arrays every time you iterate over the faces. They should be initialised only once.

I won’t go into how your code needs restructing, but I would suggest you have a read of the Java naming conventions.

Ah, I see! I’ll have to figure out how to initialize it with the numbers I want without being in the loop. At any rate it works now! Thanks for the tips :slight_smile: