Now I have this part down but now i need to make it so it creates another row at then end that says the percentage change overall.
here is the code I have so far
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim col As Integer
Dim rows() As String = IO.File.ReadAllLines("C:\sales.txt")
Dim All(rows.GetUpperBound(0), rows(0).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries).GetUpperBound(0)) As Decimal
Dim newRows As New List(Of String)
For row As Integer = 0 To rows.GetUpperBound(0)
Dim parts() As String = rows(row).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries)
newRows.Add(String.Join(" ", parts) & " ")
For column As Integer = 0 To parts.GetUpperBound(0)
col = parts.GetUpperBound(0)
All(row, column) = CDec(parts(column))
If row > 0 Then
newRows(newRows.Count - 1) &= (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & " "
ElseIf row = 0 AndAlso column = 0 Then
Dim d(parts.GetUpperBound(0)) As Decimal
newRows(newRows.Count - 1) &= String.Join(" ", Array.ConvertAll(Of Decimal, String)(d, Function(v As Decimal) v.ToString("n2")))
End If
Next
Next
TextBox1.Lines = newRows.ToArray
'to save:
IO.File.WriteAllLines("C:\Solution.txt", newRows.ToArray)
' squareArray is the array
MessageBox.Show("Done!!!")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
Were you here under a different name, or is this a class assignment? I ask because that's a really strange requirement, and you are the second person to ask in as many weeks.
In any case, what is the percentage overall? Is that just the change from the first to the last?
Here is the "sales.txt" file and the "solution.txt" file that my program creates. But the sales.txt file is subject to change, so the program has to manipulate the data dynamically.
Basically the percentage of change between the first and last rows of data in Sales.txt
I can think of doing this in two ways:
-adding all the values in the newly created rows together in each column
-(last row/first row * 100) - 100 for each column
the total percentage differences will be in the difference array + i'm sure you'll be able to work out how to add it to the newrows list before you write your new file.
at least you admitted this is homework that guy last week forgot to mention it...
I am still getting the same problem...
Rather than getting the overall percentage change for each column I'm getting -34.41
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim col As Integer
Dim rows() As String = IO.File.ReadAllLines("C:\sales.txt")
Dim All(rows.GetUpperBound(0), rows(0).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries).GetUpperBound(0)) As Decimal
Dim newRows As New List(Of String)
For row As Integer = 0 To rows.GetUpperBound(0)
Dim parts() As String = rows(row).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries)
newRows.Add(String.Join(" ", parts) & " ")
For column As Integer = 0 To parts.GetUpperBound(0)
col = parts.GetUpperBound(0)
All(row, column) = CDec(parts(column))
If row > 0 Then
newRows(newRows.Count - 1) &= (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & " "
ElseIf row = 0 AndAlso column = 0 Then
Dim d(parts.GetUpperBound(0)) As Decimal
newRows(newRows.Count - 1) &= String.Join(" ", Array.ConvertAll(Of Decimal, String)(d, Function(v As Decimal) v.ToString("n2")))
End If
Next
Next
TextBox1.Lines = newRows.ToArray
'to save:
IO.File.WriteAllLines("C:\Solution.txt", newRows.ToArray)
Dim change = (From line In IO.File.ReadAllLines("C:\Solution.txt") Let fields = line.Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries) Select CDec(fields(7))).Sum
TextBox1.AppendText(vbCrLf & change)
'Dim lines() As String = IO.File.ReadAllLines("C:\Solution.txt")
' Dim change2 As Decimal = ((CDec(lines(lines.GetUpperBound(0)).Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)(0)) / CDec(lines(0).Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)(0))) * 100) - 100.ToString("n2") & " "
' Dim WriteSolution As System.IO.StreamWriter
' WriteSolution = System.IO.File.AppendText("C:\Solution.txt")
'WriteSolution.WriteLine(TextBox1.Text)
'WriteSolution.Close()
IO.File.WriteAllLines("C:\Solution.txt", TextBox1.Text)
MessageBox.Show("Done!!!")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
I also don't know why I get:
"Value of type 'String' cannot be converted to '1-dimensional array of String'."
When i try the uncommented out method of writing to the text file.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim col As Integer
' Dim lengthof As Integer
Dim rows() As String = IO.File.ReadAllLines("C:\sales.txt")
Dim All(rows.GetUpperBound(0), rows(0).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries).GetUpperBound(0)) As Decimal
Dim difference(All.GetUpperBound(1)) As Decimal
Dim newRows As New List(Of String)
For row As Integer = 0 To rows.GetUpperBound(0)
Dim parts() As String = rows(row).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries)
newRows.Add(String.Join(" ", parts) & " ")
For column As Integer = 0 To parts.GetUpperBound(0)
col = parts.GetUpperBound(0)
All(row, column) = CDec(parts(column))
If row > 0 Then
newRows(newRows.Count - 1) &= (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & " "
difference(column) += (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & " "
ElseIf row = 0 AndAlso column = 0 Then
Dim d(parts.GetUpperBound(0)) As Decimal
newRows(newRows.Count - 1) &= String.Join(" ", Array.ConvertAll(Of Decimal, String)(d, Function(v As Decimal) v.ToString("n2")))
End If
Next
Next
TextBox1.Lines = newRows.ToArray
TextBox1.AppendText(vbCrLf)
For count As Integer = 0 To difference.Length - 1
TextBox1.AppendText(difference(count) & " ")
Next
'to save:
IO.File.WriteAllText("C:\Solution.txt", TextBox1.Text)
MessageBox.Show("Done!!!")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
think it through. All(,) is a decimal, difference() is a decimal, so to add a decimal element + a decimal element, why convert 1 of those values to a string?