storing numbers in a matrix

ok ill first show my code

BufferedReader milesIN = new BufferedReader(new FileReader("miles.dat"));
String miles = milesIN.readLine(); //reads in miles.dat

while( (miles = milesIN.readLine()) != null )
{


if(miles.startsWith("*") != true) //skip comments
{
if(miles.contains(",") != true) //finding miles
{

StringTokenizer st = new StringTokenizer(miles);
//System.out.println(st.nextToken()); //prints out all miles in order

for( int i = 0; i < st.countTokens(); i++ )
{
for( int j = 0; j < st.countTokens(); j++ )
{
distanceBetweenCities[i][j] = st.nextToken();

}


}


}

}

}

milesIN.close(); //in closed

what i want to do is store numbers into a type of diagonal matrix
ex)
row 0 will have the numbers : (none)
row 1 will have the numbers : 966
row 2 will have the numbers : 1513 and 2410
row 3 will have the numbers : 2964 and 1520 and 604

im confused on how to construct the for loop
dat file is located at
http://www.cs.uiowa.edu/~sriram/21/fall07/miles.dat

thanks guys
[1349 byte] By [Mykullski] at [2007-11-20 11:18:28]
# 1 Re: storing numbers in a matrix
hey, i'm fairly new to java, but I think if you set your second for loop to go from 0 to i, then you'll get what you're looking for.

for( int j = 0; j <= i; j++ )
java_bean at 2007-11-10 2:14:10 >
# 2 Re: storing numbers in a matrix
but I think if you set your second for loop to go from 0 to i, then you'll get what you're looking for.err, No.

im confused on how to construct the for loop
Why do you have nested for loops. You are parsing one line of text at a time and that line of text contains from 0 to n distances which you need to add to your array. Each line you read in will have 1 more distance than the previous line so your 'i' index needs to increment every time you've read in a line and parsed it - it needs to be 0 for the first pass so it's probably best to increment it just before looping back to read in the next line. The 'j' index needs to be in a loop to add each of the distances to the array.

You need to think about what you are doing before you write code. Solve the problem using pen and paper before attempting code.
keang at 2007-11-10 2:15:16 >
# 3 Re: storing numbers in a matrix
i thought about it and as of now im right here can you help me out

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer ;

