Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help reading a txt file into a datatype similar to a db

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help reading a txt file into a datatype similar to a db

    For my project I in phase 1 we cannot use a database and I am having trouble figuring out what to do. the textfiles are like

    ProductID1 ProductName1 Price1 Quantity1
    ProductID2 ProductName2 Price2 Quantity2
    ProductID3 ProductName3 Price3 Quantity3
    ProductID4 ProductName4 Price4 Quantity4
    ProductID5 ProductName5 Price5 Quantity5

    ^with actual data though

    I know how to read in the files using StringTokenizer, I just don't understand what datatype to place everything in because everything I've been working with so far that I've known java is 1D Arrays/Lists. When you have 4, 5, or 6 colums what to can I do when I don't have a DB to hold the information and I need to access it easily. I looked on google and it seemed like I should use a Map or HashMap but I still don't see any examples of how to use one when you have 4 columns of data for each row. If anyone can shield some light on what to do I'd be grateful.
    Last edited by jmc117; February 27th, 2013 at 02:15 PM. Reason: solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help reading a txt file into a datatype similar to a db

    You could define a class to hold the data and save instances of the new class in an ArrayList.

    Or you could define a two dimensional array and save the tokens from the file as Strings in the array.
    The first dim would be the row, the second dim would be the column on that row.

    How will the data be accessed? Sequentially or by a key?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help reading a txt file into a datatype similar to a db

    by key. So going with your first advice, I create a class similar to this but with getters and setters and a constructor like
    public class Product {

    public Product(int id, String name, float price, int quantity){
    //set private values here
    }

    //getters and setters here

    }

    then make my readFile from my driver return an Arraylist of instances of my Product class i assume. After that how do I iterate through my objects? I will want to search by the id for example. i need to figure out how to access my objects from an arraylist..ill google it shortly unless i get a reply. thanks.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help reading a txt file into a datatype similar to a db

    If you are accessing the data by key, you will want to use a Map instead of an ArrayList.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help reading a txt file into a datatype similar to a db

    Alright, I'll see what I can do. If I need any more help I'll come back to this thread. I appreciate it. I wasn't able to even think of the idea of an Map/List of objects until you helped me so thanks I should be able to figure it out from here.

    --- Update ---

    thank you Norm! i got it working with a HashMap and a Product class. Thanks again

Similar Threads

  1. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  2. Reading a txt file and storing them in a Java Array
    By FrozenFox in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 27th, 2012, 07:19 AM
  3. reading and witing to txt file
    By nautilusz in forum File I/O & Other I/O Streams
    Replies: 25
    Last Post: January 22nd, 2012, 02:02 PM
  4. Reading from a txt file into 3 arraylists and save it as double
    By depi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2011, 02:25 PM
  5. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM