Java程序,用于计算给定文本文件中的行、字和字符数
我正在练习编写一个程序,从用户那里获取文本文件,并提供文本中的字符、单词和行等数据 我已经搜索并查看了同一主题,但找不到让代码运行的方法Java程序,用于计算给定文本文件中的行、字和字符数,java,Java,我正在练习编写一个程序,从用户那里获取文本文件,并提供文本中的字符、单词和行等数据 我已经搜索并查看了同一主题,但找不到让代码运行的方法 public class Document{ private Scanner sc; // Sets users input to a file name public Document(String documentName) throws FileNotFoundException { File inputFile = new File(docu
public class Document{
private Scanner sc;
// Sets users input to a file name
public Document(String documentName) throws FileNotFoundException {
File inputFile = new File(documentName);
try {
sc = new Scanner(inputFile);
} catch (IOException exception) {
System.out.println("File does not exists");
}
}
public int getChar() {
int Char= 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
Char += line.length() + 1;
}
return Char;
}
// Gets the number of words in a text
public int getWords() {
int Words = 0;
while (sc.hasNext()) {
String line = sc.next();
Words += new StringTokenizer(line, " ,").countTokens();
}
return Words;
}
public int getLines() {
int Lines= 0;
while (sc.hasNextLine()) {
Lines++;
}
return Lines;
}
}
主要方法:
public class Main {
public static void main(String[] args) throws FileNotFoundException {
DocStats doc = new DocStats("someText.txt");
// outputs 1451, should be 1450
System.out.println("Number of characters: "
+ doc.getChar());
// outputs 0, should be 257
System.out.println("Number of words: " + doc.getWords());
// outputs 0, should be 49
System.out.println("Number of lines: " + doc.getLines());
}
}
我很清楚为什么我得到的是1451而不是1451。原因是我在最后一句末尾没有“\n”,但我的方法添加了
numChars+=line.length()+1
但是,我无法找到一个解决方案,来解释为什么单词和行的值为0。
*我的文本包括以下元素:?,-'
毕竟,有谁能帮我做这件事吗
**到目前为止,我所关心的问题是,如果最后一句没有“\n”元素,如何获得大量字符。我有没有可能用if语句来解决这个问题
-谢谢大家! 在doc.getChar()
之后,您已到达文件末尾。所以在这个文件里没有更多的东西可以读了
您应该在getChar/Words/Lines
方法中重置扫描仪,例如:
public int getChar() {
sc = new Scanner(inputFile);
...
// solving your problem with the last '\n'
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (sc.hasNextLine())
Char += line.length() + 1;
else
Char += line.length();
}
return char;
}
请注意,行尾并不总是\n
!它也可能是\r\n
(尤其是在windows下)
我将使用一次扫描来计算所有3个,使用不同的计数器。在每个字符上循环一次,检查是否是新词等,增加计数,使用character.isWhiteSpace*
import java.io.*;
/**Cound lines, characters and words Assumes all non white space are words so even () is a word*/
public class ChrCounts{
String data;
int chrCnt;
int lineCnt;
int wordCnt;
public static void main(String args[]){
ChrCounts c = new ChrCounts();
try{
InputStream data = null;
if(args == null || args.length < 1){
data = new ByteArrayInputStream("quick brown foxes\n\r new toy\'s a fun game.\nblah blah.la la ga-ma".getBytes("utf-8"));
}else{
data = new BufferedInputStream( new FileInputStream(args[0]));
}
c.process(data);
c.print();
}catch(Exception e){
System.out.println("ee " + e);
e.printStackTrace();
}
}
public void print(){
System.out.println("line cnt " + lineCnt + "\nword cnt " + wordCnt + "\n chrs " + chrCnt);
}
public void process(InputStream data) throws Exception{
int chrCnt = 0;
int lineCnt = 0;
int wordCnt = 0;
boolean inWord = false;
boolean inNewline = false;
//char prev = ' ';
while(data.available() > 0){
int j = data.read();
if(j < 0)break;
chrCnt++;
final char c = (char)j;
//prev = c;
if(c == '\n' || c == '\r'){
chrCnt--;//some editors do not count line seperators as new lines
inWord = false;
if(!inNewline){
inNewline = true;
lineCnt++;
}else{
//chrCnt--;//some editors dont count adjaccent line seps as characters
}
}else{
inNewline = false;
if(Character.isWhitespace(c)){
inWord = false;
}else{
if(!inWord){
inWord = true;
wordCnt++;
}
}
}
}
//we had some data and last char was not in new line, count last line
if(chrCnt > 0 && !inNewline){
lineCnt++;
}
this.chrCnt = chrCnt;
this.lineCnt = lineCnt;
this.wordCnt = wordCnt;
}
}
import java.io.*;
/**共行、字符和单词假定所有非空白都是单词,所以偶数()是单词*/
公共类计数{
字符串数据;
int chrCnt;
int lineCnt;
int-wordCnt;
公共静态void main(字符串参数[]){
ChrCounts c=新的ChrCounts();
试一试{
InputStream数据=null;
如果(args==null | | args.length<1){
data=newbytearrayinputstream(“快速棕色狐狸”\n\r新玩具是一款有趣的游戏。\nblah blah.la la la ga ma).getBytes(“utf-8”);
}否则{
数据=新的BufferedInputStream(新的FileInputStream(args[0]);
}
c、 过程(数据);
c、 打印();
}捕获(例外e){
系统输出打印项次(“ee”+e);
e、 printStackTrace();
}
}
公开作废印刷品(){
System.out.println(“行cnt”+lineCnt+“\nword cnt”+wordCnt+“\n chrs”+chrCnt);
}
公共无效进程(InputStream数据)引发异常{
int chrCnt=0;
int-lineCnt=0;
int-wordCnt=0;
布尔inWord=false;
布尔inNewline=false;
//char prev='';
while(data.available()>0){
int j=data.read();
如果(j<0)断裂;
chrCnt++;
最终字符c=(字符)j;
//prev=c;
如果(c=='\n'| | c=='\r'){
chrCnt--;//某些编辑器不将行分隔符计为新行
inWord=false;
if(!inNewline){
inNewline=真;
lineCnt++;
}否则{
//chrCnt--;//某些编辑器不将ADJ强调线SEP计算为字符
}
}否则{
inNewline=假;
if(字符.isWhitespace(c)){
inWord=false;
}否则{
如果(!inWord){
inWord=true;
wordCnt++;
}
}
}
}
//我们有一些数据,最后一个字符不在新行中,请计算最后一行
如果(chrCnt>0&&!在新线路中){
lineCnt++;
}
this.chrCnt=chrCnt;
this.lineCnt=lineCnt;
this.wordCnt=wordCnt;
}
}
请显示您的实际代码。您正在调用代码中不存在的getNumberOfCharacters方法,因此它甚至不会编译,更不用说运行:不要使用StringTokenizer。这是一个遗留类,不应使用。使用字符串的拆分方法或正则表达式。新的StringTokenizer(第,“,”)将在“,”上拆分,因此“我在这里”不会真正包含单词。这是我的实际代码。我之所以调用不同的方法,是因为我运行了许多类似的代码,以了解如何修复自己的方法。这就是我忘记将其更改回我的方法的原因。如果您要调试最后两个方法,或者您正在进入循环,否则您将继续得到0,由于扫描已经完成,这实际上帮助我获得了大量的单词,而不是零。它应该为您提供正确的行数,您可能需要在单词方法上做一些工作;)是的,在这一点上,我得到了一个错误的字符和单词数。我的最后一句话结尾没有“\n”,因此我当前的算法不起作用。我想使用if语句以某种方式修复它。你能给我一些建议吗?我已经根据你的getChar问题编辑了我的帖子
import java.io.*;
/**Cound lines, characters and words Assumes all non white space are words so even () is a word*/
public class ChrCounts{
String data;
int chrCnt;
int lineCnt;
int wordCnt;
public static void main(String args[]){
ChrCounts c = new ChrCounts();
try{
InputStream data = null;
if(args == null || args.length < 1){
data = new ByteArrayInputStream("quick brown foxes\n\r new toy\'s a fun game.\nblah blah.la la ga-ma".getBytes("utf-8"));
}else{
data = new BufferedInputStream( new FileInputStream(args[0]));
}
c.process(data);
c.print();
}catch(Exception e){
System.out.println("ee " + e);
e.printStackTrace();
}
}
public void print(){
System.out.println("line cnt " + lineCnt + "\nword cnt " + wordCnt + "\n chrs " + chrCnt);
}
public void process(InputStream data) throws Exception{
int chrCnt = 0;
int lineCnt = 0;
int wordCnt = 0;
boolean inWord = false;
boolean inNewline = false;
//char prev = ' ';
while(data.available() > 0){
int j = data.read();
if(j < 0)break;
chrCnt++;
final char c = (char)j;
//prev = c;
if(c == '\n' || c == '\r'){
chrCnt--;//some editors do not count line seperators as new lines
inWord = false;
if(!inNewline){
inNewline = true;
lineCnt++;
}else{
//chrCnt--;//some editors dont count adjaccent line seps as characters
}
}else{
inNewline = false;
if(Character.isWhitespace(c)){
inWord = false;
}else{
if(!inWord){
inWord = true;
wordCnt++;
}
}
}
}
//we had some data and last char was not in new line, count last line
if(chrCnt > 0 && !inNewline){
lineCnt++;
}
this.chrCnt = chrCnt;
this.lineCnt = lineCnt;
this.wordCnt = wordCnt;
}
}