0
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.

5
  • how do you start execution? What's in your main()? Commented Oct 19, 2015 at 1:04
  • public class test{ public static void main (String[] args) throws Exception { Ordliste liste = new Ordliste(); liste.lesBok("scarlet.text"); } } Commented Oct 19, 2015 at 1:08
  • cant compile the code i have right now. Commented Oct 19, 2015 at 1:09
  • why not use String.split? Do you expect each word to be separated by a space,ie, ' '? Commented Oct 19, 2015 at 1:15
  • I forgot to say that the textfile replaced every space with linebreaks, there is just one word each line. I think my "leggTilOrd" method does not work the way I want it to. Commented Oct 19, 2015 at 1:20

1 Answer 1

1

the code "list.contains(w)" in method leggTilOrd is not appropriate;

you are not charging the repeat word but the repeat Ord object.

so if you want to charge the repeat word, your object Ord should overwrite the equals method:

class Ord{

@Override
public boolean equals(Object obj) {
    Ord o = (Ord)obj;
    return tekst.equals(o.tekst);
}

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++;
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to do this inside the leggTilOrd method? Because I am not really supposed to change the Ord class.
I am sorry, i just didn't see the code clearly, your list is declared ArrayList<String> not ArrayList<Ord>. So if you just want to save non-repeated words in list:use list.contains(ord) not list.contains(w) in leggTilOrd method is ok. But i am confused that how do you handler the Ord objects?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.