It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

Calculating from Multiple fields in a .txt into a listBox in Visual Basic

page: 1
0

log in

join
share:

posted on Apr, 18 2015 @ 08:29 PM
link   
OK, so basically I am stuck. Obviously lol. I have a .txt file that I have to import into a lstbox and populate it with city names that are listed in the text field. I got that. Below (in the .txt file) is a market price. I have to show in another list box a 5 year 3% increase. So every year for five years the price increases for 5 years. How do I go about doing that? I got the information imported to show the city names in the first textbox. But I am unsure how to do it for the arithmetic.

Here is the .txt file.



New York City
441600
Los Angeles
350100
Chicago
167400
Houston
167800
Philadelphia
209800
Phoenix
159100
San Antonio
160600
San Diego
405400
Dallas
157200
San Jose
685000


Here is what I have done with my code thus far.




Option Strict On

Imports System.IO

Public Class frmMedianHomePrices
Private _intRateOfIncrease As Integer = 0.03D
Public Shared _intSizeOfArray As Integer = 9
Public Shared _strHouse(_intSizeOfArray) As String
Private _strItemID(_intSizeOfArray) As String
Private _decInitialHousePrice(_intSizeOfArray) As Decimal
Private _intQuantity(_intSizeOfArray) As Integer


Private Sub frmMedianHomePrices_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim objReader As IO.StreamReader
Dim strLocationAndNameOfFile As String = "C:\USMedianHomePrices\cities.txt"
Dim intCount As Integer = 0
Dim intFill As Integer
Dim strFileError As String = "File not available"
Dim FileLines As New List(Of String)

'Doesn't work
'If IO.File.Exists(strLocationAndNameOfFile) Then
' objReader = IO.File.OpenText(strLocationAndNameOfFile)

' Do While objReader.Peek -1
' _strHouse(intCount) = objReader.ReadLine()
' _strItemID(intCount) = objReader.ReadLine()
' _decInitialHousePrice(intCount) = Convert.ToDecimal(objReader.ReadLine())
' _intQuantity(intCount) = Convert.ToInt32(objReader.ReadLine())
' intCount += 1

' Loop
' objReader.Close()

' For intFill = 0 To (_strItemID.Length - 1)
' lstSelectCity.Items.Add(_strItemID(intFill))

' Next
'Else : MsgBox(strFileError, , "Error")

'End If


'Works but shows the home prices when it's supposed to not be visible...only used begin the math.
' lstSelectCity.Items.AddRange(System.IO.File.ReadAllLines(strLocationAndNameOfFile))

'This works
If IO.File.Exists(strLocationAndNameOfFile) Then 'Chech if the file exists

FileLines.AddRange(IO.File.ReadAllLines(strLocationAndNameOfFile)) 'add each item to an element on the list
If FileLines.Count > 0 Then 'check if the file is larger than 0 items
For x = 0 To FileLines.Count - 1 Step 2 'show every other item
lstSelectCity.Items.Add(FileLines(x)) 'Add the item to the list held as x
Next
End If
Else

MsgBox("error", MsgBoxStyle.Critical, "error")
End If

End Sub
End Class



posted on Apr, 18 2015 @ 09:32 PM
link   
a reply to: wwe9112

did you do a google search for ways to do that?



posted on Apr, 18 2015 @ 10:23 PM
link   

originally posted by: undo
a reply to: wwe9112

did you do a google search for ways to do that?

Yeah, I come up with nothing but my program crashing. I'm a week into trying different ways and am literally stuck -_-. I did add more code but it's somewhat irrelevant to what the question was but I kept one way I thought would work and it didn't...everything just freezes and my label doesn't update like it did prior to the loop you will see in the code. Basically it's taking the initial cost, multiplying it by 3 percent and each time adding it to the new cost for the increase. Even if the math is wrong, but listBox shold have updated and the program shouldn't have froze like that 0_0.


Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

Dim intNewYorkCityInitialHouseCost As Integer = 441600
Dim intLAInitialHouseCost As Integer = 350100
Dim intChicagoInitialHouseCost As Integer = 167400
Dim intHoustonInitialHouseCost As Integer = 167800
Dim intPhiladelphiaInitialHouseCost As Integer = 209800
Dim intPhoenixInitialHouseCost As Integer = 159100
Dim intSanAntonioInitialHouseCost As Integer = 160600
Dim intSanDiegoInitialHouseCost As Integer = 405400
Dim intDallasInitialHouseCost As Integer = 157200
Dim IntSanJoseInitialHouseCost As Integer = 685000

Dim strSelectedCity As String = lstSelectCity.Text
Dim decLAFinalPrice As Decimal

If strSelectedCity = "New York City" Then
lblSelectedCityResultLabel.Text = "New York City Median House price is: " & intNewYorkCityInitialHouseCost.ToString("C")

