Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I solved this type of error previously but this time am getting stucked.
I have a file containing 2,31,200 numbers of 8 and 10 digits and i have to do sum all digits in each number then adding sum into a list, but while adding sums i got error. sum range is 0 to 90. I tried 'int, Int32, Int64' but by use of these i got different error "Value was either too large or too small for an Int32. hence im using 'long' by referencing some suggestions but my problem is not solved yet and getting another error-
Below is my code where Im getting above error in line
long sumOfnum = Summation(Convert.ToInt64(Allbarcodes[i]));
Whats wrong in my code please anyone suggest me way.

What I have tried:

public long Summation(long n)
     {
         long m, sum = 0;

         while (n != 0)
         {
             m = n % 10;
             sum = sum + m;
             n = n / 10;
         }
         return sum;
     }


 private void button2_Click(object sender, EventArgs e) // do task
     {
        List<long> barcodeList = new List<long>();

        using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read))
             using (StreamReader sr = new StreamReader(fs))
             {
                 string barcode_data = sr.ReadToEnd().Replace("\r\n", ",");
                 string[] Allbarcodes = barcode_data.Split(',');

                 for (int i = 0; i < Allbarcodes.Length; i++)
                 {
                     long sumOfnum = Summation(Convert.ToInt64(Allbarcodes[i]));
                     barcodeList.Add(sumOfnum);
                 }
             }
      }
Posted
Updated 24-Feb-17 18:44pm
v2
Comments
Graeme_Grant 25-Feb-17 0:12am    
What is the value of Allbarcodes[i] when the error occurs for the highlighed line:
long sumOfnum = Summation(Convert.ToInt64(Allbarcodes[i]));
Bryian Tan 25-Feb-17 0:14am    
possible Allbarcodes[i] = "" ? then Convert.ToInt64 blow out?
Graeme_Grant 25-Feb-17 0:32am    
Did you look at the value? Sorry Bryian, mistook you for the OP.

1 solution

Sound like a data issue, these work around will stop the error but it might pollute the results. You need to find out what when wrong in Allbarcodes[i]

1. Use TryParse, if failed substitute with 0
C#
long sumOfnum = 0;
Int64.TryParse(Allbarcodes[i], out sumOfnum);
barcodeList.Add(sumOfnum);

2.check if Allbarcodes[i] is empty before converting
C#
if (!string.IsNullOrEmpty(Allbarcodes[i]) )
{
    long sumOfnum = Summation(Convert.ToInt64(Allbarcodes[i]));
    barcodeList.Add(sumOfnum);
}
 
Share this answer
 
Comments
Member 11543226 25-Feb-17 0:44am    
i tried your 2nd option and it worked bcz last string is empty when i replace "\r\n" with "," i am getting actually 0-2,31,198 numbers and last 2 string empty hence i got error . thanks

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