Home > Enterprise >  Read a text file and count the number of white spaces in the text file
Read a text file and count the number of white spaces in the text file

Time:09-27

I need my program in Java to get a text file, read that text file, count and reproduce the amount of white spaces in that text file

Here is the code I currently have

import java.io.File;  //import the file class
import java.io.FileNotFoundException; //import this class to handle errors
import java.util.Scanner; //import the scanner class to read the file

public class whitespaceReader {

    public static void main(String[] args) {
        try {

            File myFile = new File("aliceInWonderland.txt");

            Scanner reader = new Scanner(myFile);

            int space = 0;
            int i = 0;
            String word;

            word = reader.nextLine();

            while (i < word.length()){
                char a = word.charAt(i);

                if(a == '//tried to use empty brackets here, did not work') {
                    space  ;
                }
                i  ;
            }
            System.out.println("amount of white space is: "   space);

        } catch (FileNotFoundException e) {
            System.out.print("Uh oh! Something went wrong");
            e.printStackTrace();
        }
    }
}

CodePudding user response:

Use BufferedReader to readLines like this.

final String fileName = "Path to your file"
final char countedChar = ' ';
int characterCount = 0;
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = null;
while((line = br.readLine()) != null){
 for(int i = 0; i < line.length(); i  ){
   if(countedChar == line.charAt(i)){
     characterCount  ;
   }
 }
}
System.out.println(characterCount);

CodePudding user response:

You can just do it like this

public static void main(String[] args) {
    try {

        File myFile = new File("aliceInWonderland.txt");

        Scanner reader = new Scanner(myFile);

        int space = 0;
        int n = 0;
        String word = "";

        while(reader.hasNextLine()){
            word  = reader.nextLine();
        }
        n = word.length();

        space = word.replace(" ","").length();
        space = n - space;
        System.out.println("amount of white space is: "   space);

    } catch (FileNotFoundException e) {
        System.out.print("Uh oh! Something went wrong");
        e.printStackTrace();
    }
}

You just need to subtract the length of the sentence with the length of the sentence without whitespaces.

No need for loops

CodePudding user response:

If you are using class java.util.Scanner and you are counting only the space characters, i.e. the character whose Unicode code point is 32 (decimal), and you are using at least JDK 9, then you can simply call method findAll and count the number of elements in the stream returned by that method.

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class WhitespaceReader {

    public static void main(String[] args) {
        Path path = Paths.get("aliceInWonderland.txt");
        try (Scanner reader = new Scanner(path)) {
            long space = reader.findAll(" ")
                               .count();
            System.out.println("amount of white space is: "   space);
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

Note that the above code uses try-with-resources to ensure that the Scanner is closed so as to prevent resource leaks.

Also note that I changed the class name to WhitespaceReader so as to adhere to java naming conventions.

If you want to count all whitespace characters, simply change the string argument (to method findAll) to \\s . Refer to javadoc for class java.util.regex.Pattern

  • Related