Results 1 to 12 of 12
  1. #1
    celebor is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Thumbs up [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:
    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);
        }
    That's no problem...

    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:
    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);
        }
    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 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);
            }
        }
    Finally, when one part of the program tries to use the array I get an error. :(
    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:
    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 ();
    
        }
    My question is - what am I doing wrong? Am I writing it to the file wrong? Am I reading it from the file wrong?

    Thanks for reading!
    Last edited by celebor; 06-17-2010 at 02:44 PM.

  2. #2
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    What are the dimensions of your arrays....?? write all of them

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    This is pretty suspicious:
    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 ();
    
        }
    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    celebor is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    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.

  5. #5
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    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.
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  6. #6
    celebor is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Here is how I declared it:
    public static int storeRatings[] [] = new int [55] [86];
    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 ();
                    }
                }
            }
    Then:
    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 ();
    
        }
    Edit:

    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 05:12 PM.

  7. #7
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Here is how I declared it:
    public static int storeRatings[] [] = new int [55] [86];
    Here is your problem..... you declared it [55] [86] and below accessing it as [86] [55]

    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?
    Just correct this and I hope it will be solved. I would recommend using size() and length fields instead of hardcoded limits.

    Hope that helps.
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  8. #8
    celebor is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    OMG!! Lmao, dumb me, dumb me! Thank you so much for pointing that out!! It seems to be working now!!

  9. #9
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    celebor,
    Is this thread solved?
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  10. #10
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  11. #11
    celebor is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    @tyroceur
    Yep, it's solved!

    @m00nchile
    Hehe, yah :]

  12. #12
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    So please mark it solved by selecting 'Mark Thread as Solved' from 'Thread Tools' on top of the thread.

    Thanks :)
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

Similar Threads

  1. Reading and Writing to XML - Help Please!
    By JonnySnip3r in forum New To Java
    Replies: 4
    Last Post: 01-17-2010, 11:55 PM
  2. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 11:44 AM
  3. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 03:05 PM
  4. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11:41 AM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •