读取文本文件并计算文本文件中的空格数
Read a text file and count the number of white spaces in the text file
我需要 Java 中的程序来获取一个文本文件,读取该文本文件,计算并重现该文本文件中的空格数量
这是我目前拥有的代码
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();
}
}
}
像这样使用 BufferedReader 读取行。
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);
你可以这样做
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();
}
}
只需要用不带空格的句子长度减去句子长度即可。
不需要循环
如果您使用 class java.util.Scanner
并且您只计算 space 个字符,即 Unicode 代码点为 32(十进制)的字符,并且您使用的是至少 JDK 9,那么你可以简单地调用方法 findAll 并计算该方法返回的流中元素的数量。
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();
}
}
}
注意上面的代码使用了try-with-resources来保证Scanner
关闭,防止资源泄露
另请注意,我将 class 名称更改为 WhitespaceReader
以遵守 java naming conventions。
如果要统计全部whitespace characters, simply change the string argument (to method findAll
) to \s+
. Refer to javadoc for class java.util.regex.Pattern
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class NumberOfSpacesInAFile {
public static long noOfSpaces = 0;
public static void main(String[] args) throws IOException {
Files.lines(Paths.get("file.txt"))
.map(lines->{noOfSpaces = noOfSpaces + lines.chars().filter(eachChar->eachChar==32).count();return lines;})
.count();
System.out.println(noOfSpaces);
}
}
我需要 Java 中的程序来获取一个文本文件,读取该文本文件,计算并重现该文本文件中的空格数量
这是我目前拥有的代码
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();
}
}
}
像这样使用 BufferedReader 读取行。
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);
你可以这样做
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();
}
}
只需要用不带空格的句子长度减去句子长度即可。
不需要循环
如果您使用 class java.util.Scanner
并且您只计算 space 个字符,即 Unicode 代码点为 32(十进制)的字符,并且您使用的是至少 JDK 9,那么你可以简单地调用方法 findAll 并计算该方法返回的流中元素的数量。
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();
}
}
}
注意上面的代码使用了try-with-resources来保证Scanner
关闭,防止资源泄露
另请注意,我将 class 名称更改为 WhitespaceReader
以遵守 java naming conventions。
如果要统计全部whitespace characters, simply change the string argument (to method findAll
) to \s+
. Refer to javadoc for class java.util.regex.Pattern
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class NumberOfSpacesInAFile {
public static long noOfSpaces = 0;
public static void main(String[] args) throws IOException {
Files.lines(Paths.get("file.txt"))
.map(lines->{noOfSpaces = noOfSpaces + lines.chars().filter(eachChar->eachChar==32).count();return lines;})
.count();
System.out.println(noOfSpaces);
}
}