Getting Sum Of Numbers In Each Line Of File

a 夏天 提交于 2020-01-26 01:45:06

问题


i have a file that there is some numbers that i want to sum number one with number two in each line.here is numbers in my file:

-944 -857
-158 356
 540 70
 15 148

for example i want to sum -944 and -857 what should i do?? i did it like the code below to check whats the numbers and the output is -158 and 15(it doesnt show -944 and 540 !!!):

        StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
        while (ar.ReadLine() != null)
        {
            string[] spl = ar.ReadLine().Split(' ');
            MessageBox.Show(spl[0]);

        }

回答1:


you're doing a readline in your while condition and again in the body of your while scope, thus skipping 1 readline instruction (in the while condition). try this instead:

StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
string s = ar.ReadLine();
while (s != null)
{
    //string[] spl = s.Split(' ');
    // below code seems safer, blatantly copied from one of the other answers..
    string[] spl = s.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
    MessageBox.Show(spl[0]);
    s = ar.ReadLine();
 }



回答2:


You are reading a line in the while check, and then again to parse the value - which is why it only seems to read the even lines.

Proposed solution:

StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
// Prepare the first line.
string line = ar.ReadLine();
while (line != null) {
    // http://msdn.microsoft.com/en-us/library/1bwe3zdy.aspx
    string[] spl = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
    MessageBox.Show(spl[0]);
    // Prepare the next line.
    line = ar.ReadLine();
}

update: using an overload of string.Split() that returns no empty results and max 2 values (1 and the rest of the string).




回答3:


Try this (streamlined version):

    StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
    while ((string line = ar.ReadLine()) != null)
    {
        string[] spl = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
        MessageBox.Show(spl[0]);
    }

You are reading twice but only using the second read.

Note: You are also not trimming spaces from the start of the lines so you will lose numbers if the data has leading spaces (as shown in the example).




回答4:


    StreamReader ar = new StreamReader(@"C:\Users\arash\Desktop\problem1 (3).in");
    // Load a line into s
    string s = ar.ReadLine();
    while (s != null)
    {
        // Split on space
        string[] spl = s.Trim(' ').Split(' ');
        // Declare two variables to hold the numbers
        int one;
        int two;
        // Try to parse the strings into the numbers and display the sum
        if ( int.TryParse( spl[0], out one ) && int.TryParse( spl[1], out two) ) {
            MessageBox.Show( (one + two).ToString() )
        }
        // Error if the parsing failed
        else {
            MessageBox.Show("Error: Numbers were not in integer format");
        }
        // Read the next line into s
        s = ar.ReadLine();

    }


来源:https://stackoverflow.com/questions/7595339/getting-sum-of-numbers-in-each-line-of-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!