Results 1 to 12 of 12
- 06-16-2010, 08:30 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
[Solved!] Writing and reading to/from a 2D array!
Greetings! Basically, I'm having troubles writing integer data into a 2D array. I'm also not sure if I'm writing it correctly to, though it seems right.
Now to the examples:
This is how I write to 1D array:
That's no problem...Java Code:public static void write () { try { DataOutput d = new DataOutputStream (new FileOutputStream ("save.txt")); DataOutput f = new DataOutputStream (new FileOutputStream ("alreadySaved.txt")); for (int i = 0 ; i < 55 ; i++) { d.writeInt (userBooksRated [i]); f.writeBoolean (true); } catch (Exception e) { String msg = e.toString (); System.out.println (msg); } c.print ("Saved"); System.exit (-0); }
Next, I write a 2D array, declared in the beginning of the program, to a file. I do that so I can create a database file to use in the future.
Here's how I do it:
and finally, on the next program launch, I want to write the integers from the database file into the array. This is what I do:Java Code:public static void write () { try { DataOutput g = new DataOutputStream (new FileOutputStream ("database.dat")); for (int i = 0 ; i < storeRatings.length ; i++) { for (int j = 0 ; j < storeRatings [i].length ; j++) { g.writeInt (storeRatings [i] [j]); } } } catch (Exception e) { String msg = e.toString (); System.out.println (msg); } c.print ("Saved"); System.exit (-0); }
Finally, when one part of the program tries to use the array I get an error. :(Java Code:public static void readDatabase () { try { DataInput b = new DataInputStream (new FileInputStream ("database.dat")); for (int i = 0 ; i < storeRatings.length ; i++) { for (int j = 0 ; j < storeRatings [i].length ; j++) { storeRatings [i] [j] = b.readInt (); } } } catch (Exception e) { String msg = e.toString (); System.out.println (msg); } }
java.lang.ArrayIndexOutOfBoundsException: 55
at BookISP.calculate(BookISP.java:238)
at BookISP.getRatings(BookISP.java:230)
at BookISP.menu(BookISP.java:423)
at BookISP.main(BookISP.java:64)
and here's the part of the code that uses the 2D array:
My question is - what am I doing wrong? Am I writing it to the file wrong? Am I reading it from the file wrong?Java Code:public static void calculate () { for (int i = 0 ; i < 86 ; i++) { matchBooks [i] = userBooksRated [0] * storeRatings [i] [0]; for (int j = 1 ; j < 55 ; j++) { matchBooks [i] += userBooksRated [j] * storeRatings [i] [j]; } } c.close (); c = new Console (); findSimilar (); }
Thanks for reading!Last edited by celebor; 06-17-2010 at 01:44 PM.
- 06-16-2010, 09:29 AM #2
What are the dimensions of your arrays....?? write all of them
- 06-16-2010, 11:23 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
This is pretty suspicious:
Why the hardcoded limits for i and j? I'd suggest limiting i and j with the .length field of your arrays, like you did in your previous methods.Java Code:public static void calculate () { for (int i = 0 ; i < 86 ; i++) { matchBooks [i] = userBooksRated [0] * storeRatings [i] [0]; for (int j = 1 ; j < 55 ; j++) { matchBooks [i] += userBooksRated [j] * storeRatings [i] [j]; } } c.close (); c = new Console (); findSimilar (); }Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-16-2010, 03:23 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
suspicious? lol...
You see, this 2d array that I'm trying to write and read - it was hardcoded before. And it's pretty damn huge. That's why I want to read and write it from a file. The calculations worked fine before, so I doubt the problem lies in there.
- 06-16-2010, 03:31 PM #5
your exception tells that the problem lies in the index used to access the array element.... so write the array dimensions here or please double-check the dimensions of the arrays you have created.
http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-16-2010, 04:00 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
Here is how I declared it:
public static int storeRatings[] [] = new int [55] [86];
Then:
Then:Java Code:public static void readDatabase () { try { DataInput b = new DataInputStream (new FileInputStream ("database.dat")); for (int i = 0 ; i < storeRatings.length ; i++) { for (int j = 0 ; j < storeRatings [i].length ; j++) { storeRatings [i] [j] = b.readInt (); } } }
Edit:Java Code:public static void main (String[] args) { c = new Console (); books.getBooks (); books.printBooks (); readDatabase (); //Calling for it here, before the calculations read (); if (alreadySaved == false) { for (int i = 0 ; i < 55 ; i++) { alreadyRated [i] = false; alreadyPrinted [i] = false; userBooksRated [i] = 0; getRatings (); } } else menu (); }
Just so you know, there are 86 users and each provide 55 ratings. 1 rating per book and 0 means they haven't rated it.
Oh and also, I changed the method I used to write the array into the file. [it's a one time run piece of code]
Java Code:for (int i = 0 ; i < storeRatings.length ; i++) { for (int j = 0 ; j < storeRatings [i].length ; j++) { g.writeInt (storeRatings [i] [j]); } }Last edited by celebor; 06-16-2010 at 04:12 PM.
- 06-16-2010, 04:06 PM #7
Here is your problem..... you declared it [55] [86] and below accessing it as [86] [55]Here is how I declared it:
public static int storeRatings[] [] = new int [55] [86];
Just correct this and I hope it will be solved. I would recommend using size() and length fields instead of hardcoded limits.and here's the part of the code that uses the 2D array:
Code:
public static void calculate ()
{
for (int i = 0 ; i < 86 ; i++)
{
matchBooks [i] = userBooksRated [0] * storeRatings [i] [0];
for (int j = 1 ; j < 55 ; j++)
{
matchBooks [i] += userBooksRated [j] * storeRatings [i] [j];
}
}
c.close ();
c = new Console ();
findSimilar ();
}
My question is - what am I doing wrong? Am I writing it to the file wrong? Am I reading it from the file wrong?
Hope that helps.http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-16-2010, 04:14 PM #8
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
OMG!! Lmao, dumb me, dumb me! Thank you so much for pointing that out!! It seems to be working now!!
- 06-17-2010, 10:39 AM #9
celebor,
Is this thread solved?http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-17-2010, 11:21 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
This is exactely why I said that the hardcoded part was suspicious. Always use the arrays .length field for loops iterating through an array, this way you avoid errors in logic like this.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-17-2010, 01:45 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
@tyroceur
Yep, it's solved!
@m00nchile
Hehe, yah :]
- 06-17-2010, 01:48 PM #12
So please mark it solved by selecting 'Mark Thread as Solved' from 'Thread Tools' on top of the thread.
Thanks :)http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
Similar Threads
-
Reading and Writing to XML - Help Please!
By JonnySnip3r in forum New To JavaReplies: 4Last Post: 01-17-2010, 10:55 PM -
Reading and writing to a file
By jigglywiggly in forum New To JavaReplies: 13Last Post: 03-09-2009, 10:44 AM -
Reading/Writing to file
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-28-2008, 02:05 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks