Results 1 to 7 of 7
  1. #1
    sandraW is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Smile enter array values to random access file and search for maximum value

    The first line of code below creates and initializes an integer array called age. The second line of code creates a stream to a random access file MyFile.dat.

    int age[]={ 21, 25, 23, 19};
    RandomAccessFile r = new RandomAccessFile("MyFile.dat","rw");

    Can anyone tell me how to use the RandonAccessFile methods writeInt( ), readInt( ), seek( ) and length( ) to enter the array values into the RandonAccessFile and then search the RandomAccessFile to find the maximum value an display this value on the command window??

    Help muchly appreciated :)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sandraW View Post
    The first line of code below creates and initializes an integer array called age. The second line of code creates a stream to a random access file MyFile.dat.

    int age[]={ 21, 25, 23, 19};
    RandomAccessFile r = new RandomAccessFile("MyFile.dat","rw");

    Can anyone tell me how to use the RandonAccessFile methods writeInt( ), readInt( ), seek( ) and length( ) to enter the array values into the RandonAccessFile and then search the RandomAccessFile to find the maximum value an display this value on the command window??

    Help muchly appreciated :)
    Wasn't the API documentation for the RandomAccessFile of any help?
    Writing those ints is easy:

    Java Code:
    for (int i= 0; i < age.length; i++)
       r.writeInt(age[i]));
    Reading them back in isn't much more difficult either after you have moved the stream pointer to its initial position: r.seek(0);

    Your turn now ...

    kind regards,

    Jos

  3. #3
    sandraW is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Thank you very much,

    I thought that might be the answer but thought it may have been just too simple!

    Do you have any idea of how to search for the maximum value in the random access file?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sandraW View Post
    Thank you very much,

    I thought that might be the answer but thought it may have been just too simple!

    Do you have any idea of how to search for the maximum value in the random access file?
    Yes I have; have you?

    kind regards,

    Jos

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    how to search for the maximum value in the random access file
    The same as anywhere else where the numbers are in random order, start at the beginning and continue until the end.

  6. #6
    sandraW is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Smile

    JosAh, should there be () after i<age.length ??

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sandraW View Post
    JosAh, should there be () after i<age.length ??
    Nope, arrays have a (read only) field named 'length'. Normally classes have a method named 'length()'.

    kind regards,

    Jos

Similar Threads

  1. random file access and GUI
    By leiferouis in forum New To Java
    Replies: 1
    Last Post: 02-19-2009, 04:21 AM
  2. Random File Access
    By viper110110 in forum New To Java
    Replies: 11
    Last Post: 11-28-2008, 12:28 AM
  3. Random access to text file
    By lunarbof in forum New To Java
    Replies: 1
    Last Post: 11-05-2008, 08:53 PM
  4. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 10:07 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
  •