Click here to Skip to main content
14,271,712 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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).
   
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.
OriginalGriff 3-Mar-18 5:27am
   
Start by using the debugger to find out exactly what is happening - put the cursor on the line

If item4 < 4 Then

And set a breakpoint (Menu: "Debug...Toggle breakpoint" - a red dot will appear at the beginning of the line to show the breakpoint is set - you can click on the dot to remove it, or click "where the dot should be" to add a breakpoint. Try it: you'll see what I mean).
Run your app in the debugger, and when it reaches that line, it will stop and let you take over. You can now look at the contents of your variables and see exactly what you have in them. You can also run your code line-by-line by using the "single step" function.
Give it a try, and see if what you think should be going on is what is actually happening!
faiqaa 4-Mar-18 5:02am
   
I have another question, what if I want to highlight/pinpoint the row of stock whose quantity is lower than 4? currently a message box is displayed every time the form is opened if one or more products' quantity is lower than 4, "A stock item's quantity is too low please check carefully". Thanks.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100