-
Mar 19th, 2015, 04:25 PM
#1
Thread Starter
New Member
[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.
-
Mar 19th, 2015, 05:52 PM
#2
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.
-
Mar 19th, 2015, 05:54 PM
#3
Thread Starter
New Member
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.
-
Mar 19th, 2015, 06:07 PM
#4
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.
-
Mar 19th, 2015, 06:14 PM
#5
Thread Starter
New Member
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.
-
Mar 19th, 2015, 06:16 PM
#6
Thread Starter
New Member
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.
-
Mar 19th, 2015, 06:25 PM
#7
Thread Starter
New Member
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.
-
Mar 20th, 2015, 06:55 AM
#8
Thread Starter
New Member
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.
-
Mar 20th, 2015, 07:11 AM
#9
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.
-
Mar 20th, 2015, 07:25 AM
#10
Thread Starter
New Member
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.
-
Mar 20th, 2015, 07:41 AM
#11
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.
-
Mar 20th, 2015, 07:56 AM
#12
Thread Starter
New Member
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.
-
Mar 20th, 2015, 09:16 AM
#13
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.
-
Mar 20th, 2015, 09:23 AM
#14
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|