简体   繁体   中英

Counting number of lines in .txt file Java

Hey I have problem with this simple program:

package werd;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;


public class Werd {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws FileNotFoundException, IOException {


    BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream("ikso.txt"), "UTF-8"));

    String line; 
    String[] tmp = null;
    int slowa = 0;
    int lines = 0;

    while ((line = in.readLine())!= null){

        lines++;
        tmp = line.split("\\s");
        System.out.println(line);
        //System.out.println(line);   

        for(String s : tmp){
            slowa++;
        }
    }
    in.close();

    System.out.println("Liczba wierszy to " +line+" a liczba slow w tekscie to " + slowa)

}


}

Problem is that the variable counting is not increasing. Moreover Netbeans telling me that variable limits is not used. I had glanced at similar questions as min on this page. Way of solving counting number of lines was similar to mine. I don't understand why it doesn't work... Thanks

From the code you provided it seems you're printing line instead of lines .

The counting code looks correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM