Results 1 to 2 of 2
  1. #1
    BrandonMajors is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Default Reading text file into Object array and creating Random Access File

    Hi all, i am fairly new to java and still pretty basic when it comes to terms and problem solving with java programming so please bare with me. I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...

    The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data. Please take a look at my code and offer any help you can, would be greatly appreciated. thanks.

    Here is the base Product class that must be used to create the objects for the array.

    public class Product
    {
    public String pName;
    public String stringName;
    public double price;
    public int quanity;



    //Constructor
    public Product( String pName, double price, int quanity )
    {
    pName = pName;
    price = price;
    quanity = quanity;
    }

    //Getter for Product Name
    public String getPname( )
    {
    return pName;
    }
    //Getter for Price
    public double getPrice( )
    {
    return price;
    }
    //Getter for Quanity
    public int getQuanity( )
    {
    return quanity;
    }

    //Setter for product name.
    public void setPname( String name )
    {
    pName = name;
    }

    //Convert char array to string
    public String getStringName( char[] name )
    {
    stringName = new String(name);
    return stringName;
    }

    //Setter for product price.
    public void setPrice( double p )
    {
    price = p;
    }

    //Setter for quanity.
    public void setQuanity( int q )
    {
    quanity = q;
    }
    //Compares product name and price
    public boolean equals( Object other)
    {
    return true;
    }

    //ToString Method to return total amount.
    public String toString( )
    {
    return ( "Product : " + pName + " Price : " + price + " Quanity : " + quanity );
    }
    }

    Here is what i have so far for the ProductData class that will preform the operations.. i know the readText method is completely incorrect..

    import java.io.*;
    import java.util.*;

    public class ProductData
    {
    private RandomAccessFile file;
    private String textFile;
    public static final int INT_SIZE = 4;
    public static final int DOUBLE_SIZE = 8;
    public static final int NAME_LENGTH = 30;
    public static final int NAME_SIZE = 2 * NAME_LENGTH;
    public static final int RECORD_SIZE = INT_SIZE + DOUBLE_SIZE + NAME_SIZE;
    public Product[] productArray;
    public int positionProduct, size;




    //Constructor
    public ProductData( )
    {
    file = null;
    }

    //Constructor for textfile
    public ProductData( String textFile )
    {
    textFile = textFile;
    }

    //Reads String from RAF
    public String readFixedString()
    {
    return textFile;
    }

    public Product read(int n) throws IOException
    {
    file.seek(n * RECORD_SIZE);
    String pName = file.readLine();
    double price = file.readDouble();
    int quanity = file.readInt();

    return new Product(pName, price, quanity);
    }

    //Writes fixed width string to RAF
    public void writeFixedString(String s, int size)
    {
    }

    //Opens Random Acces File named filename
    public void openRandom(String filename) throws IOException
    {
    if (file != null)
    {
    file.close();
    }
    file = new RandomAccessFile(filename, "rw");
    }

    //Calculates the size of the RAF and returns the # of products in RAF.
    public int size()throws IOException
    {
    return (int)(file.length() / RECORD_SIZE);
    }

    //Opens Text file and reads data into an array of Product objects, # not known.
    public Product[] readText(String textFile)
    throws IOException, ClassNotFoundException
    {
    Product product;

    File f = new File(textFile);

    if (f.exists())
    {
    ObjectInputStream in = new ObjectInputStream
    ( new FileInputStream(f));
    product = (Product) in.readObject();
    in.close();
    }
    return productArray;
    }



    //Writes a product to the RAF
    public void write(int n, Product product) throws IOException
    {
    file.seek(n * RECORD_SIZE);
    file.writeChars(product.getPname());
    file.writeDouble(product.getPrice());
    file.writeInt(product.getQuanity());
    }

    //Accessor
    public String getFileName()
    {
    return textFile;
    }


    //Close RAF
    public void close() throws IOException
    {
    if (file != null)
    {
    file.close();
    }
    file = null;
    }

    //Finds position of product name
    public int find(java.lang.String productName)
    throws java.io.IOException
    {
    for (int i = 0; i < size(); i ++)
    {
    file.seek(i * RECORD_SIZE);
    String p = file.readLine();
    if ( p.equals(productName))
    {
    return i;
    }
    }
    return -1;
    }


    //ToString Method
    public String toString( )
    {
    return ( textFile );
    }
    }

    and then here is the data from the text file that i must extract to use to create product objects.

    Dill Seed,938,34

    Mustard Seed,100,64

    Coriander Powder,924,18

    Turmeric,836,80

    Cinnamon (Ground Korintje),951,10

    Cinnamon (Ground) Xtra Hi Oil (2x),614,31

    Cinnamon (Ground) High Oil (1X),682,19

    these continue for about 40-50 entries, they are not seperated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name seperated with spaces, then price after a comma, then quanity after the second comma.....

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Reading text file into Object array and creating Random Access File

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Creating a simple random file access program
    By bad biddy in forum New To Java
    Replies: 6
    Last Post: 04-03-2012, 12:13 AM
  2. Replies: 5
    Last Post: 11-17-2011, 04:45 PM
  3. Creating an array from reading a file
    By Reece in forum Java Software
    Replies: 1
    Last Post: 10-23-2011, 06:48 PM
  4. Replies: 6
    Last Post: 08-26-2010, 02:26 PM
  5. Random access to text file
    By lunarbof in forum New To Java
    Replies: 1
    Last Post: 11-05-2008, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •