Results 1 to 14 of 14

Thread: [RESOLVED] Reading Text File Populate it into Arrays and Loops ( Please I need a help )

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Resolved [RESOLVED] Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:13 PM.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    Welcome to the forum. You have not explained at all whats going wrong. We do not just read your code and hope to work out for our selfs whats the issue. Please take the time to explin in full whats going on, what should be happening... the question should be clear with out the code. Although we always want the code.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:12 PM.

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    I can see two problems...

    You're not populating the arrays correctly, see my edit below;
    Code:
            Using MyReader As New mvb.FileIO.TextFieldParser(strFullFilePath)
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(",")
                Dim i As Integer = 0
                Do While (Not MyReader.EndOfData)
                    strReadRow = MyReader.ReadFields()
                    strReadFoodItems(i) = strReadRow(0)
                    intReadCalories(i) = strReadRow(1)
                    cboFoodItems.Items.Add(strReadFoodItems(i))
                    i += 1
                Loop
            End Using
    and in the ComboBox event you need;
    Code:
            intCountFoodItems = lstFoodItems.Items.Count
    
            If intCountFoodItems = 8 Then
                MsgBox("Full")
                Exit Sub
            End If
    
            strEntry = cboFoodItems.Text
            strReadFoodItems(intCountFoodItems) = strEntry
            intCountFoodItems = intCountFoodItems + 1
    
            lstFoodItems.Items.Add(strEntry)
            lstFoodCalories.Items.Add(intReadCalories(cboFoodItems.SelectedIndex))
    
            Dim Total As Integer
            For i = 0 To lstFoodCalories.Items.Count - 1
                Total += lstFoodCalories.Items(i)
            Next
            lblItemsAndTotalCalories.Text = Total.ToString()


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:13 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:13 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:13 PM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:13 PM.

  9. #9
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    How many items are there?

    Your array is defined as "Public strReadFoodItems(7) As String", is the "7" high enough? If you have more than 8 elements (0-7), then array item 8 (the ninth) will be deemed to not exist.

    You should also check that two elements are read per line. So...

    Code:
    If strReadRow.Count >=2 Then
        strReadFoodItems(i) = strReadRow(0)
        intReadCalories(i) = strReadRow(1)
    End If
    Last edited by Bulldog; Mar 20th, 2015 at 07:16 AM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:14 PM.

  11. #11
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    Well set the array size to something higher than you're using.

    You should also add;
    Code:
    If strReadRow.Count >=2 Then
        strReadFoodItems(i) = strReadRow(0)
        intReadCalories(i) = strReadRow(1)
    End If
    in case some lines do not have two items or you reach the end of the file.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:14 PM.

  13. #13
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    The array is made up of elements, so an array like MyArray(7) has 8 elements (0-7). To set one of those elements, you would do MyArray(0) = 5 and so on. To populate that array I made a simple loop, where i is the index which count from 0 upwards.

    i += 1 is just a quick way of writing i = i + 1, it increments i by one.

    The ComboBox has a list of items, each item has an index number from 0. The SelectedIndex property returns the index of the item currently selected. So SelectedIndex of 2, means the third item is selected.

    Total is a number, when we write it we should convert it to the type of "String". It is important to take control of the various types, like String, integer, double and so on. A good way to do that is to put Option Strict On as the first line of the Class. This forces you to resolve any code where types are mixed up.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Reading Text File Populate it into Arrays and Loops ( Please I need a help )

    ,,,,,,,
    Last edited by M7edShin; Mar 22nd, 2015 at 06:14 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
  •  



Click Here to Expand Forum to Full Width