473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trying to input data (double) from text file to array

Hi All, I'm trying to read data from a text file into an array. The text file (a test) looks like this:

1
2
3
4
5
6
7
8
9

And the code I've got so far looks like this:

Dim readerVar As StreamReader
Dim dr As DialogResult
Dim i As Integer
Dim WorkString As String = "a"

dr = dlgOpen.ShowDialog()
If dr = DialogResult.OK Then

readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1 ' Loop to read number of lines in file
WorkString = readerVar.ReadLine() ' Get line
i = i + 1 ' Count number of lines in file
End While

ReDim readArray(i)
readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1
WorkString = readerVar.ReadLine() ' Get line
readArray(i) = Double.TryParse(WorkString, readArray(i))

i = i + 1
End While
txtBox.Text = readArray(0)
readerVar.Close()
Else

End If

(I know, terrible isn't it!)

The array now reads something like this:

-1
-1
-1
-1
-1
-1
-1
0
0

Can anyone help me?

Andy

PS does anyone have any code to draw a simple graph from a 1D array?
Oct 31 '06 #1
2 3818
willakawill
1,646 1GB
Hi All, I'm trying to read data from a text file into an array. The text file (a test) looks like this:

1
2
3
4
5
6
7
8
9

And the code I've got so far looks like this:

Dim readerVar As StreamReader
Dim dr As DialogResult
Dim i As Integer
Dim WorkString As String = "a"

dr = dlgOpen.ShowDialog()
If dr = DialogResult.OK Then

readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1 ' Loop to read number of lines in file
WorkString = readerVar.ReadLine() ' Get line
i = i + 1 ' Count number of lines in file
End While

ReDim readArray(i)
readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1
WorkString = readerVar.ReadLine() ' Get line
readArray(i) = Double.TryParse(WorkString, readArray(i))

i = i + 1
End While
txtBox.Text = readArray(0)
readerVar.Close()
Else

End If

(I know, terrible isn't it!)

The array now reads something like this:

-1
-1
-1
-1
-1
-1
-1
0
0

Can anyone help me?

Andy

PS does anyone have any code to draw a simple graph from a 1D array?
Hi Andy,
This looks like .NET code. Not too easy for crusty old vb6 monkeys to help :)
Oct 31 '06 #2
Killer42
8,435 Expert 8TB
Hi Andy,
This looks like .NET code. Not too easy for crusty old vb6 monkeys to help :)
Afraid I'm VB6 too, but could it be related to not closing the reader after the first time through the file? I'm not familiar with the Double.TryParse business, guess I would have used Val() or Cdbl() functions.

Also, on closer inspection, even without really being familiar with .NET, this construction
Expand|Select|Wrap|Line Numbers
  1. readArray(i) = Double.TryParse(WorkString, readArray(i))
just doesn't look correct. readArray(i) is both one of the two parameters, and the output. Perhaps the value returned by the TryParse method (-1 or 0 in this case) is simply a success/failure indicator. Maybe try putting that in a dummy variable. Or, if it is valid syntax, just do this
Expand|Select|Wrap|Line Numbers
  1. Double.TryParse(WorkString, readArray(i))
Oct 31 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
5
by: google | last post by:
first, a little background... i have a C program which preprocesses some data, and then outputs the results into a text file. that text file, in turn, is used as input to a FORTRAN...
4
by: Ronny Mandal | last post by:
Hi. Assume you have a file with the format 1.2 4.2 4.3 2.9 i.e doubledouble
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
1
by: supahsain | last post by:
I got this assignment, and almost have no clue, and my teacher doesn't even teaches us anything This is what i am suppose to do...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
1
by: xiao | last post by:
HI~ guys , I have a program here (Sorry it is very long about 240 lines.) It can read and write the header information successfully but it cannot write the array successfully. I guess there is...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.