-
Apr 24th, 2018, 06:57 AM
#1
Thread Starter
Junior Member
Help please!!
Hello all, I am new to posting on this site and have a problem with a piece of code. The problem is I have to write a program which will read from a csv into multiple arrays, count occurrences of a certain letter in the csv, add up a total cost and then write to another csv file. So far I have a form application, 2 buttons and a listbox, if i post the code could someone help please. Thanks, Brian.
-
Apr 24th, 2018, 07:05 AM
#2
Thread Starter
Junior Member
Re: Help please!!
This is the code I have so far
Code:
Public Class Form
'This Button is the main button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Declaring Variables
Dim customerid(499) As String
Dim ticketid(499) As String
Dim numberoftickets(499) As Integer
Dim method(499) As String
'Sub Routines
'Sub Routine to read data From the document
readdata(customerid, ticketid, numberoftickets, method)
'Sub Routine To Calculate
calculations(customerid, ticketid, numberoftickets, method)
'Sub Routine To Write Data To Document
writedata(customerid, ticketid, numberoftickets, method)
End Sub
'This Sub Contains The Code That Reads The Data
Sub readdata(ByRef customerid, ByRef ticketid, ByRef numberoftickets, ByRef method)
'Declaring Local Variables
Dim newsentence, filename As String
Dim x As Integer
Dim Temp(3) As String
'Telling the computer where to find the data
filename = "\\gs236svr001\Pupils\147452PK\COMPUTING\Course Work\Choral Shield\Testdata\Coral 3.csv"
'the computer reading the lines of the csv file and putting them into arrays
Dim objtextfile As New System.IO.StreamReader(filename)
For x = 0 To 499
newsentence = objtextfile.ReadLine()
'Splits Data At Delimeter
Temp = newsentence.Split(","c)
customerid(x) = Temp(0)
ticketid(x) = Temp(1)
numberoftickets(x) = Temp(2)
method(x) = Temp(3)
Next
'Closing and Disposing the file
objtextfile.Close()
objtextfile.Dispose()
End Sub
'This Sub Contains The Code That Calculates The Data
Sub calculations(ByRef customerid, ByRef ticketid, ByRef numberoftickets, ByRef method)
'Declaring Local Variables
Dim x, popular, total As Integer
Dim msg As String
'Making the counter so that if people buy the tickets online then it will add one to the counter a.k.a popular
For x = 0 To 499
If method(x) = "O" Then
popular = popular + 1
End If
Next
'If the counter popular is over 250 sales then thats more than half the tickets so the best sale method would be Online else School
If popular > 250 Then
msg = "The overall favorite method of purchase was Online"
Else
msg = "The overall favorite method of purchase was School"
End If
'Clearing the total
total = 0
'Pricing the nights Friday = £10 Rest = £5
For x = 0 To 499
If ticketid(x).substring(0, 1) = "F" Then
total = total + numberoftickets(x) * 10
Else
total = total + numberoftickets(x) * 5
End If
Next
ListBox1.Items.Add("Essel Shield " & DateTime.Now.Year)
ListBox1.Items.Add(msg)
ListBox1.Items.Add("£" & total)
End Sub
'This Sub Contains The Code That Writes The Data
Sub writedata(ByRef customerid, ByRef ticketid, ByRef numberoftickets, ByRef method)
'Declaring Local Variables
Dim x As Integer
Dim newsentence, filename As String
'Telling The Computer Where To
filename = "\\gs236svr001\Pupils\147452PK\COMPUTING\Course Work\Choral Shield\Testdata\Friday.csv"
Dim objtextfile As New System.IO.StreamWriter(filename)
For x = 0 To 499
If ticketid(x).Substring(0, 1) = "F" Then
newsentence = customerid(x) & "," & ticketid(x) & "," & numberoftickets(x) & "," & method(x)
objtextfile.WriteLine(newsentence)
End If
Next
'Closing The Open File
objtextfile.Close()
End Sub
-
Apr 24th, 2018, 07:09 AM
#3
Thread Starter
Junior Member
-
Apr 24th, 2018, 07:13 AM
#4
Re: Help please!!
Originally Posted by Brian Frost
How do I post code?
Your first attempt was right... unfortunately our anti-spam measures can sometimes mean that posts from new members don't show up immediately, so a moderator needs to approve your post before it can be seen.
It's there now
-
Apr 24th, 2018, 07:14 AM
#5
Thread Starter
Junior Member
-
Apr 24th, 2018, 07:21 AM
#6
Re: Help please!!
Welcome to the forums.
One major pointer: we don't do homework. That doesn't mean we won't help, it just means we won't hand you the answers on a plate. Rather, we'll point you in the right direction so you can do it for yourself.
So the code looks OK at a glance but I'm sure it's not or you wouldn't be posting here. What's wrong? What are you seeing when you run it? What were you expecting to see?
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 07:22 AM
#7
Re: Help please!!
It terms of the assistance you're after, you seem to have quite a bit of code already and know what you want it to do.. what is the specific issue you are having at the moment? (eg: is there an error on a particular line, is there a piece of functionality you don't know how to code, etc)
-
Apr 24th, 2018, 07:37 AM
#8
Thread Starter
Junior Member
Re: Help please!!
This
Code:
Temp = newsentence.Split(","c)
is returning a 'Object reference not set to an instance of an object.' I have no idea what that means or how to fix it.
-
Apr 24th, 2018, 07:40 AM
#9
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
Welcome to the forums.
One major pointer: we don't do homework. That doesn't mean we won't help, it just means we won't hand you the answers on a plate. Rather, we'll point you in the right direction so you can do it for yourself.
So the code looks OK at a glance but I'm sure it's not or you wouldn't be posting here. What's wrong? What are you seeing when you run it? What were you expecting to see?
Thanks, I look forward to learning about more
-
Apr 24th, 2018, 07:59 AM
#10
Re: Help please!!
is returning a 'Object reference not set to an instance of an object.' I have no idea what that means or how to fix it
That means exactly what it says, it's just using techy speak which might be unfamiliar at first. I'm going to assume you know what an Object is, if not post back and we'll explain. An "Object Reference" is a reference (or "variable") used to address some sort of Object. that message is telling you that you're variable doesn't actually have an object on the end of it - it's pointing to nothing.
That message occurs when you try and carry out some operation against the object you're expecting to be on the end of the reference. The operation can't be carried out because there is no object, so the message pops up.
In your case you're trying to call the Split operation on the newsentence object. So it must mean that newsentence doesn't exist. You can confirm this by typing ?NewSentence into the immediate window in your ide.
So your next task is to work out why new sentence doesn't exist. Hopefully you know how to step through code in your ide (if not, post back and we'll explain how) so you need to step through the code checking the variables and watching what happens. I'm being a bit cruel here because that might mean you have to step through the loop 499 times - though I suspect you might find you get the error slightly before then
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 07:59 AM
#11
Re: Help please!!
Originally Posted by Brian Frost
This
Code:
Temp = newsentence.Split(","c)
is returning a 'Object reference not set to an instance of an object.' I have no idea what that means or how to fix it.
An Object is essentially a variable, and often contains multiple values (but doesn't have to). You can spot the object in your code because it is followed by a dot (so in this case it is newsentence).
What the message means is that the object doesn't actually have a value.
If we look at the loop that the line of code is in:
Code:
For x = 0 To 499
newsentence = objtextfile.ReadLine()
'Splits Data At Delimeter
Temp = newsentence.Split(","c)
customerid(x) = Temp(0)
ticketid(x) = Temp(1)
numberoftickets(x) = Temp(2)
method(x) = Temp(3)
Next
... we can see that you do assign a value to newsentence, but it comes from reading a file. I suspect that for some reason there is a blank line in the file (or possibly that you are reading past the end of the file).
There are a variety of potential fixes, one is simply to add an If block so that you only work with lines that have a value, eg:
Code:
For x = 0 To 499
newsentence = objtextfile.ReadLine()
If Not String.IsNullOrEmpty(newsentence) Then
'Splits Data At Delimeter
Temp = newsentence.Split(","c)
customerid(x) = Temp(0)
ticketid(x) = Temp(1)
numberoftickets(x) = Temp(2)
method(x) = Temp(3)
End If
Next
-
Apr 24th, 2018, 08:12 AM
#12
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by si_the_geek
An Object is essentially a variable, and often contains multiple values (but doesn't have to). You can spot the object in your code because it is followed by a dot (so in this case it is newsentence).
What the message means is that the object doesn't actually have a value.
If we look at the loop that the line of code is in:
Code:
For x = 0 To 499
newsentence = objtextfile.ReadLine()
'Splits Data At Delimeter
Temp = newsentence.Split(","c)
customerid(x) = Temp(0)
ticketid(x) = Temp(1)
numberoftickets(x) = Temp(2)
method(x) = Temp(3)
Next
... we can see that you do assign a value to newsentence, but it comes from reading a file. I suspect that for some reason there is a blank line in the file (or possibly that you are reading past the end of the file).
There are a variety of potential fixes, one is simply to add an If block so that you only work with lines that have a value, eg:
Code:
For x = 0 To 499
newsentence = objtextfile.ReadLine()
If Not String.IsNullOrEmpty(newsentence) Then
'Splits Data At Delimeter
Temp = newsentence.Split(","c)
customerid(x) = Temp(0)
ticketid(x) = Temp(1)
numberoftickets(x) = Temp(2)
method(x) = Temp(3)
End If
Next
That seemed to get past that error, however another error has been thrown up 'Object variable or With block variable not set.' on this line
Code:
If ticketid(x).substring(0, 1) = "F" Then
I should add that this is in the calculations sub.
Last edited by Brian Frost; Apr 24th, 2018 at 08:14 AM.
Reason: Made a mistake
-
Apr 24th, 2018, 08:17 AM
#13
Re: Help please!!
Well you know what the error means now, right? ticketid(x) must be nothing. Why would that be? In particular think about the change you made to the code.
Just something to consider, all your loops iterate 500 times (from 0 to 499). Why 500? Why not 10? or 1000? I suspect the answer to all your problems may be answered if you really consider that question.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 08:19 AM
#14
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
Well you know what the error means now, right? ticketid(x) must be nothing. Why would that be? In particular think about the change you made to the code.
Just something to consider, all your loops iterate 500 times (from 0 to 499). Why 500? Why not 10? or 1000? I suspect the answer to all your problems may be answered if you really consider that question.
The csv file I was reading in has over 300 entries over 4 columns. should I adjust the loop to repeat less?
-
Apr 24th, 2018, 08:27 AM
#15
Re: Help please!!
should I adjust the loop to repeat less?
Bingo You want to iterate until there are no more lines to read. If you carry on iterating after that objtextfile.ReadLine() won't return anything... and your troubles begin.
Have you been shown While loops yet? Because your loop in ReadData really wants to be a while loop rather than a For loop.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 08:30 AM
#16
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
Bingo You want to iterate until there are no more lines to read. If you carry on iterating after that objtextfile.ReadLine() won't return anything... and your troubles begin.
Have you been shown While loops yet? Because your loop in ReadData really wants to be a while loop rather than a For loop.
There are 300 lines to read, I have adjusted to 300 but still have the same error? However the CSV file when I scroll down has 1000s.
Last edited by Brian Frost; Apr 24th, 2018 at 08:31 AM.
Reason: mistake
-
Apr 24th, 2018, 08:41 AM
#17
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 08:42 AM
#18
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
Are there blank lines?
Yes, it literally goes on for ever when i scroll down, all blank lines.
-
Apr 24th, 2018, 08:52 AM
#19
Re: Help please!!
OK. I'm guessing you're opening it with excel. In which case those probably aren't blank lines. Excel will show you the content of the file and then it will "add" an inifinite number of blank lines on the end. They don't really exist.
So your problem is probably that there aren't exactly 300 lines in the file. Maybe your accidentally counting a header row, or you've set the to 300 in which case you're actually doing 301 loops (because your first loop is zero).
It doesn't really matter though because there's a much better way of deciding when to stop reading from the file. Have you been shown how to do a while loop yet?
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 08:53 AM
#20
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
OK. I'm guessing you're opening it with excel. In which case those probably aren't blank lines. Excel will show you the content of the file and then it will "add" an inifinite number of blank lines on the end. They don't really exist.
So your problem is probably that there aren't exactly 300 lines in the file. Maybe your accidentally counting a header row, or you've set the to 300 in which case you're actually doing 301 loops (because your first loop is zero).
It doesn't really matter though because there's a much better way of deciding when to stop reading from the file. Have you been shown how to do a while loop yet?
No I haven't, could you show me please?
-
Apr 24th, 2018, 09:10 AM
#21
Re: Help please!!
OK. Here's a page that demonstrates the different kinds of loops. You want either a Do While or a While because these let you specify a condition to determine when to exit. In your case the condition will be "stop reading when there are no more lines to read". The ReadLine method your using will return a null when there are no more lines to read so that's what you check for. Have a go at replacing your For loop with a Do While or a While and see how you get on.
Tip, if you choose a while loop you're going to gave to think about how to get it to enter the loop the first time (you need to read the first line before entering the loop), if you choose a Do While loop you'll need to think about how to prevent it from process the last, non-existent, line (Si's suggestion from post 11 will take care of that).
NB, this isn't going to resolve the second error you got - but we'll come to that.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 09:15 AM
#22
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
OK. Here's a page that demonstrates the different kinds of loops. You want either a Do While or a While because these let you specify a condition to determine when to exit. In your case the condition will be "stop reading when there are no more lines to read". The ReadLine method your using will return a null when there are no more lines to read so that's what you check for. Have a go at replacing your For loop with a Do While or a While and see how you get on.
Tip, if you choose a while loop you're going to gave to think about how to get it to enter the loop the first time (you need to read the first line before entering the loop), if you choose a Do While loop you'll need to think about how to prevent it from process the last, non-existent, line (Si's suggestion from post 11 will take care of that).
NB, this isn't going to resolve the second error you got - but we'll come to that.
OK, so should I set the Do Loop Until to have the condition x<= 299, I'm really stuck with this.
-
Apr 24th, 2018, 09:21 AM
#23
Re: Help please!!
OK, so should I set the Do Loop Until to have the condition x<= 299
No, you should set the condition to "there are no more lines to read". When there are no more lines to read this:-
Code:
newsentence = objtextfile.ReadLine()
will set newsentence to null. So you're condition would be There's a better way of checking for Null, though, which Si gave in his previous post. Really you should use this:-
Code:
String.IsNullOrEmpty(newsentence)
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 09:26 AM
#24
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by FunkyDexter
No, you should set the condition to "there are no more lines to read". When there are no more lines to read this:-
Code:
newsentence = objtextfile.ReadLine()
will set newsentence to null. So you're condition would be There's a better way of checking for Null, though, which Si gave in his previous post. Really you should use this:-
Code:
String.IsNullOrEmpty(newsentence)
I literally have no idea where to put this or how to construct this condition, I tried setting to newsentence = null and no joy.
-
Apr 24th, 2018, 09:38 AM
#25
Re: Help please!!
I notice the Do-loop link earlier was for C#, here is a VB one: https://docs.microsoft.com/en-us/dot...loop-statement
If you abbreviate the Syntax block at the link you can get this:
Code:
Do
[ statements ]
Loop Until condition
You can simply replace condition with String.IsNullOrEmpty(newsentence)
-
Apr 24th, 2018, 09:42 AM
#26
Re: Help please!!
OK, take a step back and breath. I think you're miss understanding the purpose of all this.
The idea is that we don't want to read a set number of lines, we want to read however many lines there are. Are you happy with that idea?
When you use a For loop you do a set number of reads. If somebody gave you a larger file you wouldn't read the whole thing. Get a smaller file and you'll try and read a load of lines that don't exist (which is what was happening in your original code).
So we're changing it to a Do Loop with a condition. That loop will keep reading lines until the condition is met. And in your case you want the condition to be "there are no more lines". So the loop will keep reading lines until it's read them all. So your condition wants to be
Code:
Until String.IsNullOrEmpty(newsentence)
This doesn't change the way you set newsentence at all. That still wants to be read from the file using this line of code
Code:
newsentence = objtextfile.ReadLine()
exactly as you were before.
so your loop will look something like:-
Code:
do
newsentence = objtextfile.ReadLine()
if not String.IsNullOrEmpty(newsentence)
'Do some Stuff
end if
until String.IsNullOrEmpty(newsentence)
Try popping that into your code and see how you go.
Most importantantly, make sure you've understood what the loop is doing and why we've chosen the condition we have. Think, in particular, about what the contents of your various arrays (customerid, ticketid etc.) will look like once the loops finished. It's important you understand this because solving the next error you got is really going to require it.
Edit>
I notice the Do-loop link earlier was for C#
Oops, my bad
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 09:42 AM
#27
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by si_the_geek
I notice the Do-loop link earlier was for C#, here is a VB one: https://docs.microsoft.com/en-us/dot...loop-statement
If you abbreviate the Syntax block at the link you can get this:
Code:
Do
[ statements ]
Loop Until condition
You can simply replace condition with String.IsNullOrEmpty(newsentence)
OK, I've replaced with a Do While loop, seems to be working, how could I correct the next error?
-
Apr 24th, 2018, 09:51 AM
#28
Re: Help please!!
I'm about to head home so will be incommunicado for a bit. Will check in once I get home. Si's in the thread, though, so I'm sure he'll help you out in the mean time.
Most importantly, have you thought about what the contents of your arrays are going to look like?
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 24th, 2018, 09:53 AM
#29
Thread Starter
Junior Member
-
Apr 24th, 2018, 11:01 AM
#30
Re: Help please!!
Originally Posted by Brian Frost
That seemed to get past that error, however another error has been thrown up 'Object variable or With block variable not set.' on this line
Code:
If ticketid(x).substring(0, 1) = "F" Then
I should add that this is in the calculations sub.
Here's the relevant code:
Originally Posted by Brian Frost
Code:
'This Sub Contains The Code That Calculates The Data
Sub calculations(ByRef customerid, ByRef ticketid, ByRef numberoftickets, ByRef method)
...
'Pricing the nights Friday = £10 Rest = £5
For x = 0 To 499
If ticketid(x).substring(0, 1) = "F" Then
total = total + numberoftickets(x) * 10
Else
total = total + numberoftickets(x) * 5
End If
Next
...
And an important note above:
Originally Posted by FunkyDexter
Most importantly, have you thought about what the contents of your arrays are going to look like?
So now you've altered the other code in readdata, what will be in the ticketid array?
Some of the elements will have values, but (depending on the size of the file) not all of them will... and when there isn't a value, you'll get the error.
One way to deal with that is the same way we dealt with readdata, this time checking ticketid(x) instead of newsentence. Have a go at fixing it yourself, and if you can't get it working show us what you've come up with, and tell us what is going wrong.
-
Apr 25th, 2018, 02:42 AM
#31
Thread Starter
Junior Member
Re: Help please!!
Originally Posted by si_the_geek
Here's the relevant code:
And an important note above:
So now you've altered the other code in readdata, what will be in the ticketid array?
Some of the elements will have values, but (depending on the size of the file) not all of them will... and when there isn't a value, you'll get the error.
One way to deal with that is the same way we dealt with readdata, this time checking ticketid(x) instead of newsentence. Have a go at fixing it yourself, and if you can't get it working show us what you've come up with, and tell us what is going wrong.
Hello, I'm back online and trying to sort this ticketid array. Any more suggestions would be welcome.
-
Apr 25th, 2018, 03:51 AM
#32
Re: Help please!!
Read the last three paragraphs from Si's post and really think about them. In conjunction with the technique he showed you in post 11 they contain everything you need to solve the remaining issue. It's about checking there's something to act on before you act.
Have a go and see how you get on.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
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
|