dcsimg
Storing FileInputStream into 2D array
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Monisha_Butler
Posted On:   Tuesday, November 12, 2002 04:47 AM

How do I read my text file into a 2d array? I have a text file that is 4 columns wide and 10000 rows long. The heading of the titles is time, x,y and z. I need these values in order to plot. I figured the best way of extracting the needed data would be to put it in a two dimensional array. However, I am uncertain how to that. I did make an attempt but when I tried to compile I received errors. My attempt is listed below. public static void main(String[] args){ //byte buff[]= new byte[10000]; String line; FileInputStream fis; BufferedInputStream bis; DataInputStream dataIn; double x,y,z,time,x1,y1,z1,time1; int i;    More>>

How do I read my text file into a 2d array?


I have a text file that is 4 columns wide and 10000 rows long. The heading of the titles is time, x,y and z. I need these values in order to plot. I figured the best way of extracting the needed data would be to put it in a two dimensional array. However, I am uncertain how to that. I did make an attempt but when I tried to compile I received errors. My attempt is listed below.

			
public static void main(String[] args){

//byte buff[]= new byte[10000];

String line;

FileInputStream fis;

BufferedInputStream bis;

DataInputStream dataIn;

double x,y,z,time,x1,y1,z1,time1;

int i;


try{

fis = new FileInputStream("trajectory.txt");

bis = new BufferedInputStream(fis);

dataIn = new DataInputStream(bis);

}

catch(FileNotFoundException e)

{

System.out.println("Error in opening file");

return;

}


line = "";


x1 = 0; y1 = 0; z1 = 0; time1 = 0;

i = 0;


try{

while((line = dataIn.readLine()) != null && line.length() >1)

{

StringTokenizer st = new StringTokenizer(line,",");

time = Double.valueOf(st.nextToken()).doubleValue();

x = Double.valueOf(st.nextToken()).doubleValue();

y = Double.valueOf(st.nextToken()).doubleValue();

z = Double.valueOf(st.nextToken()).doubleValue();


time1 += time;

x1 += x;

y1 += y;

z1 += z;


System.out.println(time1);

}

dataIn.close();

}


The errors that I get when I compile are:


java.lang.NumberFormatException: 18988.5000000000 -4188.45496811692 -5172.77359298831 -2145.06581152183


at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1171)


at java.lang.Double.valueOf(Double.java:163)


at triangleaerray.triangleclass.main (triangleclass.java:182)


Exception in thread "main" count = 0, total = 180    <<Less

Re: Storing FileInputStream into 2D array

Posted By:   Christopher_Koenigsberg  
Posted On:   Tuesday, November 12, 2002 07:47 AM

java.lang.NumberFormatException: 18988.5000000000 -4188.45496811692 -5172.77359298831 -2145.06581152183



That's not a "compile" time error, it's a runtime error, I think..... it looks like you are trying to feed a string containing 4 numbers, as one number.



I think you'll need to debug by printing out the intermediate strings, tokens etc. that you are getting, before trying to use them (what does "st.nextToken()" actually return each time you call it, etc.).

About | Sitemap | Contact