Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read entries in a file.txt into a 2 dim array

Status
Not open for further replies.

NuJoizey

MIS
Aug 16, 2006
450
US
I'm a newbie trying to learn Java & Swing and I'm attempting to use the Jtable element to display really simple car records in CSV format in my UI. The values in my file are like so:

1970,Chevy,Chevelle,16000
1972,Oldsmobile,Cutlass,15000
1969,Chevy,Camaro,22000
1971,Plymouth,Barracuda,19500

and as far as I can tell, from the documentation i've read

( the suggested way for it to work should just be to read in the records to the JTable as a two dimensional object array. But I keep getting java.lang.NullPointerException.

what am i doing wrong? the tokens seem to print out just fine, but I can't read them into the array. i tried a lot of things, spent a good deal of time with my Java book and Mr. Google, but nothing seems to be working and i'm stuck. As far as i can tell, I should be able to:

1. use buffered file reader to read the records
2. use either split() or token to break the records out by the comma delimiter
3. pack the data into a two dimensional array
4. feed that into the JTable constructor.

right? well, I'm stuck on step 3. - here's some code:

Code:
		try
		{
			FileReader fr = new FileReader("carStock.txt");
			BufferedReader br = new BufferedReader(fr);
			String newline=null;
			int i=0;
			String myCarData[][] = null;		
			
			
			while((newline=br.readLine())!=null)
			{
				//carInfoFromFile.add(newline);		
				String[] theLine = newline.split(",");
				
				for(String token:theLine)
				{
					System.out.println(token);	
					myCarData[i][0]=token;
					myCarData[i][1]=token;
					myCarData[i][2]=token;
					myCarData[i][3]=token;
				
					System.out.println(myCarData[i][0]);
				}			
				
			}
			i++;
		}
		catch (FileNotFoundException e)
		{		
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
 
I do a similar thing and, being a novice myself, I don't know if this is the most efficient:
Code:
         row = 0;
         while ((line=dbFile.readLine()) != null) {
               parseLine(line, linelm);
               for (int i=0; i<numfields; i++) {petDb[row][i]=linelm[i];}
               row++;
         }

where my method, parseLine, returns a 1-D array, linelm for each line of the database (csv) file.

_________________
Bob Rashkin
 
Code:
public class TestArray{
       public static void main(String args[]){
              String record[][] = new String[100][2]; 
              // you may replace 100 with the number of row of records
                  record [0][0] = "hello";
                  System.out.println(record[0][0]);
       }
}

You need to initialize array
String myCarData[][] = new String[100][2];
 
ok, so far i have learned that i did not initialize my array correctly. so now i have:

Object myCarData[][] = new String[4][4];

but i still do not see how to successfully iterate over all of the csv records that get read in from my text file: i.e.

Code:
while((newline=br.readLine())!=null)
{
   //Parse the line being read in
   String[] theLine = newline.split(",");
   myCarData[0][0] = theLine[0];
   myCarData[0][1] = theLine[1];
   myCarData[0][2] = theLine[2];
   myCarData[0][3] = theLine[3];

//now what????  how do i get to the next "theLine?"
}

ok, that's fine, but that only seems to output the first line. How do I iterate to the next line?

I realize I should be using two for loops to populate the array, but i am doing it longways first. But I can't figure it out longways. I know this is probably a simple matter of syntax. What am I missing?
 
Code:
int currentLine = 0;
while((newline=br.readLine())!=null)
{
   //Parse the line being read in
   String[] theLine = newline.split(",");
   myCarData[currentLine][0] = theLine[0];
   myCarData[currentLine][1] = theLine[1];
   myCarData[currentLine][2] = theLine[2];
   myCarData[currentLine][3] = theLine[3];
   currentLine++;
}
//now what???? how do i get to the next "theLine?"
The while statement wil pull th enext line from the file, until the end of the file is reached. Be aware that there can be only 4 lines of CarData, or you'll have to adjust the array definition ;-)

HTH
TonHu
 
Yeah, it's really easy when you know how. Thanks everyone for the discussion and the code samples.

My problem really is that I just don't "get" how you put together the Java objects to make them work as desired (yet) - I knew this was simple This is what I came up with on my own through brute force....
Code:
for(int i=0;i<4;i++)
{
       String newline=br.readLine();
       for(int j=0;j<4;j++)
       {
	String[] theLine = newline.split(",");
	myCarData[i][j]=theLine[j];
       }
}
 
Could be something like:
Code:
String newline; // optimize/avoid garbage collection
String[] theLine;
for(int i=0;i<4;i++) {
    newline=br.readLine();
    if (newline != null) { // just to avoid a NPE
        theLine = newline.split(",");
        for(int j=0;j<4;j++) {
            myCarData[i][j]=theLine[j];
        }
    }
}
You where almost fine ;-)

HTH
TonHu
 
I'd have to test this, but ..

Code:
String newline; // optimize/avoid garbage collection
String[] theLine;
for(int i=0;i<4;i++) {
    newline=br.readLine();
    if (newline != null) { // just to avoid a NPE
        theLine = newline.split(",");
             myCarData[i]=theLine;
        }
    }
}

I thing you can assing 1-D arrays this way

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top