Java 完成文件类
我不断收到一个错误,告诉我无法将行号解析为变量?我真的不知道如何准确地解决这个问题。我不是将某个文件导入到java中来帮助实现这一点吗 还有,我如何计算有空格和没有空格的字符数 此外,我需要一种方法来计算独特的单词,但我不确定什么是独特的单词Java 完成文件类,java,file,tokenize,Java,File,Tokenize,我不断收到一个错误,告诉我无法将行号解析为变量?我真的不知道如何准确地解决这个问题。我不是将某个文件导入到java中来帮助实现这一点吗 还有,我如何计算有空格和没有空格的字符数 此外,我需要一种方法来计算独特的单词,但我不确定什么是独特的单词 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Scan
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.List;
public class LineWordChar {
public 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()){
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);
}
}
abstract class WordCount {
/**
* @return HashMap a map containing the Character count, Word count and
* Sentence count
* @throws FileNotFoundException
*
*/
public static void main() throws FileNotFoundException {
lineNumber=2; // as u want
File f = null;
ArrayList<Integer> list=new ArrayList<Integer>();
f = new File("file_stats.txt");
Scanner sc = new Scanner(f);
int totalLines=0;
int totalWords=0;
int totalChars=0;
int totalSentences=0;
while(sc.hasNextLine())
{
totalLines++;
if(totalLines==lineNumber){
String line = sc.nextLine();
totalChars += line.length();
totalWords += new StringTokenizer(line, " ,").countTokens(); //line.split("\\s").length;
totalSentences += line.split("\\.").length;
break;
}
sc.nextLine();
}
list.add(totalChars);
list.add(totalWords);
list.add(totalSentences);
System.out.println(lineNumber+";"+totalWords+";"+totalChars+";"+totalSentences);
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.Scanner;
导入java.util.StringTokenizer;
导入java.util.ArrayList;
导入java.util.List;
公共类LineWordChar{
public 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=圣奈克特肯();
words++;
//我们在分离过程中提取每个单词,并计算这些单词中的字符数
对于(int i=0;i
应使用数据类型声明lineNumber变量
int lineNumber=2;//随你的便
通过设置数据类型,将main方法中的第一行从just lineNumber更改为int lineNumber=2,因为在Java中设置每个变量的数据类型很重要。为了让代码运行,您必须至少做两个更改: 替换:
lineNumber=2; // as u want
与
另外,您需要修改main
方法,您不能在main
方法声明中抛出异常,因为上面没有任何内容可以捕获异常,您必须在其内部处理异常:
public static void main(String[] args) {
// Convert our text file to string
try {
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()) {
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);
} catch (Exception e) {
e.printStackTrace();
}
}
publicstaticvoidmain(字符串[]args){
//将文本文件转换为字符串
试一试{
字符串文本=新扫描仪(新文件(“文件路径”),“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=圣奈克特肯();
words++;
//我们在分离过程中提取每个单词,并计算这些单词中的字符数
对于(int i=0;i
我使用了一个全局
Exception
catch,您可以在几个catch中分离expection,以便分别处理它们。它给了我一个异常,告诉我一个明显的FileNotFoundException
,除此之外,您的代码现在正在运行。发布准确的错误消息,请尝试将您的问题限制为一个问题。我需要添加什么来计算有空格和无空格的字符数?我不确定您的要求是什么,基本上是计算文件中文本之间的空格。我想这是另一个问题。计算字符串中单词的简单方法可以是string.length()-string.replaceAll(“,”).length()
public static void main(String[] args) {
// Convert our text file to string
try {
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()) {
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);
} catch (Exception e) {
e.printStackTrace();
}
}