[英]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();
基本上是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.我正在扫描一个 txt 文件,想为每个新单词创建一个新的 Word 类,如果这个词重复,我应该使用 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我应该扫描单词,将它们放在一个名为 fileArray 的 ArrayList 中,然后检查它们是否重复,如果重复,我应该增加 Word 类的计数器,如果不是,我创建一个新的 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.首先,如果你的
fileArray
是一个List<Word>
,你试图在一个Word
对象列表中找到一个字符串( nextLine()
的返回类型nextLine()
......这永远不会起作用。
Secondly, you're trying to call counterIncrease()
on a string rather than on an instance of Word
.其次,您试图对字符串而不是
Word
的实例调用counterIncrease()
。
Thirdly, you're calling nextLine()
multiple times when I'm pretty sure you only want to call it once.第三,当我很确定你只想调用一次
nextLine()
时,你会多次调用它。
I strongly suspect you want a Map<String, Word>
at which point you can use:我强烈怀疑您想要一个
Map<String, Word>
,此时您可以使用:
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
.此代码每次迭代仅调用
nextLine()
一次,按 string查找当前单词,并且仅对Word
的实例调用counterIncrease()
。
I would personally change the Word
code so that counter
is 0 until you call counterIncrease
, at which point the loop would become:我会亲自更改
Word
代码,以便counter
为 0,直到您调用counterIncrease
,此时循环将变为:
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".换句话说,您将“确保我们有一个
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:不清楚你的
Word
类是否真的有必要的构造函数——它应该是这样的:
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.说了这么多,如果你只想要字数,你根本不需要
Word
类。 You can just use a Map<String, Integer>
or Map<String, AtomicInteger>
.您可以只使用
Map<String, Integer>
或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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.