public class City
{

private String[] cityList;
private String[] stateList;
private String[] longitudeList;
private String[] latitudeList;
private String[] populationList;
private String[][] distanceBetweenCities;
private boolean[] selectedCities;
private boolean[] selectedEdges;


public City()
{
cityList = new String[128];
stateList = new String[128];
longitudeList = new String[128];
latitudeList = new String[128];
populationList = new String[128];
distanceBetweenCities = new String[128][128];

try
{

//reads city and parses
BufferedReader milesIN = new BufferedReader(new FileReader("miles.dat"));
String miles = milesIN.readLine(); //reads in miles.dat

int milesIndex = 0;
int cityIndex = 0;


while( (miles = milesIN.readLine()) != null ) //continues till .dat files has no more lines
{
if(miles.startsWith("*") != true) //skip comments
{
if(miles.contains(",") != true) //finding miles
{
while (milesIndex < cityIndex)
{
StringTokenizer st = new StringTokenizer(miles);
distanceBetweenCities[cityIndex][milesIndex] = st.nextToken();
System.out.println(distanceBetweenCities[cityIndex][milesIndex]);
milesIndex++;
}
cityIndex++;
}

}

}

milesIN.close(); //in closed

}

catch (IOException e)
{

}

}


public static void main(String args[])
{

new City(); //calls a method

}

its not storing in the array correctly and i get an array out of bounds error
Mykullski at 2007-11-10 2:16:12 >
# 4 Re: storing numbers in a matrix
not entirely sure with the code what the IOException Catch is for...but oh well...
rhinomist at 2007-11-10 2:17:11 >
# 5 Re: storing numbers in a matrix
also what is this and why do you have the ('*') or the coma(",")?

if(miles.startsWith("*") != true) //skip comments
{
if(miles.contains(",") != true) //finding miles
rhinomist at 2007-11-10 2:18:13 >
# 6 Re: storing numbers in a matrix
because in the .dat file (that i cant alter) i have to by pass all comments (*) and all cities (they have a , in their name)

this is my still unworking updated code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class City
{

private String[] cityList;
private String[] stateList;
private String[] longitudeList;
private String[] latitudeList;
private String[] populationList;
private String[][] distanceBetweenCities;
private boolean[] selectedCities;
private boolean[] selectedEdges;


public City()
{
cityList = new String[128];
stateList = new String[128];
longitudeList = new String[128];
latitudeList = new String[128];
populationList = new String[128];
distanceBetweenCities = new String[576][576];

try
{

//reads city and parses
BufferedReader in = new BufferedReader(new FileReader("miles.dat"));
String city; //reads in miles.dat

// read all the words from the file
System.out.println( "Reading miles.dat..." );
System.out.println();
int ctr = 0;
int milesIndex = 0;
int cityIndex = 1;

while( (city = in.readLine()) != null ) //continues till .dat files has no more lines
{
if(city.startsWith("*") != true) //skip comments
{
if(city.contains(",") == true) //finding words with ","
{

StringTokenizer st = new StringTokenizer(city, "," + "[" + "]");

cityList[ctr] = st.nextToken();
//System.out.println(cityList[ctr]);

stateList[ctr] = st.nextToken().replaceAll(" ", "");
//System.out.println(stateList[ctr]);

longitudeList[ctr] = st.nextToken();
//System.out.println(longitudeList[ctr]);

latitudeList[ctr] = st.nextToken();
//System.out.println(latitudeList[ctr]);

populationList[ctr] = st.nextToken();
//System.out.println(populationList[ctr]);
ctr++;

}

//city = in.readLine();
if(city.contains(",") != true)
{

while (milesIndex < cityIndex)
{

StringTokenizer st = new StringTokenizer(city);

while(st.hasMoreTokens())
{

distanceBetweenCities[cityIndex][milesIndex] = st.nextToken();
System.out.println(distanceBetweenCities[cityIndex][milesIndex]);
milesIndex++;
}

}
milesIndex = 0;
cityIndex++;
}

}

}

in.close(); //in closed
}







catch (IOException e)
{

}


}



public boolean[] selectAllCities()
{

selectedCities = new boolean[128];
for (int i = 0; i < 128; i++)
{
selectedCities[i] = true;

}
return selectedCities;

}

public boolean[] unselectAllCities()
{

selectedCities = new boolean[128];
for (int i = 0; i < 128; i++)
{
selectedCities[i] = false;

}
return selectedCities;

}

public boolean[] selectAllEdges()
{

selectedEdges = new boolean[128];
for (int i = 0; i < 128; i++)
{
selectedEdges[i] = true;

}
return selectedEdges;

}

public boolean[] unselectAllEdges()
{

selectedEdges = new boolean[128];
for (int i = 0; i < 128; i++)
{
selectedEdges[i] = false;

}
return selectedEdges;

}

public boolean[] printEdges()
{
for (int i = 0; i < 128; i++)
{
System.out.println(selectedEdges[i]);

}
return selectedEdges;
}

public static void main(String args[])
{

new City(); //calls a method

}

}

and im stuck here

if(city.contains(",") != true)
{

while (milesIndex < cityIndex)
{

StringTokenizer st = new StringTokenizer(city);

while(st.hasMoreTokens())
{

distanceBetweenCities[cityIndex][milesIndex] = st.nextToken();
System.out.println(distanceBetweenCities[cityIndex][milesIndex]);
milesIndex++;
}

}
milesIndex = 0;
cityIndex++;
}
Mykullski at 2007-11-10 2:19:13 >
# 7 Re: storing numbers in a matrix
I get a headache looking at that code, so I can only give you general advice.

If you're reading data from a file, you can't assume it has a known fixed length, so either limit the amount you read from it, or better still, don't use arrays - use a collection class such as ArrayList.

If you do decide to use arrays, then when you loop through the arrays you must use the array length in the end of loop condition. The traditional way of iterating over an array looks like this:for (int i=0; i < array.length; i++) {
doSomething( array[i] ); // whatever
}You can change the variable names and add additional ('OR') expressions to the condition, but this basic format guarantees you won't fall off the end of the array and get an out of bounds error.

If you get a problem with a simple loops like these, step through the code by hand using a small dataset, writing down the values as they change, and paying particular attention to what happens at and near the end of the data.

You will need to rework your code to follow these principles, but it looks like it needs it anyway.

The purpose of computing is insight, not numbers...
R. Hamming
dlorde at 2007-11-10 2:20:22 >