Java 对文件中的字符、单词和行进行计数
这应该计算文件中的行数、字数和字符数 但它不起作用。从输出中,它仅显示Java 对文件中的字符、单词和行进行计数,java,count,output,Java,Count,Output,这应该计算文件中的行数、字数和字符数 但它不起作用。从输出中,它仅显示0 代码: public static void main(String[] args) throws IOException { int ch; boolean prev = true; //counters int charsCount = 0; int wordsCount = 0; int linesCount = 0; Scanner in =
0
代码:
public static void main(String[] args) throws IOException {
int ch;
boolean prev = true;
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner in = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
}
// count the characters of the file till the end
while(in.hasNext()) {
ch = in.next().charAt(0);
if (ch != ' ') ++charsCount;
if (!prev && ch == ' ') ++wordsCount;
// don't count if previous char is space
if (ch == ' ')
prev = true;
else
prev = false;
if (ch == '\n') ++linesCount;
}
//display the count of characters, words, and lines
charsCount -= linesCount * 2;
wordsCount += linesCount;
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
in.close();
}
我不明白发生了什么事。
有什么建议吗?您的代码只查看文件中默认标记(单词)的前几个字符
当您执行此操作时,
ch=in.next().charAt(0)
,它将获取令牌(单词)的第一个字符,扫描程序将前进到下一个令牌(跳过该令牌的其余部分)。您的代码只查看文件中默认令牌(单词)的第一个字符
当您执行此操作时,
ch=in.next().charAt(0)
,它将获取令牌(单词)的第一个字符,扫描仪将前进到下一个令牌(跳过该令牌的其余部分)。您可以将每一行存储在列表中,然后linescont=List.size()
计算charscont
:
for(final String line : lines)
charsCount += line.length();
for(final String line : lines)
wordsCount += line.split(" +").length;
计算wordscont
:
for(final String line : lines)
charsCount += line.length();
for(final String line : lines)
wordsCount += line.split(" +").length;
将这些计算组合在一起可能是一个明智的想法,而不是将它们分开进行。您可以将每一行存储在列表中,然后linescont=List.size()
计算charscont
:
for(final String line : lines)
charsCount += line.length();
for(final String line : lines)
wordsCount += line.split(" +").length;
计算wordscont
:
for(final String line : lines)
charsCount += line.length();
for(final String line : lines)
wordsCount += line.split(" +").length;
将这些计算组合在一起可能是一个明智的想法,而不是单独进行。使用扫描仪方法:
int lines = 0;
int words = 0;
int chars = 0;
while(in.hasNextLine()) {
lines++;
Scanner lineScanner = new Scanner(in.nextLine());
lineScanner.useDelimiter(" ");
while(lineScanner.hasNext()) {
words++;
chars += lineScanner.next().length();
}
}
使用扫描仪
方法:
int lines = 0;
int words = 0;
int chars = 0;
while(in.hasNextLine()) {
lines++;
Scanner lineScanner = new Scanner(in.nextLine());
lineScanner.useDelimiter(" ");
while(lineScanner.hasNext()) {
words++;
chars += lineScanner.next().length();
}
}
不同的方法。使用字符串查找行数、字数和字符数:
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner in = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
}
while (in.hasNext()) {
String tmpStr = in.nextLine();
if (!tmpStr.equalsIgnoreCase("")) {
String replaceAll = tmpStr.replaceAll("\\s+", "");
charsCount += replaceAll.length();
wordsCount += tmpStr.split(" ").length;
}
++linesCount;
}
//display the count of characters, words, and lines
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
in.close();
}
注意:
对于其他编码样式,使用新扫描仪(新文件(selectedFile),“####”)代码>代替新扫描仪(选择文件)代码>
###
是所需的字符集。参考和不同的方法。使用字符串查找行数、字数和字符数:
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner in = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
}
while (in.hasNext()) {
String tmpStr = in.nextLine();
if (!tmpStr.equalsIgnoreCase("")) {
String replaceAll = tmpStr.replaceAll("\\s+", "");
charsCount += replaceAll.length();
wordsCount += tmpStr.split(" ").length;
}
++linesCount;
}
//display the count of characters, words, and lines
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
in.close();
}
注意:
对于其他编码样式,使用新扫描仪(新文件(selectedFile),“####”)代码>代替新扫描仪(选择文件)代码>
###
是所需的字符集。参考和看起来每个人都在建议你另一种选择
您的逻辑的缺陷是,您没有循环遍历整行的所有字符。您只是在每行的第一个字符之间循环
ch = in.next().charAt(0);
另外,2在charscont-=linescont*2中是什么代码>表示
在访问文件时,您可能还希望包含try-catch块
try {
in = new Scanner(selectedFile);
} catch (FileNotFoundException e) {}
看起来每个人都在建议你另一种选择
您的逻辑的缺陷是,您没有循环遍历整行的所有字符。您只是在每行的第一个字符之间循环
ch = in.next().charAt(0);
另外,2在charscont-=linescont*2中是什么代码>表示
在访问文件时,您可能还希望包含try-catch块
try {
in = new Scanner(selectedFile);
} catch (FileNotFoundException e) {}
这里有几个问题
首先,行尾的测试会导致问题,因为它通常不是一个表示行尾的字符。有关此问题的更多详细信息,请阅读
单词之间的空白字符可以不仅仅是ASCII 32(空格)值。将标签视为一种情况。您很可能需要检查Character.isWhitespace()
您还可以使用在中找到的两个扫描仪解决线端问题
下面是对您提供的代码以及输入和输出的快速破解
import java.io.*;
import java.util.Scanner;
import javax.swing.JFileChooser;
public final class TextApp {
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner fileScanner = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
fileScanner = new Scanner(selectedFile);
}
while (fileScanner.hasNextLine()) {
linesCount++;
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
// count the characters of the file till the end
while(lineScanner.hasNext()) {
wordsCount++;
String word = lineScanner.next();
charsCount += word.length();
}
lineScanner.close();
}
//display the count of characters, words, and lines
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
fileScanner.close();
}
}
以下是测试文件输入:
$ cat ../test.txt
test text goes here
and here
以下是输出:
$ javac TextApp.java
$ java TextApp
# of chars: 23
# of words: 6
# of lines: 2
$ wc test.txt
2 6 29 test.txt
字符数之间的差异是由于不计算空白字符,这似乎是您在原始代码中尝试执行的操作
我希望这能有所帮助。你这里有几个问题
首先,行尾的测试会导致问题,因为它通常不是一个表示行尾的字符。有关此问题的更多详细信息,请阅读
单词之间的空白字符可以不仅仅是ASCII 32(空格)值。将标签视为一种情况。您很可能需要检查Character.isWhitespace()
您还可以使用在中找到的两个扫描仪解决线端问题
下面是对您提供的代码以及输入和输出的快速破解
import java.io.*;
import java.util.Scanner;
import javax.swing.JFileChooser;
public final class TextApp {
public static void main(String[] args) throws IOException {
//counters
int charsCount = 0;
int wordsCount = 0;
int linesCount = 0;
Scanner fileScanner = null;
File selectedFile = null;
JFileChooser chooser = new JFileChooser();
// choose file
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
fileScanner = new Scanner(selectedFile);
}
while (fileScanner.hasNextLine()) {
linesCount++;
String line = fileScanner.nextLine();
Scanner lineScanner = new Scanner(line);
// count the characters of the file till the end
while(lineScanner.hasNext()) {
wordsCount++;
String word = lineScanner.next();
charsCount += word.length();
}
lineScanner.close();
}
//display the count of characters, words, and lines
System.out.println("# of chars: " + charsCount);
System.out.println("# of words: " + wordsCount);
System.out.println("# of lines: " + linesCount);
fileScanner.close();
}
}
以下是测试文件输入:
$ cat ../test.txt
test text goes here
and here
以下是输出:
$ javac TextApp.java
$ java TextApp
# of chars: 23
# of words: 6
# of lines: 2
$ wc test.txt
2 6 29 test.txt
字符数之间的差异是由于不计算空白字符,这似乎是您在原始代码中尝试执行的操作
我希望这能有所帮助。也许我的代码能帮助你……一切都正常
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class LineWordChar {
public static void main(String[] args) throws IOException {
// Convert our text file to string
String text = new Scanner( new File("way to your file"), "UTF-8" ).useDelimiter("\\A").next();
BufferedReader bf=new BufferedReader(new FileReader("way to your file"));
String lines="";
int linesi=0;
int words=0;
int chars=0;
String s="";
// while next lines are present in file int linesi will add 1
while ((lines=bf.readLine())!=null){
linesi++;}
// Tokenizer separate our big string "Text" to little string and count them
StringTokenizer st=new StringTokenizer(text);
while (st.hasMoreTokens()){
`enter code here` s = st.nextToken();
words++;
// We take every word during separation and count number of char in this words
for (int i = 0; i < s.length(); i++) {
chars++;}
}
System.out.println("Number of lines: "+linesi);
System.out.println("Number of words: "+words);
System.out.print("Number of chars: "+chars);
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.Scanner;
导入java.util.StringTokenizer;
公共类LineWordChar{
公共静态void main(字符串[]args)引发IOException{
//将文本文件转换为字符串
字符串文本=新扫描仪(新文件(“文件路径”),“UTF-8”)。使用分隔符(\\A”)。下一步();
BufferedReader bf=新的BufferedReader(新的文件阅读器(“文件之路”));
字符串行=”;
int-linesi=0;
int字=0;
int chars=0;
字符串s=“”;
//当文件int linesi中存在下一行时,我将添加1
而((lines=bf.readLine())!=null){
linesi++;}
//标记器将大字符串“文本”分隔为小字符串,并对它们进行计数
StringTokenizer st=新的StringTokenizer(文本);
而(st.hasMoreTokens()){
`在此处输入代码`s=st.nextToken();
words++;
//我们在分离过程中提取每个单词,并计算这些单词中的字符数
对于(int i=0;i
也许我的代码会对你有所帮助