3

in this code, i am trying to print the starting line number. here i am reading the values from text file and adding up the values. but i am trying to get the line number from where it is starting to count..

How can i do this?

Code

import java.io.*;

class CountR
{
    public static void main(String args[])
    {
        setForSum("read.txt",7);
    }

    public static void setForSum(String filename,int param2)
    {
        try
        {
            FileInputStream fstream = new FileInputStream(filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            int i = 0;
            int sum=0; 
            int startline=0;
            System.out.println(param2+": Is in");
            while ((strLine = br.readLine()) != null)   
            {
                ++i;
                startline = i;
                int line = Integer.parseInt(strLine);
                sum=sum+line;
                System.out.println("Sum= "+sum);
                if(sum>=param2)
                {                 
                    /* System.out.println("line number : "+i);*/ 
                    System.out.println("Start line number :"+startline+" End line number:" +i);
                    sum =0;
                }                                
            }            
            in.close();
        }
        catch (Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

read.txt file

1
2
3
4
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3

the output i am getting now is like this.

Sum= 1
Sum= 3
Sum= 6
Sum= 10
Start line number :4 End line number:4
Sum= 1
Sum= 3
Sum= 6
Sum= 7
Start line number :8 End line number:8
Sum= 2
Sum= 5
Sum= 6
Sum= 8
Start line number :12 End line number:12
Sum= 3
Sum= 4
Sum= 6
Sum= 9
Start line number :16 End line number:16
Sum= 1
Sum= 3
Sum= 6
Sum= 7
Start line number :20 End line number:20
Sum= 2
Sum= 5
Sum= 6
Sum= 8
Start line number :24 End line number:24
Sum= 3

But it should be

    Sum= 1
    Sum= 3
    Sum= 6
    Sum= 10
    Start line number :1 End line number:4
    Sum= 1
    Sum= 3
    Sum= 6
    Sum= 7
    Start line number :5 End line number:8
    Sum= 2
    Sum= 5
    Sum= 6
    Sum= 8
    Start line number :9 End line number:12
    Sum= 3
    Sum= 4
    Sum= 6
    Sum= 9
    Start line number :13 End line number:16
    Sum= 1
    Sum= 3
    Sum= 6
    Sum= 7
    Start line number :17 End line number:20
    Sum= 2
    Sum= 5
    Sum= 6
    Sum= 8
    Start line number :21 End line number:24
    Sum= 3
4

2 回答 2

3

你不是在重置startline任何地方。使用以下内容:

       while ((strLine = br.readLine()) != null)   
        {
            ++i;
            if(startline == 0) // set startline only if startline is zero, which will happen the first time and everytime you've printed the status
                startline = i;
            int line = Integer.parseInt(strLine);
            sum=sum+line;
            System.out.println("Sum= "+sum);
            if(sum>=param2)
            {                 
                /* System.out.println("line number : "+i);*/ 
                System.out.println("Start line number :"+startline+" End line number:" +i);
                sum =0;
                startline = 0;  // reset startline here
            }                                
        }            
于 2013-07-17T10:09:30.360 回答
3

初始化 1

int startline = 1;

并在打印后重置​​:

if(sum>=param2)
{                 
    /* System.out.println("line number : "+i);*/ 
    System.out.println("Start line number :"+startline+" End line number:" +i);
    sum =0;
    startline = i+1;
}  

(当然,删除startline=i你现在拥有的)

于 2013-07-17T10:09:43.027 回答