Java 识别文件中的每个单词
导入一个大的单词列表,我需要创建代码来识别文件中的每个单词。我使用分隔符来识别每个单词之间的分隔,但我收到一个抑制错误,指出未使用linenumber和delimiter的值。我需要做什么才能让程序读取此文件并分离该文件中的每个单词Java 识别文件中的每个单词,java,arraylist,import,bufferedreader,filereader,Java,Arraylist,Import,Bufferedreader,Filereader,导入一个大的单词列表,我需要创建代码来识别文件中的每个单词。我使用分隔符来识别每个单词之间的分隔,但我收到一个抑制错误,指出未使用linenumber和delimiter的值。我需要做什么才能让程序读取此文件并分离该文件中的每个单词 public class ASCIIPrime { public final static String LOC = "C:\\english1.txt"; @SuppressWarnings("null") public static v
public class ASCIIPrime {
public final static String LOC = "C:\\english1.txt";
@SuppressWarnings("null")
public static void main(String[] args) throws IOException {
//import list of words
@SuppressWarnings("resource")
BufferedReader File = new BufferedReader(new FileReader(LOC));
//Create a temporary ArrayList to store data
ArrayList<String> temp = new ArrayList<String>();
//Find number of lines in txt file
String line;
while ((line = File.readLine()) != null)
{
temp.add(line);
}
//Identify each word in file
int lineNumber = 0;
lineNumber++;
String delimiter = "\t";
//assess each character in the word to determine the ascii value
int total = 0;
for (int i=0; i < ((String) line).length(); i++)
{
char c = ((String) line).charAt(i);
total += c;
}
System.out.println ("The total value of " + line + " is " + total);
}
}
公共类AsciPrime{
公共最终静态字符串LOC=“C:\\english1.txt”;
@抑制警告(“空”)
公共静态void main(字符串[]args)引发IOException{
//导入单词列表
@抑制警告(“资源”)
BufferedReader文件=新的BufferedReader(新文件读取器(LOC));
//创建临时ArrayList以存储数据
ArrayList temp=新的ArrayList();
//查找txt文件中的行数
弦线;
而((line=File.readLine())!=null)
{
温度添加(行);
}
//识别文件中的每个单词
int lineNumber=0;
lineNumber++;
字符串分隔符=“\t”;
//评估单词中的每个字符以确定ascii值
int-total=0;
对于(int i=0;i<((字符串)行).length();i++)
{
charc=((字符串)行).charAt(i);
总数+=c;
}
System.out.println(““+行+”的总值为“+总值”);
}
}
您可以使用String类的split方法
String[] split(String regex)
这将返回一个字符串数组,您可以直接处理该数组,并将其转换为您可能需要的任何其他集合
我还建议删除suppresswarning,除非您确定自己在做什么。在大多数情况下,消除警告的原因比消除警告要好。当我开始阅读文件时,我使用了NewBoston中的这本很棒的教程: 这个视频似乎非常适合你。它涵盖了如何保存数据的文件字。只需将字符串数据添加到ArrayList。以下是您的代码的外观:
import java.io.*;
import java.util.*;
public class ReadFile {
static Scanner x;
static ArrayList<String> temp = new ArrayList<String>();
public static void main(String args[]){
openFile();
readFile();
closeFile();
}
public static void openFile(){
try(
x = new Scanner(new File("yourtextfile.txt");
}catch(Exception e){
System.out.println(e);
}
}
public static void readFile(){
while(x.hasNext()){
temp.add(x.next());
}
}
public void closeFile(){
x.close();
}
}
import java.io.*;
导入java.util.*;
公共类读取文件{
静态扫描仪x;
静态ArrayList temp=新ArrayList();
公共静态void main(字符串参数[]){
openFile();
readFile();
closeFile();
}
公共静态void openFile(){
试一试(
x=新扫描仪(新文件(“yourtextfile.txt”);
}捕获(例外e){
系统输出打印ln(e);
}
}
公共静态void readFile(){
while(x.hasNext()){
临时添加(x.next());
}
}
公共文件(){
x、 close();
}
}
使用java util scanner的一个好处是,它可以自动跳过单词之间的空格,从而使使用和识别单词变得容易。这听起来像是家庭作业,但没关系 导入一个大的单词列表,我需要创建代码来识别文件中的每个单词。我需要做什么才能让程序读取此文件并分离该文件中的每个单词
public class ASCIIPrime {
public final static String LOC = "C:\\english1.txt";
@SuppressWarnings("null")
public static void main(String[] args) throws IOException {
//import list of words
@SuppressWarnings("resource")
BufferedReader File = new BufferedReader(new FileReader(LOC));
//Create a temporary ArrayList to store data
ArrayList<String> temp = new ArrayList<String>();
//Find number of lines in txt file
String line;
while ((line = File.readLine()) != null)
{
temp.add(line);
}
//Identify each word in file
int lineNumber = 0;
lineNumber++;
String delimiter = "\t";
//assess each character in the word to determine the ascii value
int total = 0;
for (int i=0; i < ((String) line).length(); i++)
{
char c = ((String) line).charAt(i);
total += c;
}
System.out.println ("The total value of " + line + " is " + total);
}
}
你需要
- 读文件
- 把你读到的单词和你读到的分开
- …我不知道在那之后你想对他们做什么。我会把他们排在一个大名单上
BufferedReader File = new BufferedReader(new FileReader(LOC));//LOC is defined as class variable
//Create an ArrayList to store the words
List<String> words = new ArrayList<String>();
String line;
String delimiter = "\t";
while ((line = File.readLine()) != null)//read the file
{
String[] wordsInLine = line.split(delimiter);//separate the words
//delimiter could be a regex here, gotta watch out for that
for(int i=0, isize = wordsInLine.length(); i < isize; i++){
words.add(wordsInLine[i]);//put them in a list
}
}
BufferedReader File=new BufferedReader(new FileReader(LOC));//LOC定义为类变量
//创建一个ArrayList来存储单词
List words=new ArrayList();
弦线;
字符串分隔符=“\t”;
while((line=File.readLine())!=null)//读取文件
{
String[]wordsInLine=line.split(分隔符);//分隔单词
//分隔符可能是正则表达式,要小心
for(int i=0,isize=wordsInLine.length();i
…这是您的全部代码吗?@Pimgd已编辑以添加我的所有代码格式化,以便我阅读…仅供参考,您将收到某些变量未使用的警告(您可能正在递增或分配值给它们,但从未读回值。这本身不是错误,而是编译器告诉您程序中存在潜在错误,或者有一些多余的代码可以删除。您的程序将在出现警告的情况下成功编译,但可能无法正常工作。Not作业..我正在放暑假,只是想在暑假里跟上编码的进度,这样我就不会在秋天对编程II生疏了。谢谢你的帮助,我会尝试一下!!啊,好的。如果你需要解释这篇文章中的任何内容,请让我知道。