Wrong Return Type/Scanner












0















emphasized textI am currently trying to run this code (with two different files). One file is just a dictionary of words and the other is just a file with some text. When I run this code, my code outputs the entire text of the file that is provided in getIncorrectWords. I know this is wrong since there are errors that this SpellChecker should be picking up (and only the incorrect words should be printed out). I went back to make some changes to my constructor and I cannot seem to find the error in my code. Can anyone provide some input as to what could be going wrong with my program and provide some hints or advice for how to fix it? For reference I am making a SpellChecker which stores a "dictionary" -some defined words, in a HashSet. I am supposed to parse through another file and check which words in it are not defined in the dictionary and store those in a List. This is done n my getIncorrectWords method. Finally I am providing suggestions for what words could have been meant in my getSuggestions method. The suggestions are found by the following:



Add One Character - Try adding a character at every point in the string and see if that creates a valid word.



Remove One Character - Try removing one character from any position and see if that creates a valid word.



Exchange Adjacent Characters - Try swapping two consecutive characters at any position in the string and see if that creates a valid word.



Thank you for the time and input!



public class SpellChecker {
private Set<String> dictionary = new HashSet<>();

public SpellChecker(String fileName) {
dictionary = getDictionary(fileName);
}

public List<String> getIncorrectWords(String fileName) {
File file = new File(fileName);
List<String> words = new ArrayList<>();
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim().toLowerCase();
if (!dictionary.contains(word)) {
words.add(word);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return words;
}

public static List<String> getSuggestions(String word){
List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z");
List<String> suggestions = new ArrayList<String>();
StringBuilder builder = new StringBuilder(word);
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.insert(i, string);
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length()-2; i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
char one = suggestion.charAt(i + 1);
char two = suggestion.charAt(i);
suggestion.replace(i, i + 1, String.valueOf(one));
suggestion.replace(i+1, i + 2, String.valueOf(two));
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.replace(i, i + 1, "");
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
return suggestions;
}

public Set<String> getDictionary(String fileName) {
File file = new File(fileName);
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim();
dictionary.add(word.toLowerCase());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return dictionary;
}
}


This is my main class where I call the functions.



public class SpellCheckerTester {

public static void main(String args) throws IOException {

SpellChecker checker = new SpellChecker("words.txt");
List<String> incorrectWords = checker.getIncorrectWords("test.txt");

for (String word : incorrectWords) {
List<String> suggestions = checker.getSuggestions(word);
System.out.print(word + " - ");
for (String suggestion : suggestions) {
System.out.print(suggestion + " ");
}
System.out.println();
}
}
}









share|improve this question




















  • 1





    We don't know how are you calling these functions. From the code you provided the description of the behavior is correct. getIncorrectWords() will return a list with unknown words.

    – uli
    Nov 25 '18 at 4:01











  • @uli my mistake. I went back to add my main method.

    – A.Torres
    Nov 25 '18 at 4:23











  • Could you confirm that application printing all expected incorrect words (no more, no less), but suggested all words from dictionary?

    – uli
    Nov 25 '18 at 4:37











  • @uli no. It actually prints out every word in the dictionary both the correct and incorrect words. It essentially copies the exact output in the "test.txt" file as opposed to just those words that are not defined in the "words.txt" file.

    – A.Torres
    Nov 25 '18 at 4:41






  • 1





    Is it the latest version of the code? It cannot be executed: missing return statement for getSuggestions(). I added it and executed on words: boom, test: boom, fail. it printed fail -. As expected, no issues. may be you could provide an example of data that fails for you.

