Click here to Skip to main content
15,886,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to display a message box based on the quantity regarding certain products saved in rows of data (4th item) in a text file, I know how to read each row but not how to only read a specific item.Does anyone have any idea? Let me know,thanks in advance.
Below you can see what code I'm using to display all rows of data.

What I have tried:

Dim STUDENT_FILE As String = ("C:\Users\Windows 7 User\Desktop\Stock.txt")
       Dim objReader As New System.IO.StreamReader(STUDENT_FILE)

       Dim strDataline As String 'Data line
       Dim strArr(2) As String ' Array for bits of line
       Dim blfound As Boolean
       ListBox1.Items.Clear()

       Do While objReader.Peek() <> -1 'read the file till the end.
           strDataline = (objReader.ReadLine()) ' read line and store into variable
           strArr = strDataline.Split(",") 'Split line and put into array

           ListBox1.Items.Add(strDataline)
           blfound = True

       Loop
       MsgBox("Stock data has been loaded")
       objReader.Close()
       If blfound = False Then MsgBox("stock data has not been found")
Posted
Updated 2-Mar-18 20:36pm

1 solution

You can't read "a specific item" from a text file, because it doesn't have any implicit order - in fact even lines are just an interpretation of characters within the file: each time a newline character (or character pair, depending on the operating system) is encountered, that is the end of the previous line.

Your code assumes that the data is in lines, with data separated by commas (this is called CSV or "Commas separated value" data) and the only way to get "the fourth item from each row" is literally to read each line - which you do - and use Split - which you do - to break it into items. Once split, you can access each rows items via an array index into strArr.

Personally? I'd use a CSVReader: A Fast CSV Reader[^] which will process teh data better than a "brute force" technicque like you use (as it supports text strings which may contain commas).
 
Share this answer
 
Comments
faiqaa 3-Mar-18 2:51am    
Thank you for your time, can you please give me an example of accessing each rows' items via an array index?
OriginalGriff 3-Mar-18 4:21am    
You are kidding, right?
You want me to show you how to do this:
Dim item1 As String = strArr(0)
Dim item2 As String = strArr(1)
...

Or am I missing something?
faiqaa 3-Mar-18 4:52am    
Hi, the thing is I tried doing that but it still didn't work so I thought I was doing something wrong. Sorry if I offended you by asking the dumb question.
OriginalGriff 3-Mar-18 5:02am    
No offended, but ...
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So if you don't tell us something, we have nothing to go on! (And you be surprised how many people do want us to write code like that for them... :sigh: )

So what does "it still didn't work" mean? Your computer exploded? The cat ran away from home? You got the wrong values? What did you expect, what input did you provide, what results did you get? Tell us what happened, and show us what code you used to get that result - or we are just left guessing! :laugh:
faiqaa 3-Mar-18 5:03am    
This is what I tried using, by the way I'm a new coder that's the reason why I'm not good at this.
Private Sub frm7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim STUDENT_FILE As String = ("C:\Users\Windows 7 User\Desktop\Stock.txt")
Dim objReader As New System.IO.StreamReader(STUDENT_FILE)

Dim strDataline As String 'Data line
Dim strArr(2) As String ' Array for bits of line
Dim blfound As Boolean
ListBox1.Items.Clear()

Do While objReader.Peek() <> -1 'read the file till the end.

Loop
strDataline = (objReader.ReadLine()) ' read line and store into variable


strArr = strDataline.Split(",") 'Split line and put into array

Dim item1 As Integer = strArr(0)
Dim item2 As String = strArr(1)
Dim item3 As Integer = strArr(2)
Dim item4 As Integer = strArr(3)
If item4 < 4 Then
MessageBox.Show("Quantity of stock is low, please check")
End If

Basically when I load the form the data is displayed and literally nothing happens instead it should display the msgbox. I saved on purpose a stock item's quantity as 3.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900