Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading txt file contents into parallel arrays

    Hi,

    I have been trying to read data from text files in java into parallel arrays but haven't had any success yet. The format of the contents on my txt file is as follows:

    (Note: the line 1, line 2 etc. is not actually in the txt file)

    _____________________________________
    Line 1: Stringwith064numbers
    Line 2: String with more than one word
    Line 3 (int): 4
    Line 4: Stringwith455numbers
    Line 5: String with more than one word again
    Line 6 (another int): 65
    _____________________________________

    It's a pattern of three lines per entry. I have three arrays (two string and one int) and I want to get the correct values into the arrays.

    I have been reading online and trying different things but can't get it functioning.


    Any guidance appreciated.


    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You pretty much need to chunk out three lines at a time, and drop them into the arrays in position 'i' for whatever iteration you are in.
    A better option would be to use a collection though; arrays require manual resizing, and then you are splitting the data up between three separate arrays. Since the data has to represent something, you simply pull in three lines at a time, construct an object that represents it, and then provide that object to a list collection. Mucho easier than three arrays.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You pretty much need to chunk out three lines at a time, and drop them into the arrays in position 'i' for whatever iteration you are in.
    A better option would be to use a collection though; arrays require manual resizing, and then you are splitting the data up between three separate arrays. Since the data has to represent something, you simply pull in three lines at a time, construct an object that represents it, and then provide that object to a list collection. Mucho easier than three arrays.
    I understand it may not be the most efficient way, but this is a college assignment and I'm required to do it this way.

  • #4
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You pretty much need to chunk out three lines at a time, and drop them into the arrays in position 'i' for whatever iteration you are in.
    A better option would be to use a collection though; arrays require manual resizing, and then you are splitting the data up between three separate arrays. Since the data has to represent something, you simply pull in three lines at a time, construct an object that represents it, and then provide that object to a list collection. Mucho easier than three arrays.
    Can you tell me what is wrong with the following code, it won't run.


    // Create File Object
    String fileName = "file.txt";
    File file = new File(fileName);

    if (file.exists())
    {
    Scanner scan = new Scanner(file);
    int counter = 0;

    while(scan.hasNext())
    {
    String code = scan.next();
    String moduleName = scan.next();
    double totalPurchase = scan.nextDouble();

    moduleCodes[counter] = code;
    moduleNames[counter] = moduleName;
    numberOfStudents[counter] = totalPurchase;

    counter++;


    }


  •  

    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
    •