    – uli
    Nov 25 '18 at 5:21


















0















emphasized textI am currently trying to run this code (with two different files). One file is just a dictionary of words and the other is just a file with some text. When I run this code, my code outputs the entire text of the file that is provided in getIncorrectWords. I know this is wrong since there are errors that this SpellChecker should be picking up (and only the incorrect words should be printed out). I went back to make some changes to my constructor and I cannot seem to find the error in my code. Can anyone provide some input as to what could be going wrong with my program and provide some hints or advice for how to fix it? For reference I am making a SpellChecker which stores a "dictionary" -some defined words, in a HashSet. I am supposed to parse through another file and check which words in it are not defined in the dictionary and store those in a List. This is done n my getIncorrectWords method. Finally I am providing suggestions for what words could have been meant in my getSuggestions method. The suggestions are found by the following:



Add One Character - Try adding a character at every point in the string and see if that creates a valid word.



Remove One Character - Try removing one character from any position and see if that creates a valid word.



Exchange Adjacent Characters - Try swapping two consecutive characters at any position in the string and see if that creates a valid word.



Thank you for the time and input!



public class SpellChecker {
private Set<String> dictionary = new HashSet<>();

public SpellChecker(String fileName) {
dictionary = getDictionary(fileName);
}

public List<String> getIncorrectWords(String fileName) {
File file = new File(fileName);
List<String> words = new ArrayList<>();
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim().toLowerCase();
if (!dictionary.contains(word)) {
words.add(word);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return words;
}

public static List<String> getSuggestions(String word){
List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z");
List<String> suggestions = new ArrayList<String>();
StringBuilder builder = new StringBuilder(word);
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.insert(i, string);
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length()-2; i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
char one = suggestion.charAt(i + 1);
char two = suggestion.charAt(i);
suggestion.replace(i, i + 1, String.valueOf(one));
suggestion.replace(i+1, i + 2, String.valueOf(two));
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.replace(i, i + 1, "");
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
return suggestions;
}

public Set<String> getDictionary(String fileName) {
File file = new File(fileName);
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim();
dictionary.add(word.toLowerCase());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return dictionary;
}
}


This is my main class where I call the functions.



public class SpellCheckerTester {

public static void main(String args) throws IOException {

SpellChecker checker = new SpellChecker("words.txt");
List<String> incorrectWords = checker.getIncorrectWords("test.txt");

for (String word : incorrectWords) {
List<String> suggestions = checker.getSuggestions(word);
System.out.print(word + " - ");
for (String suggestion : suggestions) {
System.out.print(suggestion + " ");
}
System.out.println();
}
}
}









share|improve this question




















  • 1





    We don't know how are you calling these functions. From the code you provided the description of the behavior is correct. getIncorrectWords() will return a list with unknown words.

    – uli
    Nov 25 '18 at 4:01











  • @uli my mistake. I went back to add my main method.

    – A.Torres
    Nov 25 '18 at 4:23











  • Could you confirm that application printing all expected incorrect words (no more, no less), but suggested all words from dictionary?

    – uli
    Nov 25 '18 at 4:37











  • @uli no. It actually prints out every word in the dictionary both the correct and incorrect words. It essentially copies the exact output in the "test.txt" file as opposed to just those words that are not defined in the "words.txt" file.

    – A.Torres
    Nov 25 '18 at 4:41






  • 1





    Is it the latest version of the code? It cannot be executed: missing return statement for getSuggestions(). I added it and executed on words: boom, test: boom, fail. it printed fail -. As expected, no issues. may be you could provide an example of data that fails for you.

