Results 1 to 5 of 5
  1. #1
    donkatec is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Arrays and File Input

    Hello, all. I've been working on a solution to a problem that I hope you may help me solve. I need to get data from a simple text file and most likely store the data in parallel arrays. Unfortunately, I don't have experience with the two concepts I'm supposed to use: accessing files and tokenizing strings.

    The data file will have data like Name/18/M/8 on each line.

    I will need to display all the information, and perform boolean expressions on the numbers.

    I was thinking of creating parallel arrays, storing all the names in one array, the second token (age) in another, etc....

    Using the concept of tokens, I think I can first create arrays of strings, then create separate arrays.

    Is this a good solution?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The easiest way to read data from the file is to use the Scanner class. Once you read a line of data you can use String.split to break the line down into the individual data items.

    DO NOT use parallel arrays. They are the spawn of Satan. Write a class to encapsulate the data.

  3. #3
    donkatec is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The easiest way to read data from the file is to use the Scanner class. Once you read a line of data you can use String.split to break the line down into the individual data items.

    DO NOT use parallel arrays. They are the spawn of Satan. Write a class to encapsulate the data.
    Thanks, Junky. I've been reading your posts. They make me crack up. I like how most of you try to be patient with people begging for code, because they don't want to think. :)

    Anyway, I was thinking of the split (another concept I'm learning on my own) but how can I keep all the data of each line from the text file connected? If they are kept as Strings, that's fine, but the hint given was that I
    might have to use parseInt to treat the two numbers as integers. For example, for every record where someone is under 18, I'd need to display their names.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    As I said you use a class to encapsulate the data (keep it together). Yes you need to Use Integer.parseInt to convert a String to an int. You can either do this before creating an object or pass or the info as Strings to the object and then convert to an int in the constructor.

    Then what you need to do is keep all the objects together. Most experience coders would use an ArrayList but if this is to advanced then just use an array. Once all you data has been read, object created and inserted into an array you would iterate over the array and (with the use of methods of your class) find the objects with an age under 18 and print the name.

    This may seem like a lot to understand and cope with but break it down into smaller steps. First thing try reading all the data from the file and print it to the screen. Don't modify it in anyway. Once this is working move onto the next step. etc.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. Replies: 4
    Last Post: 03-09-2011, 05:54 AM
  2. Filling arrays from an input file?
    By hiei_yasha in forum New To Java
    Replies: 1
    Last Post: 02-26-2011, 05:01 AM
  3. retain value of input type file in a jsp file while being dynamically generated
    By nidhi c in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:21 AM
  4. Input data from file to arrays problem
    By PVL268 in forum New To Java
    Replies: 18
    Last Post: 03-16-2009, 05:05 AM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM

Posting Permissions

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