2014-07-15 222 views
1

导入大量单词,我需要创建能识别文件中每个单词的代码。我正在使用分隔符来识别每个单词的分隔,但我收到一个压缩错误,指出未使用linenumber和分隔符的值。我需要做些什么才能让程序读取该文件并分离该文件中的每个单词?识别文件中的每个单词

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); 
    } 
} 
+0

...这是所有的代码? – Pimgd

+0

@Pimgd编辑添加我的所有代码 – mrsw

+0

格式化它,因此它对我来说是可读的... – Pimgd

回答

1

这闻起来像功课,但没问题。

导入一大串单词,我需要创建代码来识别文件中的每个单词。我需要做些什么才能让程序读取该文件并分离该文件中的每个单词?

你需要......

  • 读取文件
  • 独立的话,你在
  • 上读到的东西......我不知道你想做什么与他们在那之后。我将把它们转储到一个大名单中。

我的主要方法是将的内容...

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 
    } 
} 
+0

不能正常工作..我在夏季休息,只是为了跟上夏天的编码,这样我就不会在编程的秋天生锈了。 。谢谢你的帮助我会试试这个!! – mrsw

+0

啊,好吧。如果你需要解释这篇文章中的任何内容,请告诉我。 – Pimgd

+0

完美的解释和它的工作!谢谢!! – mrsw

0

您可以使用String类

String[] split(String regex) 

的拆分方法,这将返回您可以直接处理的变换中,你可能需要的任何其他集合字符串数组。

我建议也除去suppresswarning,除非你确定你在做什么。在大多数情况下,最好是消除警告的原因,而不是压低警告。

0

我使用thenewboston这个伟大的教程,当我开始读文件:https://www.youtube.com/watch?v=3RNYUKxAgmw

该视频似乎是您的最佳选择。它涵盖了如何保存数据的文件字。只需将字符串数据添加到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(); 
    } 
} 

有一件事是使用java util的扫描仪很好的是自动跳过单词之间的空格使得它易于使用和识别的话。

相关问题