'Can't fix "Method in class cannot be applied"
I am trying to get the first 10 words from the dictionary list but when I compile the code, I keep getting:
SpellChecker.java:34: error: constructor SpellChecker in class SpellChecker cannot be applied to given types; SpellChecker checker = new SpellChecker(); ^ required: String[] found: no arguments reason: actual and formal argument lists differ in length I get the same thing for checker.print10(); How do I fix this?
import java.util.Arrays;
public class SpellChecker {
private String[] dictionary = {"the","of","and","to","in","is","you","that","it","he","was","for","on","are",
"as","with","his","they","I","at","be","this","have","from","or","one","had","by",
"word","but","not","what","all","were","we","when","your","can","said","there","use",
"an","each","which","she","do","how","their","if","will","up","other","about","out","many",
"then","them","these","so","some","her","would","make","like","him","into","time","has","look",
"two","more","write","go","see","number","no","way","could","people","my","than","first","water",
"been","call","who","oil","its","now","find","long","down","day","did","get","come","made",
"may","cat","dog","cats","dogs"};
public SpellChecker(String[] newDictionary) {
dictionary = newDictionary;
}
public static void print10(String[] d) {
for(int i=0; i<10; i++) {
System.out.println(d[i]);
}
System.out.println();
}
public boolean spellcheck(String w) {
boolean found = false;
for (int i=0; i< dictionary.length; i++) {
if (w.equals(dictionary[i])) {
found = true;
}
}
return found;
}
public static void main(String[] args) {
SpellChecker checker = new SpellChecker();
checker.print10();
String word = "catz";
if (checker.spellcheck(word) == true) {
System.out.println(word + " is spelled correctly!");
}
else {
System.out.println(word + " is misspelled!");
}
}
}
Solution 1:[1]
I am not sure what you are trying to achieve, but if I set your code up like this:
public class MainClass {
public static void main(String[] args) {
String[] dictionary = { "the", "of", "and", "to", "in", "is", "you", "that", "it", "he", "was", "for", "on",
"are", "as", "with", "his", "they", "I", "at", "be", "this", "have", "from", "or", "one", "had", "by",
"word", "but", "not", "what", "all", "were", "we", "when", "your", "can", "said", "there", "use", "an",
"each", "which", "she", "do", "how", "their", "if", "will", "up", "other", "about", "out", "many", "then",
"them", "these", "so", "some", "her", "would", "make", "like", "him", "into", "time", "has", "look", "two",
"more", "write", "go", "see", "number", "no", "way", "could", "people", "my", "than", "first", "water",
"been", "call", "who", "oil", "its", "now", "find", "long", "down", "day", "did", "get", "come", "made",
"may", "cat", "dog", "cats", "dogs" };
SpellChecker checker = new SpellChecker(dictionary);
checker.print10();
String word = "catz";
if (checker.spellcheck(word) == true) {
System.out.println(word + " is spelled correctly!");
} else {
System.out.println(word + " is misspelled!");
}
}
}
public class SpellChecker {
private String[] dictionary;
public SpellChecker(String[] newDictionary) {
dictionary = newDictionary;
}
public void print10() {
String [] d = this.dictionary;
for (int i = 0; i < 10; i++) {
System.out.println(d[i]);
}
System.out.println();
}
public boolean spellcheck(String w) {
boolean found = false;
for (int i = 0; i < dictionary.length; i++) {
if (w.equals(dictionary[i])) {
found = true;
}
}
return found;
}
}
It works for me. And it seems to do what it should do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dave Yarwood |
