Click here to Skip to main content
15,890,350 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Im frustrated. I thought this would be easier.
I have used this code before....actually I just cut and pasted it into
my new code. However the array sizes is much larger 1000,10000,100000 and reading from a a file that accept random numbers of up to 300,000. Now my code will not work.

This is the piece of code...
<prelang> while ((number = s.ReadLine()) != null)
           {
               array[item] = Convert.ToInt32(number);
               item++;
           }</prelang>


I am getting the error that input was not in correct format. The funny thing is I separated the code into 2 different files and it worked. I cant figure out why this is. These are very large numbers, could that be it?

thanks
:confused:
Posted
Comments
JF2015 26-Nov-10 2:56am    
Could you post the part of the file that causes this exception. There must be some strange value that can not be converted to integer.
CPallini 26-Nov-10 3:27am    
Why don't you make a better use of the debugger and find what is the offending input?

Tanacia,

1 you may use int.Parse(..) or int.TryParse(..) where you have more control.
2 Can you publish the value of number that throws an exception?

Cheers,
Avi
 
Share this answer
 
v2
Comments
Dalek Dave 26-Nov-10 3:55am    
Exactly what I would have done!
Tanacia 26-Nov-10 14:03pm    
Rock on! Thank you so much! It worked.
Try this:

int LineNo = 0;
while ((number = s.ReadLine()) != null)
{
    lineNo++;
    int n = 0;
    bool didConvert = int.TryParse(number, n);
    if(didConvert)
    {
        array[item] = n;
        item++;
    }
    else
        Console.WriteLine("Line {1}: Could not convert {0} to an int!", number, lineNo);
}


Cheers

Manfred
 
Share this answer
 
v2
Comments
Dalek Dave 26-Nov-10 3:55am    
Yep! that's the way.
Tanacia 26-Nov-10 14:03pm    
Whew...this took care of the error! Worked like a charm. That was weird I dont get why now it all of a sudden didnt work!
Hi, write the while statement as
while (!string.IsNullOrEmpty(number = s.ReadLine())) may be better :) BTW
 
Share this answer
 
v3
Hi
If you are getting a "not correct format" error, there could be two reasons:

1) There are non-numeric characters in the string you are trying to convert (such as commas or spaces), or
2) as you stated, the number might be too big.

If it's because the number is too big, then consider using the decimal type. It's like an integer, but can store much larger sizes.

Hope that helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900