    – uli
    Nov 25 '18 at 5:21
















0












0








0








emphasized textI am currently trying to run this code (with two different files). One file is just a dictionary of words and the other is just a file with some text. When I run this code, my code outputs the entire text of the file that is provided in getIncorrectWords. I know this is wrong since there are errors that this SpellChecker should be picking up (and only the incorrect words should be printed out). I went back to make some changes to my constructor and I cannot seem to find the error in my code. Can anyone provide some input as to what could be going wrong with my program and provide some hints or advice for how to fix it? For reference I am making a SpellChecker which stores a "dictionary" -some defined words, in a HashSet. I am supposed to parse through another file and check which words in it are not defined in the dictionary and store those in a List. This is done n my getIncorrectWords method. Finally I am providing suggestions for what words could have been meant in my getSuggestions method. The suggestions are found by the following:



Add One Character - Try adding a character at every point in the string and see if that creates a valid word.



Remove One Character - Try removing one character from any position and see if that creates a valid word.



Exchange Adjacent Characters - Try swapping two consecutive characters at any position in the string and see if that creates a valid word.



Thank you for the time and input!



public class SpellChecker {
private Set<String> dictionary = new HashSet<>();

public SpellChecker(String fileName) {
dictionary = getDictionary(fileName);
}

public List<String> getIncorrectWords(String fileName) {
File file = new File(fileName);
List<String> words = new ArrayList<>();
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim().toLowerCase();
if (!dictionary.contains(word)) {
words.add(word);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return words;
}

public static List<String> getSuggestions(String word){
List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z");
List<String> suggestions = new ArrayList<String>();
StringBuilder builder = new StringBuilder(word);
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.insert(i, string);
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length()-2; i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
char one = suggestion.charAt(i + 1);
char two = suggestion.charAt(i);
suggestion.replace(i, i + 1, String.valueOf(one));
suggestion.replace(i+1, i + 2, String.valueOf(two));
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.replace(i, i + 1, "");
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
return suggestions;
}

public Set<String> getDictionary(String fileName) {
File file = new File(fileName);
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim();
dictionary.add(word.toLowerCase());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return dictionary;
}
}


This is my main class where I call the functions.



public class SpellCheckerTester {

public static void main(String args) throws IOException {

SpellChecker checker = new SpellChecker("words.txt");
List<String> incorrectWords = checker.getIncorrectWords("test.txt");

for (String word : incorrectWords) {
List<String> suggestions = checker.getSuggestions(word);
System.out.print(word + " - ");
for (String suggestion : suggestions) {
System.out.print(suggestion + " ");
}
System.out.println();
}
}
}









share|improve this question
















emphasized textI am currently trying to run this code (with two different files). One file is just a dictionary of words and the other is just a file with some text. When I run this code, my code outputs the entire text of the file that is provided in getIncorrectWords. I know this is wrong since there are errors that this SpellChecker should be picking up (and only the incorrect words should be printed out). I went back to make some changes to my constructor and I cannot seem to find the error in my code. Can anyone provide some input as to what could be going wrong with my program and provide some hints or advice for how to fix it? For reference I am making a SpellChecker which stores a "dictionary" -some defined words, in a HashSet. I am supposed to parse through another file and check which words in it are not defined in the dictionary and store those in a List. This is done n my getIncorrectWords method. Finally I am providing suggestions for what words could have been meant in my getSuggestions method. The suggestions are found by the following:



Add One Character - Try adding a character at every point in the string and see if that creates a valid word.



Remove One Character - Try removing one character from any position and see if that creates a valid word.



Exchange Adjacent Characters - Try swapping two consecutive characters at any position in the string and see if that creates a valid word.



Thank you for the time and input!



public class SpellChecker {
private Set<String> dictionary = new HashSet<>();

public SpellChecker(String fileName) {
dictionary = getDictionary(fileName);
}

public List<String> getIncorrectWords(String fileName) {
File file = new File(fileName);
List<String> words = new ArrayList<>();
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim().toLowerCase();
if (!dictionary.contains(word)) {
words.add(word);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return words;
}

public static List<String> getSuggestions(String word){
List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z");
List<String> suggestions = new ArrayList<String>();
StringBuilder builder = new StringBuilder(word);
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.insert(i, string);
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length()-2; i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
char one = suggestion.charAt(i + 1);
char two = suggestion.charAt(i);
suggestion.replace(i, i + 1, String.valueOf(one));
suggestion.replace(i+1, i + 2, String.valueOf(two));
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
for(int i = 0; i <= builder.length(); i++){
for(String string: letters){
StringBuilder suggestion = new StringBuilder(builder.toString());
suggestion.replace(i, i + 1, "");
if(dictionary.contains(suggestion.toString().toLowerCase())){
suggestions.add(suggestion.toString());
}
}
}
return suggestions;
}

public Set<String> getDictionary(String fileName) {
File file = new File(fileName);
try {
Scanner input = new Scanner(file);
while (input.hasNextLine()) {
String word = input.nextLine().trim();
dictionary.add(word.toLowerCase());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return dictionary;
}
}


This is my main class where I call the functions.



public class SpellCheckerTester {

public static void main(String args) throws IOException {

SpellChecker checker = new SpellChecker("words.txt");
List<String> incorrectWords = checker.getIncorrectWords("test.txt");

for (String word : incorrectWords) {
List<String> suggestions = checker.getSuggestions(word);
System.out.print(word + " - ");
for (String suggestion : suggestions) {
System.out.print(suggestion + " ");
}
System.out.println();
}
}
}






java list java.util.scanner hashset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 21:49







A.Torres

















asked Nov 25 '18 at 3:43









A.TorresA.Torres

988




988








  • 1





    We don't know how are you calling these functions. From the code you provided the description of the behavior is correct. getIncorrectWords() will return a list with unknown words.

    – uli
    Nov 25 '18 at 4:01











  • @uli my mistake. I went back to add my main method.

    – A.Torres
    Nov 25 '18 at 4:23











  • Could you confirm that application printing all expected incorrect words (no more, no less), but suggested all words from dictionary?

    – uli
    Nov 25 '18 at 4:37











  • @uli no. It actually prints out every word in the dictionary both the correct and incorrect words. It essentially copies the exact output in the "test.txt" file as opposed to just those words that are not defined in the "words.txt" file.

    – A.Torres
    Nov 25 '18 at 4:41






  • 1





    Is it the latest version of the code? It cannot be executed: missing return statement for getSuggestions(). I added it and executed on words: boom, test: boom, fail. it printed fail -. As expected, no issues. may be you could provide an example of data that fails for you.

    – uli
    Nov 25 '18 at 5:21
















  • 1





    We don't know how are you calling these functions. From the code you provided the description of the behavior is correct. getIncorrectWords() will return a list with unknown words.

    – uli
    Nov 25 '18 at 4:01











  • @uli my mistake. I went back to add my main method.

    – A.Torres
    Nov 25 '18 at 4:23











  • Could you confirm that application printing all expected incorrect words (no more, no less), but suggested all words from dictionary?

    – uli
    Nov 25 '18 at 4:37











  • @uli no. It actually prints out every word in the dictionary both the correct and incorrect words. It essentially copies the exact output in the "test.txt" file as opposed to just those words that are not defined in the "words.txt" file.

    – A.Torres
    Nov 25 '18 at 4:41






  • 1





    Is it the latest version of the code? It cannot be executed: missing return statement for getSuggestions(). I added it and executed on words: boom, test: boom, fail. it printed fail -. As expected, no issues. may be you could provide an example of data that fails for you.

    – uli
    Nov 25 '18 at 5:21










1




1





We don't know how are you calling these functions. From the code you provided the description of the behavior is correct. getIncorrectWords() will return a list with unknown words.

– uli
Nov 25 '18 at 4:01





We don't know how are you calling these functions. From the code you provided the description of the behavior is correct. getIncorrectWords() will return a list with unknown words.

– uli
Nov 25 '18 at 4:01













@uli my mistake. I went back to add my main method.

– A.Torres
Nov 25 '18 at 4:23





@uli my mistake. I went back to add my main method.

– A.Torres
Nov 25 '18 at 4:23













Could you confirm that application printing all expected incorrect words (no more, no less), but suggested all words from dictionary?

– uli
Nov 25 '18 at 4:37





Could you confirm that application printing all expected incorrect words (no more, no less), but suggested all words from dictionary?

– uli
Nov 25 '18 at 4:37













@uli no. It actually prints out every word in the dictionary both the correct and incorrect words. It essentially copies the exact output in the "test.txt" file as opposed to just those words that are not defined in the "words.txt" file.

– A.Torres
Nov 25 '18 at 4:41





@uli no. It actually prints out every word in the dictionary both the correct and incorrect words. It essentially copies the exact output in the "test.txt" file as opposed to just those words that are not defined in the "words.txt" file.

– A.Torres
Nov 25 '18 at 4:41




1




1





Is it the latest version of the code? It cannot be executed: missing return statement for getSuggestions(). I added it and executed on words: boom, test: boom, fail. it printed fail -. As expected, no issues. may be you could provide an example of data that fails for you.

– uli
Nov 25 '18 at 5:21







Is it the latest version of the code? It cannot be executed: missing return statement for getSuggestions(). I added it and executed on words: boom, test: boom, fail. it printed fail -. As expected, no issues. may be you could provide an example of data that fails for you.

– uli
Nov 25 '18 at 5:21














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464468%2fwrong-return-type-scanner%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464468%2fwrong-return-type-scanner%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen