Trouble with storing values in 2-d array.

Hi, I am a Java newbie and would appreciate any help. I am writing a program to read in bandwidth traffic data from a text file and process it to make a graphical representation to analyze the data better. Anyways I haven't gotten very far yet. I am reading the text in from file a line at a time and splitting each line up into fields. I am trying to store each set of fields ( line of text) into a separate array so that I can manipulate the data better...when I get to that. Anyways, when I run the program it will output the first 3 fields of data and then give me the error 'Usage: java ReadFile filename' If I take out the code that stores my data in the array and just test it by outputting it to the screen it works fine. Reads in the file and prints out the entire file like I want it.

Thanks for any help.

import java.lang.*;

import java.io.*;

import java.util.regex.*;

public class Main{

public static void main(String[] args){

String line;

String [][] str = new String[88][4];

try {

FileReader input = new FileReader(args[0]);// Takes an arg and reads a file as input

BufferedReader bufRead = new BufferedReader(input);

line = bufRead.readLine(); // Reads the file one line at a time

while (line != null){

int i = 0;

String text = line;

String [] fields = text.split( "\\s*;\\s*" ); // splits data file into fields

while (fields.length < 3){// this gets rid of the first 12 lines of my

line = bufRead.readLine(); // file that are unnecessary data

text = line;

fields = text.split( "\\s*;\\s*" );

}

for (int j = 0; j < fields.length; j++){

str[j] = fields[j];

System.out.print(fields[j]);

}

i++;

line = bufRead.readLine();

}

bufRead.close();

}catch (ArrayIndexOutOfBoundsException e){

/* If no file was passed on the command line, this expception is

generated. A message indicating how to the class should be

called is displayed */

System.out.println("Usage: java ReadFile filename\n");

}catch (IOException e){

// If another exception is generated, print a stack trace

e.printStackTrace();

}

}// end main

}

Here is my text file I am reading from.

version;3

active;1

interface;eth0

nick;eth0

created;1182373685

updated;1184735101

totalrx;7832

totaltx;142830

currx;2464128311

curtx;3121558764

totalrxk;1

totaltxk;867

btime;1181771048

d;0;1184731201;38;1698;405;794;1

d;1;1184644801;755;17589;559;855;1

d;2;1184558401;540;2647;958;1012;1

d;3;1184472001;112;3685;377;34;1

d;4;1184385601;102;2957;523;359;1

d;5;1184299201;90;2605;961;781;1

d;6;1184212802;199;2442;53;300;1

d;7;1184126401;103;3092;494;124;1

d;8;1184040002;117;3097;1008;1009;1

d;9;1183953601;109;3136;435;337;1

d;10;1183867202;115;3921;737;1001;1

d;11;1183780801;125;4307;984;332;1

d;12;1183694401;114;3854;11;225;1

d;13;1183608001;133;4790;157;480;1

d;14;1183521601;135;4871;0;364;1

d;15;1183435201;142;5121;736;898;1

d;16;1183348801;118;3914;500;578;1

d;17;1183262401;134;4759;470;335;1

d;18;1183176001;122;4302;771;761;1

d;19;1183089602;149;5408;308;445;1

d;20;1183043514;72;2534;178;297;1

d;21;1182902401;3059;8065;334;486;1

d;22;1182816001;240;9431;435;521;1

d;23;1182729602;381;12444;204;96;1

d;24;1182643201;135;5059;628;696;1

d;25;1182556801;130;4535;782;899;1

d;26;1182470401;152;5438;197;753;1

d;27;1182384001;184;6704;113;316;1

d;28;1182373685;13;411;1019;115;1

d;29;0;0;0;0;0;0

m;0;1183262401;3190;78494;152;602;1

m;1;1182373685;4641;64336;873;265;1

m;2;0;0;0;0;0;0

m;3;0;0;0;0;0;0

m;4;0;0;0;0;0;0

m;5;0;0;0;0;0;0

m;6;0;0;0;0;0;0

m;7;0;0;0;0;0;0

m;8;0;0;0;0;0;0

m;9;0;0;0;0;0;0

m;10;0;0;0;0;0;0

m;11;0;0;0;0;0;0

t;0;1184644801;755;17589;559;855;1

t;1;1182729602;381;12444;204;96;1

t;2;1182902401;3059;8065;334;486;1

t;3;1182816001;240;9431;435;521;1

t;4;1182384001;184;6704;113;316;1

t;5;1182470401;152;5438;197;753;1

t;6;1183089602;149;5408;308;445;1

t;7;1183435201;142;5121;736;898;1

t;8;1182643201;135;5059;628;696;1

t;9;1183521601;135;4871;0;364;1

h;0;1184734501;37757;1663897

h;1;1184735101;1560;75649

h;2;1184655301;2707;52378

h;3;1184658901;2309;29868

h;4;1184662501;5689;166692

h;5;1184666101;4951;132382

h;6;1184669701;4482;128149

h;7;1184673301;1305;389

h;8;1184676901;1304;380

h;9;1184680501;1463;9641

h;10;1184684101;7479;320802

h;11;1184687701;10266;215760

h;12;1184691301;7682;338884

h;13;1184694901;1533;11403

h;14;1184698501;144097;127520

h;15;1184702101;6260;290490

h;16;1184705701;7891;351298

h;17;1184709301;258228;2123273

h;18;1184712901;65965;2951165

h;19;1184716501;29618;1321867

h;20;1184720101;51062;2405040

h;21;1184723701;48907;2124114

h;22;1184727302;50111;2361284

h;23;1184730901;51224;2258344

[5318 byte] By [sysadminnooba] at [2007-11-27 11:26:54]
# 1

You need to re-post your code using the [code] tags (see formatting tips link).

Also, the "Usage: " output is probably being caused by your parsing of the text not by an incorrect number of arguments in args. If you use e.printStackTrace() you might get more useful information.

jbisha at 2007-7-29 16:13:12 > top of Java-index,Java Essentials,Java Programming...
# 2

I haven't looked very hard at your code, but your test for correct usage is flawed.

You should initially check the length of the args array, to see if the program

was invoked correctly. Catching an oobe and *assuming* it was because

nothing was passed on the command line is bad logic. What if your other

array code has an error?

BigDaddyLoveHandlesa at 2007-7-29 16:13:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for the help guys. I didn't set my array to the right size. Also sorry about the formatting. I will do better next time.

sysadminnooba at 2007-7-29 16:13:12 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thanks for the help guys. I didn't set my array to

> the right size. Also sorry about the formatting. I

> will do better next time.

A list would have made that more flexible:

http://java.sun.com/docs/books/tutorial/collections/index.html

Also, it may make sense to define a class to represent one row of data, rather than just using an array for that, too.

BigDaddyLoveHandlesa at 2007-7-29 16:13:12 > top of Java-index,Java Essentials,Java Programming...