Hi everyone
Im trying to do something in VB.net and failing.
I want to read integers as they are below from a file and put them into an array of integer.
2,4,3,1,2,4,5,6,4,3,2,4,2
2,4,7,5,3,8,2,4,3,2,4,2,0
Each line has the same number of numbers. and each line will go to a seperate array
How can i read this data and place it into an array of type integer.
The usuall way using streamreader gets me data type missmatch errors.
Thanks in advance
Read from file help needed
Started by deloreandude, Jul 22 2008 12:03 PM
13 replies to this topic
#1
Posted 22 July 2008 - 12:03 PM
|
|
|
#2
Posted 22 July 2008 - 02:58 PM
That's because of the commas. You're better off replacing the commas with spaces; try that first, and if it doesn't work, let us know.
#4
Posted 24 July 2008 - 09:04 AM
Ok im still having problems with this..
the file needs to contain integers but the format can be changed
such as 1,2,3,1,5,2,4 or with spaces instead...
i need to ultimately get these bumbers into a 2D array... however for now into a 1D as integers would be fine.
they must be as integers,
any help would be appreciated.. thanks
the file needs to contain integers but the format can be changed
such as 1,2,3,1,5,2,4 or with spaces instead...
i need to ultimately get these bumbers into a 2D array... however for now into a 1D as integers would be fine.
they must be as integers,
any help would be appreciated.. thanks
#6
Posted 24 July 2008 - 09:37 AM
Dim data As String = System.IO.File.ReadAllText("C:\thefile.txt")
Dim thearray(10) As Integer
thearray = data.Split(",")
had to change the code because of array initialize error.. and now it says
value 1 dimentional array ... cannot be convert..because string is not derived from integer
Dim thearray(10) As Integer
thearray = data.Split(",")
had to change the code because of array initialize error.. and now it says
value 1 dimentional array ... cannot be convert..because string is not derived from integer
#8
Posted 24 July 2008 - 09:40 AM
just an error saying
value of type 1 d array of string cannot be converted to an integer
value of type 1 d array of string cannot be converted to an integer
#9
Posted 24 July 2008 - 10:39 AM
#10
Posted 24 July 2008 - 01:28 PM
ok so how do i change that to an array of integer values?
#11
Posted 24 July 2008 - 06:37 PM
Just go through each item and use CInt() on it, and store it in a second integer array.
#12
Posted 27 July 2008 - 05:11 AM
Yep. Use a for loop or a foreach loop.
Untested, but does it work?
Dim data As String = System.IO.File.ReadAllText("C:\thefile.txt")
Dim thearray(10) As String
thearray = data.Split(",")
Dim intarray(thearray.Length) As Integer
For (index As Integer) = 0 To thearray.Length - 1
intarray(index) = CInt(thearray(index))
Next index
Untested, but does it work?
Edited by Xav, 27 July 2008 - 05:17 AM.


Sign In
Create Account

Back to top









