Reputation: 341
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.
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:
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];
Upvotes: 0
Views: 9733
Reputation: 285405
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:
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>
.
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:
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.
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.
Upvotes: 2