简体   繁体   中英

Java, How can I use .nextLine as an object reference

I want to use something I scanned as a reference to an object for a method in a different class. Basically scanner.nextLine().method(); but I get cannot find symbol no matter what do I try

I am scanning a txt file and want to make a new class Word for each new word, and if the word is repeated I am supposed to increase a counter with a method from class Word.

    class Word{
int counter = 1;

public counterIncrease(){
counter ++;
}
}

Im supposed to scan for words, put them in an ArrayList named here fileArray and then check if they repeat, if they do I should increase the counter from class Word, if not I make a new Word

while(scanner.hasNextLine()){
String temp = scanner.nextLine();
   if(fileArray.contains(temp){
       temp.counterIncrease();
   else{
        fileArray.add(new Word(temp);
       }

You have at least three problems here. Firstly, if your fileArray is a List<Word> , you're trying to find a string (the return type of nextLine() ) within a list of Word objects... that's never going to work.

Secondly, you're trying to call counterIncrease() on a string rather than on an instance of Word .

Thirdly, you're calling nextLine() multiple times when I'm pretty sure you only want to call it once.

I strongly suspect you want a Map<String, Word> at which point you can use:

String line;
while ((line = scanner.nextLine()) != null) {
    Word word = map.get(line);
    if (word === null) {
        map.put(line, new Word(line)); // I assume this constructor exists
    } else {
        word.counterIncrease();
    }
}

This code only calls nextLine() once per iteration, looks up the current word by string , and only calls counterIncrease() on an instance of Word .

I would personally change the Word code so that counter is 0 until you call counterIncrease , at which point the loop would become:

String line;
while ((line = scanner.nextLine()) != null) {
    Word word = map.get(line);
    if (word === null) {
        word = new Word(line);
        map.put(line, word);
    }
    word.counterIncrease();
}

In other words, you're separating "make sure we've got a Word instance" from "increase the count for the word". It's not a huge difference, admittedly...

It's not clear whether your Word class actually has the necessary constructor - it should look something like this:

public final class Word {
    private int count;
    private final String text;

    public Word(String text) {
        this.text = text;
    }

    public int getCount() {
        return count;
    }

    public int incrementCount() {
        return ++count;
    }

    public String getText() {
        return text;
    }
}

Having said all this, if you just want the word counts, you don't need the Word class at all. You can just use a Map<String, Integer> or Map<String, AtomicInteger> . For example:

Map<String, AtomicInteger> map = new LinkedHashMap<>();

String line;
while ((line = scanner.nextLine()) != null) {
    AtomicInteger counter = map.get(line);
    if (counter === null) {
        counter = new AtomicInteger();
        map.put(line, counter);
    } 
    counter.incrementAndGet();
}
while(scanner.hasNextLine()){
   // you only want to use nexLine() once per while loop, otherwise your scanner goes forward each time
   String line = scanner.nextLine();
   if(fileArray.contains(line){
      //line.increaseCounter(); there is not such a method for String
   else{
        fileArray.add(new Word(line);
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM