Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 2nd, 2006, 12:12 AM   #11
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Here's all of it. The error occurs on the same line as the above code.

Edit* now in red

class FileRead
{
    int xPieceSize, yPieceSize, xGridSize, yGridSize;
    int totalPieces;
    int fileCount;
    Piece tetPiece [];
    String fileData [];
    public FileRead ()

    {
        int fileCount = 0;
        int xPieceSize = 0, yPieceSize = 0, xGridSize = 0, yGridSize = 0;
        int totalPieces = 0;  // we must search through the file to know how many pieces are in the file.
        //String fileData [] = new String []; // we use the size of the file to make an array of the specified size

        //int PieceData[][][] = new int [totalPieces][xPieceSize][yPieceSize];
        String str = "";

        try
        {
            String temp = "";
            BufferedReader in = new BufferedReader (new FileReader ("PieceData1.txt"));

            // gets size of file -> so that we know how big to make the array that holds the file
            while ((temp = in.readLine ()) != null)
            {
                fileCount = fileCount + 1;
            }
            in.close ();

            System.out.print ("Size of file is: " + fileCount);
            // size of file has been stored


            // we use the size of the file to make an array of the specified to size of the file
            String fileData [] = new String [fileCount];
            in = new BufferedReader (new FileReader ("PieceData1.txt"));
            for (int i = 0 ; i < fileCount ; i++) //
            {
                fileData [i] = in.readLine ();
            }
            in.close ();
        }

        catch (IOException e)
        {
        }

    }



    void updateGridSize ()
    {
        int tempLength;
        int position = 0;
        int marker = -1; // a negative value so we know it's not anywhere in the file by default

        SearchForGrid: // a label
        for (int m = 0 ; m < fileCount ; m++)
        {
            tempLength = fileData [m].length ();

            position = fileData [m].indexOf ("<GridSize>");

            if (position >= 0) // if strings not found, returns negative. Thus, if found it's (> 0)
            {
                marker = m; // holds place where grid is found
                m++; // we have nothing else to do on this line
                break SearchForGrid; // done looking for grid
            }

        }
        String tempString = new String ("");
        boolean blnXFound = false, blnYFound = false;

        SearchGridSize:
        for (int i = marker ; i < fileCount ; i++)
        {

            System.out.println ("position: " + fileData [i].indexOf ("<GPieceSize>"));
            position = fileData [i].indexOf ("<GPieceSize>");
            // if we passed onto the other tag, exit
            if (position >= 0)
            {
                System.out.println ("Check your file (PieceData.txt), Somethings not right in there. Quit right now.");
                break SearchGridSize;
            }

            position = fileData [i].indexOf ("x=");

            if (position >= 0)
            {
                tempLength = fileData [i].length (); // takes lenght so we know where string ends
                for (int z = 2 ; z < tempLength ; z++)
                {
                    tempString = tempString + fileData [i].charAt (z);
                }
                xGridSize = Integer.parseInt (tempString);
                System.out.println (" " + xGridSize);
                blnXFound = true;
            }

            position = fileData [i].indexOf ("y=");

            if (position >= 0)
            {
                tempLength = fileData [i].length (); // takes lenght so we know where the string ends

                for (int z = 2 ; z < tempLength ; z++)
                {
                    tempString = tempString + fileData [i].charAt (z); // adds the remaining digits together
                }
                yGridSize = Integer.parseInt (tempString);
                System.out.println (" " + yGridSize);
                blnYFound = true;
            }

            if (blnXFound == true && blnYFound == true)
            {
                break SearchGridSize;
            }

        }

    }
}
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 2nd, 2006, 12:39 AM   #12
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 903
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Ah.

String temp = "";
            BufferedReader in = new BufferedReader (new FileReader ("PieceData1.txt"));

            while ((temp = in.readLine ()) != null)
            {
                fileCount = fileCount + 1;
            }
            in.close ();

            System.out.print ("Size of file is: " + fileCount);
           // size of file has been stored


            // use the size of the file to make an array of the specified to size of the file
            String fileData [] = new String [fileCount];
            in = new BufferedReader (new FileReader ("PieceData1.txt"));
            for (int i = 0 ; i < fileCount ; i++) //
            {
                fileData [i] = in.readLine ();
            }
            in.close ();
The underlined statement in the above code redefines fileData, hiding the class' instance of fileData. It is destroyed at the end of the try block. You should replace the underlined code with fileData = new String [fileCount];.

Now that that's resolved, it seems you have a few other problems to deal with (ArrayIndexOutOfBounds exceptions).
titaniumdecoy is offline   Reply With Quote
Old Jun 2nd, 2006, 2:20 PM   #13
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
marker = -1 ;
...

for ( int i =  marker; ...
     System.out.println ("position: " + fileData [i].indexOf ("<GPieceSize>"));
...

Did you get everything worked out?
Harakim is offline   Reply With Quote
Old Jun 2nd, 2006, 2:32 PM   #14
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
thanks for your help.

I'm at school right now so the codes not with me. I'll be back home in a few hours and i'll be able to get back to you on this.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 2nd, 2006, 5:45 PM   #15
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Yep thx.. I'm at home and it works now thanks. Now for the ArrayIndexOutOfBounds exceptions I did "int fileCount = 0" in the main method which created another variable (with the same name) which is different from the private data used in the class. That was an obvious mistake which I easily found on my own.

Thanks a lot for your help. Your advice helped a lot!
Quote:
The underlined statement in the above code redefines fileData, hiding the class' instance of fileData. It is destroyed at the end of the try block. You should replace the underlined code with fileData = new String [fileCount];.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC