我正在尝试完成学校作业,但这并不是从字符串中完全提取出我需要的东西。基本上,我是在做收据,是在创建一个文本文件,然后将订单数量保存到其中,然后拉Cowboy.txt
来存储每件商品的描述和价格。
因此,在第16行上,我使用StreamReader
来提取Cowboy.txt
文件的内容,在第17行上,我使用Split
将每一行分成一个数组。因此,line(0)
应该是"Colt Peacemaker"
,line(1)
应该是"12.20"
,因为第17行上的代码在逗号处分割了字符串。但是,当我运行代码时,它仅在"C"
中放入line(0)
,在"olton Peacemaker"
中放入line(1)
。由于line(1)
已通过Double
变量更改为price
,因此会产生错误。
据我了解,第17行的代码是从Cowboy.txt
文件(Colt Peacemaker, 12.20
)中取出整行并将其分为两部分,在,
处进行拆分。为什么不这样做呢?
Public Class cowboyReceipt
Private Sub btnResults_Click(sender As Object, e As EventArgs) Handles
btnResults.Click
lstBoxReceipt.ClearSelected()
Dim sw As IO.StreamWriter = IO.File.CreateText("ItemQty.txt")
sw.WriteLine("3")
sw.WriteLine("2")
sw.WriteLine("10")
sw.WriteLine("1")
sw.WriteLine("4")
sw.Close()
Dim total As Integer
Dim srItemQty As IO.StreamReader = IO.File.OpenText("ItemQty.txt")
Dim srItemDescription As IO.StreamReader =
IO.File.OpenText("Cowboy.txt")
Do Until srItemDescription.EndOfStream
Dim qty As String = srItemQty.ReadLine
Dim items As String = srItemDescription.ReadLine
Dim line() As String = items.Split(items, ","c)
Dim desc As String = line(0)
Dim price As Double = line(1)
lstBoxReceipt.Items.Add(qty & desc & (CInt(qty) * CInt(price)))
total += CInt(qty) * CInt(price)
Loop
End Sub
End Class
这是Cowboy.txt
的内容:
Colt Peacemaker, 12.20
Holster, 2.00
Levi Strauss jean, 1.35
Saddle, 40.00
Stetson, 10.00