I need some help, whatever I tried to put into the code written below it did not work. I want it to read from the file Alice.txt and then put every single word into the array ordArray with lower letters and then use the rest of the program to count every word, every occurence of every word and every unique word. Please, please help! If you can give me some hints as to what I could do better, or how I should implement a part where it writes the info into the file Opplysning.txt, please do not keep quiet.
我需要一些帮助,无论我试图将其放入下面编写的代码中,它都无法正常工作。我希望它从文件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);
}
}
}
EDIT: 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:
编辑:尝试添加文件,我想它工作,但我仍然无法写入数组。我真的很擅长这个,这就是为什么我必须在接下来的一个星期才能真正得到这些东西......无论如何,我试图将所有的单词添加到一个arraylist中,我希望它能起作用。它没有,它给了我一个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];
1 个解决方案
#1
2
Your Scanner is trying to scan the String literal, "Alice.txt", not the corresponding file. If you want the File, then construct your file first and then pass it into the Scanner constructor:
您的扫描程序正在尝试扫描字符串文字“Alice.txt”,而不是相应的文件。如果需要File,则首先构造文件,然后将其传递给Scanner构造函数:
File textFile = new File("Alice.text"); // or file path variable
Scanner fileScanner = new Scanner(textFile);
// go to town with your Scanner
or,...
InputStream inStream = getClass().getResourceAsStream(someResourcePath);
Scanner myTextScanner = new Scanner(inStream);
Next we'll talk about not using an ArrayList but rather a Map<String, Integer>
such as a HashMap<String, Integer>
.
接下来我们将讨论不使用ArrayList,而是使用Map
Edit
You state:
编辑你说:
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:
尝试添加文件,我想它工作,但我仍然无法写入数组。我真的很擅长这个,这就是为什么我必须在接下来的一个星期才能真正得到这些东西......无论如何,我试图将所有的单词添加到一个arraylist中,我希望它能起作用。它没有,它给了我一个nosuchelementexception而不是。代码的一部分:
I recommend that you stop, put your program on hold and that you try to solve each portion of your program in isolation. First see if you're actually getting your file.
我建议您停止,暂停程序,并尝试单独解决程序的每个部分。首先看看你是否真的得到了你的文件。
Create something like this, but of course change your file path so that it is valid:
创建这样的东西,但当然改变你的文件路径,使其有效:
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);
}
}
}
Then only after you get this working, work on reading the text into your ArrayList.
然后,只有在您完成此工作后,才能将文本读入ArrayList。
And as always, please work to improve your code formatting, especially the indentation. If you get an error or exception, print the entire text here and indicate which line of code is throwing the exception.
和往常一样,请努力改进您的代码格式,尤其是缩进。如果出现错误或异常,请在此处打印整个文本,并指出抛出异常的代码行。
#1
2
Your Scanner is trying to scan the String literal, "Alice.txt", not the corresponding file. If you want the File, then construct your file first and then pass it into the Scanner constructor:
您的扫描程序正在尝试扫描字符串文字“Alice.txt”,而不是相应的文件。如果需要File,则首先构造文件,然后将其传递给Scanner构造函数:
File textFile = new File("Alice.text"); // or file path variable
Scanner fileScanner = new Scanner(textFile);
// go to town with your Scanner
or,...
InputStream inStream = getClass().getResourceAsStream(someResourcePath);
Scanner myTextScanner = new Scanner(inStream);
Next we'll talk about not using an ArrayList but rather a Map<String, Integer>
such as a HashMap<String, Integer>
.
接下来我们将讨论不使用ArrayList,而是使用Map
Edit
You state:
编辑你说:
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:
尝试添加文件,我想它工作,但我仍然无法写入数组。我真的很擅长这个,这就是为什么我必须在接下来的一个星期才能真正得到这些东西......无论如何,我试图将所有的单词添加到一个arraylist中,我希望它能起作用。它没有,它给了我一个nosuchelementexception而不是。代码的一部分:
I recommend that you stop, put your program on hold and that you try to solve each portion of your program in isolation. First see if you're actually getting your file.
我建议您停止,暂停程序,并尝试单独解决程序的每个部分。首先看看你是否真的得到了你的文件。
Create something like this, but of course change your file path so that it is valid:
创建这样的东西,但当然改变你的文件路径,使其有效:
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);
}
}
}
Then only after you get this working, work on reading the text into your ArrayList.
然后,只有在您完成此工作后,才能将文本读入ArrayList。
And as always, please work to improve your code formatting, especially the indentation. If you get an error or exception, print the entire text here and indicate which line of code is throwing the exception.
和往常一样,请努力改进您的代码格式,尤其是缩进。如果出现错误或异常,请在此处打印整个文本,并指出抛出异常的代码行。