How do I get the CountLines function (in Java) to output the last number to a file?

I have the following function that counts lines in a simple file. This file consists of integers separated by newlines, but the problem is that it goes one character at a time. And if the last line has 2 digits, it will return only the second digit as the final result:

Here is my code:

public static int countLines(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));


    try {
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        int lastline = 0;

       boolean empty = true;
       while (  (readChars = is.read(c))  != -1) {

         //System.out.println (" The array is + readChars " + readChars + " !!!" );

            for (int i = 0; i < readChars; ++i){
                Byte b  = c[i];
                int xx = b.intValue();
                lastLine =  xx; 

                if (c[i] == '\n'){
                    ++count;
                    empty = true;

                } else {

                    empty  = false;
                }      
            }   // END inner- FOR-LOOP  

       }// END WHILE-LOOP
        if (!empty) {
        count++;
       }


   int asciiVal  = lastLine;
   int lastLine2 = Character.getNumericValue(asciiVal);
   System.out.println("the last line was "  + lastLine2);

   return count;


    } finally {
        is.close();
    }

}//END method countLines

Here is an example of a text input file that it reads:

1
2
3
4
5
6
7
8
9
17

any tips appreciated. I want to better learn Java encoding, IO, etc. Related to this. thanks

+4
source share
2 answers

I prepared a solution that is probably closest to yours (in terms of distance editing)

import java.io.*;

public class Main {

    public static int countLines(String filename) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(filename));


        try {
            byte[] c = new byte[1024];
            int count = 0;
            int readChars = 0;
            int lastLine = 0;
            int lastNumber = 0;

            boolean empty = true;
            while (  (readChars = is.read(c))  != -1) {

                for (int i = 0; i < readChars; ++i){
                    Byte b  = c[i];
                    lastLine = b.intValue() - 48;

                    if (empty) {
                        lastNumber = 0;
                    }

                    if (c[i] == '\n'){
                        ++count;
                        empty = true;

                    } else {
                        lastNumber *= 10;
                        lastNumber += lastLine;
                        empty  = false;
                    }
                }   // END inner- FOR-LOOP

            }// END WHILE-LOOP
            if (!empty) {
                count++;
            }


            int asciiVal  = lastLine;
            int lastLine2 = Character.getNumericValue(asciiVal);
            System.out.println("the last line was "  + lastNumber);

            return count;


        } finally {
            is.close();
        }

    }//END method countLines

    public static void main(String[] args) throws IOException {
        System.out.print(countLines("/tmp/test.txt"));
    }
}

What's going on here:

  • , char char
  • c[i] ASCII byte intValue() ASCII. "" , ASCII 0. , intValue() of '5' 53, 48 (intValue() of '0'), 5
  • " " , 10,
  • lastNumber 0

, . , ? , Scanner - . , CodeWars

+2

Scanner InputStream / .

Scanner scan = new Scanner(System.in);
// All your stuff up until the while loop
while (scan.hasNext()) {
     if (sc.nextLine().equals("")) break;
     System.out.println (" The array is + scan.nextInt() " + readChars + " !!!" );
     count++;
}

( ) ( , ). empty.

BufferedReader , Scanner, Scanner . InputStream .

EDIT: BufferedReader, . , try-catch (), BufferedReader.

    BufferedReader buff = new BufferedReader (new InputStreamReader(System.in));
    // All your stuff up until the while loop
    try {
        String read = buff.readLine();
        while (read.equals("") == false && read.equals("\n") == false) {

            int nextInt = Integer.parseInt(read);
            System.out.println (" The array is + scan.nextInt() " + nextInt + " !!!" );
             count++;
            read = buff.readLine();

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

, BufferedReader , .

. int, , int, , . , int, , .

+2

Source: https://habr.com/ru/post/1618049/


All Articles