Reading and Writting

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
Having some trouble with what I thought would be a simple task.

I have a file "payrates.txt". I need to read this file which looks like :

19
30
9
1
3
..etc from top to bottom.

I need to then need to increase each one of those integers by 10% and save to "update.txt"

The GUI will only be a convert button and exit button, so no list box's to load anything to. I came up with :


VB.NET:
    Private Sub increaseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles increaseButton.Click
        Dim payrates As String
        payrates = My.Computer.FileSystem.ReadAllText("C:\payrates.txt")

        Dim Update As String
        Dim isConverted As Boolean

        Update = CStr(CDbl(payrates) * 10%))
        My.Computer.FileSystem.WriteAllText("update.txt", Update, False)

    End Sub
But these produces errors as I cannto convert anything from the string "payrates.text" to decimal.
 
Last edited by a moderator:
The problem is that ReadAllText reads the entire contents of the file into a single string. How are you going to convert that string to a number and then increase it by 10%?

What you should do is call IO.File.ReadAllLines, which returns a String array with each element containing one line's worth of text. You can then loop through the array and for each element you convert it to a number, increase its value, convert it back to a string, then assign it back to the array element. Once that's done you can call WriteAllLines to write the entire array back to the file.

Note also that if you want to increase a number by 10% you need to multiply it by 110/100, or 1.1.
 
Having some trouble with what I thought would be a simple task.

I have a file "payrates.txt". I need to read this file which looks like :



I need to then need to increase each one of those integers by 10% and save to "update.txt"

The GUI will only be a convert button and exit button, so no list box's to load anything to. I came up with :


VB.NET:
    Private Sub increaseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles increaseButton.Click
        Dim payrates As String
        payrates = My.Computer.FileSystem.ReadAllText("C:\payrates.txt")

        Dim Update As String
        Dim isConverted As Boolean

        Update = CStr(CDbl(payrates) * 10%))
        My.Computer.FileSystem.WriteAllText("update.txt", Update, False)

    End Sub
But these produces errors as I cannto convert anything from the string "payrates.text" to decimal.

This is as much a pencil and paper exercise as anything. You have written here "I need to then need to increase each one of those integers..."

Tell me. If a maths teacher gave you a list of numbers:
99
50
90
130

And told you to increase each by 10%, would you write this in your book:

995090130 * 1.1 = 1094599143

and then tell him the answer was 1094599143 (one billion, 94 million, 599 thousand, 143) ?

Ok, so you need to get your pencil and paper out and rethink the logic in this one

Remember, weeks of coding can save you hours of planning!
 
I'm sorry. I'm very new to programming all together. I'm working on some projects trying to learn. I been at what you suggested and I can't figure it out. aside from being so new arrays elude me even more.
 
Back
Top