import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
class Ordliste {
ArrayList<String> list = new ArrayList<String>();
public void lesBok(String filnavn) throws Exception {
File fil = new File(filnavn);
Scanner s = new Scanner(fil);
while(s.hasNextLine()){
leggTilOrd(s.nextLine());
}
}
private void leggTilOrd(String ord){
Ord w = new Ord(ord);
if (list.contains(w)) {
w.oekAntall();
}
else {
list.add(w);
}
}
}
class Ord {
private String tekst;
private int verdi = hentAntall();
public Ord(String tekst){
this.tekst = tekst;
this.verdi = verdi;
} //Konstruktoer
public String toString(){
return tekst;
}
public int hentAntall(){
return verdi;
}
public void oekAntall(){
verdi++;
}
}
What I am basically trying to do, is reading a words from a txt.file. The code is supposed to only save each word once, and add "verdi" 1 up each time it repeats.
What have I done wrong? "ordliste" and "ord" are two different java files. I hope you understand what I've been trying to do, even tho it's in Norwegian.