Else


If strSelectedCity = "Los Angeles" Then
lblSelectedCityResultLabel.Text = "LA Median House price is: " & intLAInitialHouseCost.ToString("C")
'For intLAInitialHouseCost = 0 To 4
' intLAInitialHouseCost = (intLAInitialHouseCost * _intRateOfIncrease)
' intLAInitialHouseCost = CInt(intLAInitialHouseCost + decLAFinalPrice)
' lstExpectedIncreaseValueResult.Items.Add(intLAInitialHouseCost.ToString("C"))
'Next
Else
If strSelectedCity = "Chicago" Then
lblSelectedCityResultLabel.Text = "Chicago Median House price is: " & intChicagoInitialHouseCost.ToString("C")
Else
If strSelectedCity = "Houston" Then
lblSelectedCityResultLabel.Text = "Houseton Median House price is: " & intHoustonInitialHouseCost.ToString("C")
Else
If strSelectedCity = "Philadelphia" Then
lblSelectedCityResultLabel.Text = "Philadelphia Median House price is: " & intPhiladelphiaInitialHouseCost.ToString("C")
Else
If strSelectedCity = "Phoenix" Then
lblSelectedCityResultLabel.Text = "Phoenix Median House price is: " & intPhoenixInitialHouseCost.ToString("C")
Else
If strSelectedCity = "San Antonio" Then
lblSelectedCityResultLabel.Text = "San Antonio Median House price is: " & intSanAntonioInitialHouseCost.ToString("C")
Else
If strSelectedCity = "San Diego" Then
lblSelectedCityResultLabel.Text = "San Diego Median House price is: " & intSanDiegoInitialHouseCost.ToString("C")
Else
If strSelectedCity = "Dallas" Then
lblSelectedCityResultLabel.Text = "Dallas Median House price is: " & intDallasInitialHouseCost.ToString("C")
Else
If strSelectedCity = "San Jose" Then
lblSelectedCityResultLabel.Text = "San Jose Median House price is: " & IntSanJoseInitialHouseCost.ToString("C")
End If
End If

End If
End If

End If
End If
End If
End If
End If
End If
End Sub
End Class



posted on Apr, 18 2015 @ 11:24 PM
link   
a reply to: wwe9112

Huh, I have no experience with visual basic but your code look very complicated. In python it would be 10 lines of code - yes so much because you will need to use exception. ....

with open('/path/to/file') as f:
for line in f:
try:
mnumber = int(line.srstrip('n'))
do whatever you want to do with number
except Eception:
continue


ATS clearly do not like indentation


edit on 18-4-2015 by JanAmosComenius because: (no reason given)

edit on 18-4-2015 by JanAmosComenius because: (no reason given)



posted on Apr, 18 2015 @ 11:52 PM
link   
You should ask this on a different forum.

Try TheNewBoston.com



posted on Apr, 19 2015 @ 01:43 AM
link   
StackOverflow would be the best place for questions like this.

My personal suggestion is, use a real programming language

edit on 19-4-2015 by Aedaeum because: (no reason given)



posted on Apr, 19 2015 @ 03:08 AM
link   
a reply to: wwe9112

did you try to google the problem itself?



posted on Apr, 19 2015 @ 10:20 AM
link   

originally posted by: Aedaeum
StackOverflow would be the best place for questions like this.

My personal suggestion is, use a real programming language


Visual basic IS a real programming language, and still has its uses.



posted on Apr, 19 2015 @ 10:38 AM
link   

originally posted by: Aedaeum
StackOverflow would be the best place for questions like this.

My personal suggestion is, use a real programming language

LOL it's not a choice, it's an assignment -_-. The way the book has it set up is wrong and doesn't work. Even the instructor said so. Yeet he doesn't answer questions or emails he said he wont be there on the job. Well that's true but m-effer, i cannot learn if I don't have something correct to look at! I've googled, I've even googled the freaking assignment to no avail.



posted on Apr, 19 2015 @ 02:49 PM
link   
a reply to: Kuroodo

The only reason to use Visual Basic is if a company is run by a bunch of old farts who can't keep up with the times. There are so many modern languages nowadays which can cut development time by 50 - 75 percent, doing the same exact operations. All in all though, I was simply poking fun, it was not meant as an insult and I believe wwe9112 understood that.

a reply to: wwe9112
StackOverflow.com is going to be your best bet for a quick fix to your issue. A lot of experienced devs on there.



posted on Apr, 20 2015 @ 10:49 AM
link   
a reply to: wwe9112

In the "Doesn't work" part, why are doing four objReader.ReadLine() in the "Do While objReader.Peek -1" loop?




top topics



 
0

log in

join