Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:55:24+00:00 2026-06-12T21:55:24+00:00

I am trying to write a program that emulates a database by writing to

  • 0

I am trying to write a program that emulates a database by writing to a text file. I can read a text file full of data to use and then convert it to a byte array to store into another text file. The issue I’m having is as I’m converting from a string to a byte array I keep getting the java.lang.ArrayIndexOutOfBoundsException: 8. I hard coded values into my for loops so that it shouldn’t be an invalid index for the array but nothing seems to fix the problem. Here is my function that the error shows up in:

public void writeBucket(int bucket, String importFile, String[][] allrecords)
{

  theDisk = new FakeDisk();

  for(int z = 0; z < bucket; z++)
  {

    try
     {

      for(int j = 0; j < 7; z++)//for(int j = 0; j < allrecords[z].length; z++)
      {

        if(allrecords[z][j] == null) //this is the line where the error shows up
        {
          continue;
        }

        theDisk.writeSector(z, allrecords[z][j].getBytes());
      }
     }
     catch(Exception e)
     {
         //System.out.println(e.getMessage());//this prints the number 8 when not commented out
       continue;
     }

  }

  try
  {

    FileWriter fwrite = new FileWriter(importFile);

    fwrite.write("\n\n\n\n");
    fwrite.close();

  }
  catch (Exception e)
  {
    System.err.println("Error: " + e.getMessage());
  }

}

I put the loop in a try/catch thinking that it would still at least output the bytes to my text file and then not add any more to the file once it hits an invalid index, but that’s not the case. I’m mainly having trouble figuring out why I keep getting this error. I can print out the array no problem and everything shows up if I don’t try to write it to the text file.

Any help is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:55:26+00:00Added an answer on June 12, 2026 at 9:55 pm

    This is the problem:

    for(int j = 0; j < 7; z++)
    

    Look at the loop initialization and the loop condition, both of which use j – then look at the increment, which is changing the value of z.

    Even the commented out version is still broken, as it’s still incrementing z.

    In fact, you don’t need j at all as far as I can see. You can change your inner loop to:

    for (String text : allrecords[z])
    {
        if (text == null)
        {
            continue;
        }
        theDisk.writeSector(z, text.getBytes());
    }
    

    However, I would strongly encourage you not to call getBytes() without an encoding in the first place. If you really want the default platform encoding, specify that explicitly. If you want some other encoding such as UTF-8, specify that. If you don’t know what encoding you want, you need to take a step back and think carefully about your data.

    (It’s also slightly odd that you’re calling writeSector with the same value of z each time… isn’t that going to just overwrite the data?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that will read from a flat file of
I'm trying to write a program that outputs data that can be served over
I am trying to write a program that will create a text file and
I am trying to write a program that can replace text and replace also
im trying to write a program that reads from a text file into a
I am trying to write a program that sends basic text files from one
i am trying to write a program that close explorer then runs another program.
I'm trying to write a program that can stop and start services using SilverLight
I'm trying to write a program that takes a large file (of any type)
I'm trying to write a program that could monitor multiple folders for file creations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.