java - 将文件中的每个单词读取到数组

标签 java arrays file

我需要一些帮助,无论我尝试将其放入下面编写的代码中,它都不起作用。我希望它从文件 Alice.txt 中读取,然后将每个单词放入带有小写字母的数组 ordArray 中,然后使用程序的其余部分来计算每个单词、每个单词的每次出现以及每个唯一单词。拜托,请帮忙!如果您能给我一些关于我可以做得更好的提示,或者我应该如何实现将信息写入文件 Opplysning.txt 的部分,请不要保持安静。

try {
        File skrivFil = new File("Opplysning.txt");
        FileWriter fw= new FileWriter(skrivFil);
        BufferedWriter bw = new BufferedWriter(fw);

        Scanner lesFil = new Scanner("Alice.txt");
        int i=0;
        int totalOrd=0;
        int antUnikeOrd=0;

        String[] ordArray = new String[5000];
        int[] antallOrd = new int[5000];    
        String ord = lesFil.next().toLowerCase();
        totalOrd++;
        boolean ordFraFor=false;
        int y=0;
        int z=0;

        for(i=0; i<ordArray.length; i++) {
             if (ord.equals(ordArray[i])) {
        antallOrd[i]++;
        ordFraFor=true;
        }
    }
        if(ordFraFor=false) {
            antUnikeOrd++;
            y=0;
            boolean ordOpptelling=false;

        while(ordOpptelling=false) {
        if(ordArray[y] == null) {
            ordArray[y] = ord;
            antallOrd[y]++;
            ordOpptelling=true;
        }
        y++;
    }
}

        for(String s: ordArray) {
        System.out.println(s);
        }
        lesFil.close();

        } catch (Exception e){
        System.out.print(e);
        }
    }
}

编辑: 尝试添加文件,我想它有效,但我仍然无法写入数组。我真的不擅长这个,这就是为什么我必须用下周的时间来真正把这些东西放进去...... 无论如何,我尝试将所有单词添加到数组列表中,我希望这能起作用。它没有,它给了我一个 nosuchelementException 。 部分代码:

File tekstFil = new File ("Alice.txt");
Scanner lesFil = new Scanner(tekstFil);
int i=0;
int totalOrd=0;
int antUnikeOrd=0;

ArrayList<String> liste = new ArrayList<String>();
while (lesFil.hasNext()){
  liste.add(lesFil.next());
}

String[] ordArray =liste.toArray(new String[liste.size()]);;
int[] antallOrd = new int[5000];

最佳答案

您的扫描程序正在尝试扫描字符串文字“Alice.txt”,而不是相应的文件。如果您想要文件,请先构造您的文件,然后将其传递到 Scanner 构造函数中:

File textFile = new File("Alice.text"); // or file path variable
Scanner fileScanner = new Scanner(textFile);

// go to town with your Scanner

或者,...

InputStream inStream = getClass().getResourceAsStream(someResourcePath);
Scanner myTextScanner = new Scanner(inStream);
<小时/>

接下来我们将讨论不使用 ArrayList 而是使用 Map<String, Integer>例如 HashMap<String, Integer> .

<小时/>

编辑
您声明:

Tried adding the file, I guess it worked, but still I haven't been able to write to the array. I am really bad at this, which is why I must use the next week to really get this stuff in... Anyways, I tried to add all the words into an arraylist, which I hoped will work. It didn't, it gave me an nosuchelementexception instead. The part of the code:

我建议您停下来,暂停您的程序,并尝试单独解决程序的每个部分。首先查看您是否确实获得了文件。

创建类似的内容,但当然要更改文件路径以使其有效:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Foo {

   // **** you will use a different String literal here!! ****
   private static final String FILE_PATH = "src/foo/foo.txt";

   public static void main(String[] args) throws FileNotFoundException {
      File file = new File(FILE_PATH);

      // check if file exits and if we're looking in the right place
      System.out.println("File path: " + file.getAbsolutePath());
      System.out.println("file exists: " + file.exists());

      Scanner scan = new Scanner(file);

      // scan through file to make sure that it holds the text 
      // we think it does, and that scanner works.
      while (scan.hasNextLine()) {
         String line = scan.nextLine();
         System.out.println(line);
      }
   }
}

只有在您完成此操作后,才可以将文本读入 ArrayList。

一如既往,请努力改进代码格式,尤其是缩进。如果出现错误或异常,请在此处打印整个文本并指出哪一行代码引发异常。

关于java - 将文件中的每个单词读取到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19327594/

相关文章:

Java 泛型 : compareTo and “capture#-of ?”

Javascript,使用数组翻译日期

javascript - 使用相同的键(日期)组合数组中的对象

java - 在已有文件的基础上创建文件

java - 通过eclipse CallHierarchy获取IMethod的方法调用

java - JUnit 黑盒/白盒测试选择排序?

java - JFreeChart 中的自定义堆积面积图

javascript - 通过与javascript中数组的单词进行比较来连接字符串的单词

C 字符串字符串比较总是导致 false

file - Swi Prolog,卸载源文件