Welcome to Dream.In.Code
Become a C# Expert!

Join 137,425 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,954 people online right now. Registration is fast and FREE... Join Now!




String conversion driving me nuts

 
Reply to this topicStart new topic

String conversion driving me nuts

rareseu
8 Jan, 2008 - 05:20 AM
Post #1

New D.I.C Head
*

Joined: 24 Dec, 2007
Posts: 4

Hello ppl ! i'm trying to read the values of signal out of a .txt file so that i can draw the signal in a picturebox, but i'm running into a very annoying problem when i try to convert a string to integer, here are the bits of code to make it easier to understand :

CODE

//variables that i'm using
private string[] info;
private int[] signalValues;


CODE
  
//read from file and take out white spaces      
info = sourceFile.ReadToEnd().Split(' ');

//converting the info to integer
for (int i = 0; i <= info.Length; i++)
   signalValues[i] = Convert.ToInt32(info[i]);


and i'm getting a "Input string not in a correct format" unhandled expection, the numbers in the file are small (3 digits max), please tell me what i'm doing wrong
User is offlineProfile CardPM
+Quote Post

qdoom
RE: String Conversion Driving Me Nuts
8 Jan, 2008 - 06:56 AM
Post #2

D.I.C Head
**

Joined: 31 Aug, 2006
Posts: 82


My Contributions
Welcome to dream.in.code!

1. <= means, that it will do the loop one too many times. <= operator returns true, if the first operand is less than or equal to the second. You should get "IndexOutOfRangeException". It should be:

CODE
for (int i = 0; i < info.Length; i++)
   signalValues[i] = Convert.ToInt32(info[i]);


2. if your numbers are not in one line, the program will not split the newline for you. That's why you could be getting "Input string not in a correct format" exception.

CODE
info = sourceFile.ReadToEnd().Split(' ', '\n');


Hope this helps.

User is offlineProfile CardPM
+Quote Post

rareseu
RE: String Conversion Driving Me Nuts
8 Jan, 2008 - 08:10 AM
Post #3

New D.I.C Head
*

Joined: 24 Dec, 2007
Posts: 4

thank you it did help smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:41AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month