CSV 파일에 문자 &로 시작하는 라인의 수를 계산

Tagyoureit :

내가 함께 및 주어진 파일에서 시작 얼마나 많은 라인을 계산하는 함수를 만들려고 노력하고 있습니다. 지금까지 나는 다음과 같은 기능을 함께했다

  public int CountNumberOfTexts(String filename)  {

        try{

            File file = new File(filename);

            if(file.exists()){

                FileReader fr = new FileReader(file);
                LineNumberReader lnr = new LineNumberReader(fr);

                int linenumber = 0;

                while (lnr.readLine() != null){
                    if (lnr.readLine().substring(0,1) == "&") {
                        linenumber++;
                    }
                }

                Log.d("Count", "NUMBER OF LINES: " + linenumber);

                lnr.close();

                return linenumber;

            }else{
                System.out.println("File does not exists: " + filename);
            }

        }catch(IOException e){
            e.printStackTrace();
        }

        return 0;
    }

현재 기능 오류는 다음과 같습니다 인식하지 라인과 및 문자 시작.

s.fuhrm :

당신은 두 가지 문제에 직면하고있다 :

  1. 당신은 두 줄에 읽기,하지만 초마다 평가하고 있습니다 :

    while (lnr.readLine() != null){  <- first consumption
    if (lnr.readLine().substring(0,1) == "&") { <- second
    
  2. 당신이 가진 문자열을 비교하는 ==연산자 대신 equals방법을. 아니면 경우에 당신도 사용할 수 있습니다 startsWith당신 같은 시나리오에 대해 정확하게 생성 방법.

이 트릭을 할 것입니다 :

String line;
while ((line = lnr.readLine()) != null){
    if (line.startsWith("&")) {

추천

출처http://43.154.161.224:23101/article/api/json?id=19273&siteId=1