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 8279757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:25:52+00:00 2026-06-08T09:25:52+00:00

I have created a method to write in a file, as shown in the

  • 0

I have created a method to write in a file, as shown in the code below. This method is called 1000 times from another method, with the two given parameters.

However in 5 of these calls, this method catches an error “e” and prints out: java.lang.ArrayIndexOutOfBoundsException and in my terminal I get the result of System.out.println(“”+(double)C/T), but in the file this result is missing. (the other calls work fine)

I was really confused in the beginning, as I am not working with arrays at all. After googling a little bit, I found this: http://www-01.ibm.com/support/docview.wss?uid=swg1IZ87600

I am still really confused and have no idea how to fix this. Somebody help please?

public void myMethod(int C, int T) {   
    try {
      FileOutputStream fout = new FileOutputStream ("output.txt", true );
      PrintStream ps = new PrintStream(fout);    
      System.out.println(""+(double)C/T);
      ps.println((double)C/T+"");
      // close file
      fout.close();
    }
    catch(Exception e) {
        System.err.println(e);
    }   
}  
  • 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-08T09:25:54+00:00Added an answer on June 8, 2026 at 9:25 am

    Worked perfectly for me on my Windows JVM:

    package cruft;
    
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    /**
     * ArrayIndexOutOfBoundsProblem
     * @author Michael
     * @link http://stackoverflow.com/questions/11640904/java-lang-arrayoutofbounds-exception-when-writing-to-a-file
     * @since 7/24/12 7:58 PM
     */
    public class ArrayIndexOutOfBoundsProblem {
    
        public static void main(String[] args) {
            ArrayIndexOutOfBoundsProblem aioob = new ArrayIndexOutOfBoundsProblem();
    
            int c = ((args.length > 0) ? Integer.valueOf(args[0]) : 10);
            int t = ((args.length > 1) ? Integer.valueOf(args[1]) : 2);
            aioob.myMethod(c, t);
        }
    
        public void myMethod(int C, int T){
            try{
                FileOutputStream fout = new FileOutputStream("output.txt", true );
                PrintStream ps = new PrintStream(fout);
                System.out.println(""+(double)C/T);
                ps.println((double)C/T+"");
                // close file
                fout.close();
            }
            catch(Exception e)
            {
                System.err.println(e);
            }
        }
    }
    

    I’d write it this way: conforms to the Java coding standards and uses names that are clearer:

    package cruft;
    
    import java.io.*;
    
    /**
     * ArrayIndexOutOfBoundsProblem
     * @author Michael
     * @link http://stackoverflow.com/questions/11640904/java-lang-arrayoutofbounds-exception-when-writing-to-a-file
     * @since 7/24/12 7:58 PM
     */
    public class ArrayIndexOutOfBoundsProblem {
    
        public static void main(String[] args) {
            ArrayIndexOutOfBoundsProblem aioob = new ArrayIndexOutOfBoundsProblem();
    
            int c = ((args.length > 0) ? Integer.valueOf(args[0]) : 10);
            int t = ((args.length > 1) ? Integer.valueOf(args[1]) : 2);
            aioob.printValues(c, t);
        }
    
        public void printValues(int c, int t){
            printValues(System.out, c, t);
        }
    
        public void printValues(String outputFilePath, int c, int t) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outputFilePath);
                PrintStream ps = new PrintStream(fos);
                printValues(ps, c, t);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                close(fos);
            }
        }
    
        public void printValues(PrintStream ps, int c, int t) {
            ps.println(String.format("c: %10d t: %10d ratio c/t: %10.4f", c, t, (double) c/t));
        }
    
        public static void close(OutputStream os) {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have created one method to write to Log file but each time it
I have created a method in my java assignment to write into a file
I have created a custom shipping method to show depo-names from which customers will
I have created a UserControl called AutorControl with a method to clear its textbox:
I have created a class library called AddServiceLibrary in which I have a method
I have this code that's supposed to unzipp a file. public class dec extends
I have created JAVA GUI that will receive text file(.txt) from other client and
I have Implemented Code to download a File from Internet, But I am getting
I have created a method to check if the keyboard is used in an
I have created a method that should ideally take a single Account object and

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.