Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,460 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,747 people online right now. Registration is fast and FREE... Join Now!




read, then multipy, then write...

 
Reply to this topicStart new topic

read, then multipy, then write..., Code reads and writes, but how do I multiply the existing values?

cckmm2407
5 Aug, 2008 - 09:23 AM
Post #1

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 5

Good afternoon,

I've written the code to read the file, and then write the file (same info). It works fine, but I need to increase the amounts from file1 by 10%. File 1 is called "payrates.txt" and file 2 is called "updated2.txt".

I've done conversions and multiplications using input boxes, but never from a file. I've referenced a book, VB Reloaded, and can't find any information. I've also searched the web looking through countless Visual Basic tutorials and the msdn site, only to find the same info. None on reading, multiplying, then writing.

I put in a message box to verify some info, that is not needed.
I also tried to code the calculations, but failed. That code is there, just commented out so you can see my attempts.

Can someone advise?

Thank you,

CODE

Option Explicit On
Option Strict On

Public Class MainForm
    Structure Product
        Public number As String
        Public pay As String
    End Structure

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub


    Private Sub increaseButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles increaseButton.Click

        Dim path As String = "C:\Copy of Pay Solution\Pay Project\"
        Dim text As String = My.Computer.FileSystem.ReadAllText(path & "payrates.txt")
        'Dim isConverted As Boolean
        'Dim increase As Integer
        
        'MsgBox(text)
        For Each word As String In My.Computer.FileSystem.ReadAllText(path & "payrates.txt")
            'increase = Convert.ToDecimal(value as string)
            'increase = text * 1.1D
            'text.Replace(text, text * 1.1D)
            'isConverted = Decimal.TryParse(.text, increase)

            My.Computer.FileSystem.WriteAllText _
            ("C:\Copy of Pay Solution\Pay Project\updated2.txt", text, False)
        Next

        
    End Sub
End Class



User is offlineProfile CardPM
+Quote Post

MrWobbles
RE: Read, Then Multipy, Then Write...
5 Aug, 2008 - 09:39 AM
Post #2

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 83



Thanked: 2 times
My Contributions
What does the text file look like?
User is offlineProfile CardPM
+Quote Post

cckmm2407
RE: Read, Then Multipy, Then Write...
5 Aug, 2008 - 09:46 AM
Post #3

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 5

QUOTE(cckmm2407 @ 5 Aug, 2008 - 10:23 AM) *

Good afternoon,

I've written the code to read the file, and then write the file (same info). It works fine, but I need to increase the amounts from file1 by 10%. File 1 is called "payrates.txt" and file 2 is called "updated2.txt".

I've done conversions and multiplications using input boxes, but never from a file. I've referenced a book, VB Reloaded, and can't find any information. I've also searched the web looking through countless Visual Basic tutorials and the msdn site, only to find the same info. None on reading, multiplying, then writing.

I put in a message box to verify some info, that is not needed.
I also tried to code the calculations, but failed. That code is there, just commented out so you can see my attempts.

Can someone advise?

Thank you,

CODE

Option Explicit On
Option Strict On

Public Class MainForm
    Structure Product
        Public number As String
        Public pay As String
    End Structure

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub


    Private Sub increaseButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles increaseButton.Click

        Dim path As String = "C:\Copy of Pay Solution\Pay Project\"
        Dim text As String = My.Computer.FileSystem.ReadAllText(path & "payrates.txt")
        'Dim isConverted As Boolean
        'Dim increase As Integer
        
        'MsgBox(text)
        For Each word As String In My.Computer.FileSystem.ReadAllText(path & "payrates.txt")
            'increase = Convert.ToDecimal(value as string)
            'increase = text * 1.1D
            'text.Replace(text, text * 1.1D)
            'isConverted = Decimal.TryParse(.text, increase)

            My.Computer.FileSystem.WriteAllText _
            ("C:\Copy of Pay Solution\Pay Project\updated2.txt", text, False)
        Next

        
    End Sub
End Class




It is simply a list of numbers

9
15
25
10
12
30
56
78
100
50

These are payrates that need to be increased by 10%.

Thank you,
cckmm2407
User is offlineProfile CardPM
+Quote Post

MrWobbles
RE: Read, Then Multipy, Then Write...
5 Aug, 2008 - 09:56 AM
Post #4

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 83



Thanked: 2 times
My Contributions
instead of reading it all in at once, why don't you read them in line by line, and then store what you read in into a variable, perform the calculation and write it to the other text file.


Something like this:

CODE

Imports System
Imports System.IO
Imports System.Collections

    Public Class MainForm
        
        Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
            Me.Close()
        End Sub


        Private Sub increaseButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles increaseButton.Click

            Dim pathR As String = "C:\Copy of Pay Solution\Pay Project\payrates.txt"
Dim pathW As String = "C:\Copy of Pay Solution\Pay Project\updated2.txt"


            Dim objReader As New StreamReader(pathR)
            Dim objWriter As New StreamWriter(pathW)
            Dim sLine As String = ""
            Dim arrText As New ArrayList()

            Do
                sLine = objReader.ReadLine()
                If Not sLine Is Nothing Then
                    arrText.Add(sLine)
                End If
            Loop Until sLine Is Nothing
            objReader.Close()

            For Each sLine In arrText
                'Perform calculation here
                objWriter.WriteLine(sLine)
            Next

        End Sub
    End Class


I didn't test it, but that should work..

This post has been edited by MrWobbles: 5 Aug, 2008 - 09:58 AM
User is offlineProfile CardPM
+Quote Post

cckmm2407
RE: Read, Then Multipy, Then Write...
5 Aug, 2008 - 10:02 AM
Post #5

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 5

I'll try it. Thank you. No where in the book or online have I found how to read line by line. I've learned this in COBOL, and thought that would be the way to go, but couldn't find the information.

Thank you again, and I'll let you know if I was able to implement your example.

User is offlineProfile CardPM
+Quote Post

Onibeaver
RE: Read, Then Multipy, Then Write...
23 Aug, 2008 - 02:52 PM
Post #6

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 8

I feel your pain cckmm2407, had to do that with C# probably will have to do it in VB too sad.gif Lord have mercy
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:16PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month