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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:27:56+00:00 2026-06-12T16:27:56+00:00

I am having some issues at reading bytes from with readInt() when using RandomAccessFile

  • 0

I am having some issues at reading bytes from with readInt() when using RandomAccessFile
whenever I want to print the File I get EOFException. The same error appears in the last catch Statement when I want to show the updated series of numbers.
Here is my code:

import java.io.*;
import java.util.Scanner;
public class CentNumbers {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int num;
RandomAccessFile numbers = new RandomAccessFile("Numbers.dat", "rw");

System.out.println("Saved numbers are:");
numbers.setLength(0);

for (int i = 0; i < 100; i++){
  num=(int)(Math.random()*100);
  numbers.writeInt(num);
  System.out.print(numbers.readInt()+" ");
  }
try{
do{
System.out.print("\nadd another number? (y/n):");

String x= input.next();

if (!x.equals("y")) throw new Exception("Comeback Soon");


    System.out.print("Write number :");
    int b=input.nextInt();
    numbers.seek(numbers.length());
    numbers.writeInt(b);

 } while(true);

}//end try


catch ( Exception e){

    System.out.println("Saved numbers are:");

    for (int i=0; i<numbers.length();i++){
        numbers.seek(i*4);
        System.out.print(numbers.readInt()+" ");

    }//end for
    System.out.println();
    System.out.println(e.getMessage() );
    numbers.close();

    }//end catch

 }
}
  • 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-12T16:27:58+00:00Added an answer on June 12, 2026 at 4:27 pm

    In this loop, you are attempting to read from the end of the file (your readInt() comes after the writeInt(), without resetting the position).

    for (int i = 0; i < 100; i++){
      num=(int)(Math.random()*100);
      numbers.writeInt(num);
      System.out.print(numbers.readInt()+" ");
    }
    

    In this loop, the number of numbers in the file is not equal to the length of the file in bytes, because there are 4 bytes per int, therefore you are reading past the end of the file.

    for (int i=0; i<numbers.length();i++){
        numbers.seek(i*4);
        System.out.print(numbers.readInt()+" ");
    
    }//end for
    

    note for the future: when dealing with issues like this, stepping through the code in a debugger is a good way to figure out where your logic problems are. you could look at the RAF position and length after each step and you should be able to figure out what you are doing wrong.

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

Sidebar

Related Questions

I've been having some issues reading an XML file in my 'res' folder and
I'm having some issues with loading a (.properties) file from the webcontent folder in
I'm having some issues create a connection to (and reading from) a Tibco EMS
I am having some issues passing commands through to flash from javascript. My code
I'm having some issues getting file uploads to work correctly and the following code
I'm opening a CSV file and reading values from it using File.open(filename). So I
I'm having some weird issues improving a bookmarklet. I took this example from here
I'm building a monitor app and am having some threading issues. I have, using
Im reading c++ primer plus and having some issues understanding how implicit instantiation works.
I am having some issues when using mod_jk with Spring Security that has worked

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.