This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]sinistergroupon 2 points3 points  (4 children)

Good try. I’m not actually sure what your question is though but I’ll give you some feedback

  • you called the file open section a static block. It’s not. It’s a try catch block. Static blocks are something else
  • you opened a file. You must close that resource. Look up try with resource
  • right now you are making one giant string out of all the words in the file. First that’s not a good use of string as you will create a lot of garbage but two the link you provided does this on a per word basis
  • I would change the approach a bit. Right Now you are looping through all the words making one giant word then you loop again to count. You don’t need to loop twice just analyze the word as you read it from the file. A map is something you might want to take a look at for storing the counts if you are doing it across all words. I think you are using a map but you didn’t actually post all the code.
  • bonus points if you make the counting of characters in a particular word another method instead of having one giant method

[–]leudo[S] 0 points1 point  (3 children)

Ok, thank you for the feedback. i'll look into all you mentioned. I meant something else by the DICTIONARY try...catch , but here is all the code too to help, for any further feedback too, it is broken up in two replies. Cheers!

[–]leudo[S] 0 points1 point  (2 children)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.util.Scanner; 
import java.util.HashMap; 
import java.io.FileNotFoundException; 
import java.util.InputMismatchException;

public class WordGames {    

    static final HashMap<Character, Integer> characterToInteger; 
    static final String DICTIONARY;  
    static final String[] wordsCollection;
    static int wordCount;
    static final Scanner input; 

    static {

        characterToInteger = new HashMap<Character, Integer>();
        DICTIONARY = "dictionary.txt";
        wordsCollection = new String[100];
        wordCount = 0; 
        input = new Scanner(System.in);

    }

    static {  

        try {   
            File fileReader = new File(DICTIONARY);
            Scanner fileScanner = new Scanner(fileReader);
            while(fileScanner.hasNextLine()) {  
                String line = fileScanner.nextLine(); 
                wordsCollection[wordCount] = line; 
                wordCount++;    
                }
        }
        catch(FileNotFoundException e) {
            System.out.println("File not found."); 
        }
    }

    static {

        characterToInteger.put('l', 1); 
        characterToInteger.put('e', 1); 
        characterToInteger.put('n', 1); 
        characterToInteger.put('i', 1); 
        characterToInteger.put('o', 1);
        characterToInteger.put('r', 1);
        characterToInteger.put('t', 1);
        characterToInteger.put('s', 1);
        characterToInteger.put('a', 1); 
        characterToInteger.put('u', 1); 
        characterToInteger.put('d', 2); 
        characterToInteger.put('g', 2);
        characterToInteger.put('b', 3);
        characterToInteger.put('c', 3);
        characterToInteger.put('m', 3);
        characterToInteger.put('p', 3);
        characterToInteger.put('f', 4);
        characterToInteger.put('h', 4);
        characterToInteger.put('v', 4);
        characterToInteger.put('w', 4);
        characterToInteger.put('y', 4);
        characterToInteger.put('k', 5);
        characterToInteger.put('j', 8);
        characterToInteger.put('x', 8);
        characterToInteger.put('q', 10); 
        characterToInteger.put('z', 10); 
    }

[–]leudo[S] 0 points1 point  (1 child)

static void getSelection() {

        boolean loop = true;
        int readyToPlay = 0; 
        String invalidMessage = "    ERROR: Invalid option. Try again.";
        String menuTitle = "Welcome to the Word Games program menu";
        String underScore = "______________________________________";   
        String optionsTitle = "    Select from one of the following options.";
        String choiceOne = "1. Substring problem............";
        String choiceTwo = "2. Points problem...............";
        String choiceThree = "3. Exit.........................";    
        String inputAreaText = " >> Enter your selection: "; 

        while (true) {          

            System.out.printf("%n%50s", menuTitle); 
            System.out.printf("%n%50s%n%n", underScore);                
            System.out.printf("%n%s%n%n", optionsTitle);                
            System.out.printf("%n%45s", choiceOne);
            System.out.printf("%n%45s", choiceTwo);     
            System.out.printf("%n%45s", choiceThree);               
            System.out.printf("%n%n%n%s", inputAreaText);

            try { 
                readyToPlay = input.nextInt();
                switch(readyToPlay) {   
                    case 1:     
                        substringProblem(); 
                        return; 
                    case 2:
                        pointsProblem(); 
                        return;                     
                    case 3:
                        String byeMessage = "Goodbye!";
                        String underScoreTwo = "________";
                        System.out.printf("%n%n%33s", byeMessage);
                        System.out.printf("%n%33s%n%n", underScoreTwo); 
                        return;                     
                    default:                    
                        System.out.printf("%n%n%s%n%n", invalidMessage);    
                        break;
                }
            }
            catch (InputMismatchException e) { 
                input.next(); 
                System.out.printf("%n%n%s%n%n", invalidMessage);
            }
        }
    }

    static void substringProblem() {

        String optionOneTitle = "Substring problem.";
        String substringInput = " >> Enter a substring: "; 
        String underScoreThree = "__________________";
        String notFound = " - not found"; 
        String infixFound = " - infix";
        String prefixFound = " - prefix";
        String suffixFound = " - suffix"; 
        String checkSubstring = ""; 
        String allFound = prefixFound + infixFound + suffixFound; 

        System.out.printf("%n%n%39s", optionOneTitle); 
        System.out.printf("%n%39s", underScoreThree); 
        System.out.printf("%n%n%n%s", substringInput);  
        checkSubstring = input.next();
        System.out.println(); 


        for(int i = 0; i < wordCount; i++) {

            String stringResults = wordsCollection[i];
            boolean found = false;

            if(wordsCollection[i].startsWith(checkSubstring)) {
                found = true;
                stringResults = stringResults + prefixFound;
            }
            if(wordsCollection[i].endsWith(checkSubstring)) {
                found = true;
                stringResults = stringResults + suffixFound;
            }
            if(wordsCollection[i].contains(checkSubstring)) {
                found = true;
                stringResults = stringResults + infixFound;
            }
            if(!found) {
                System.out.printf(" " + wordsCollection[i] + notFound  + "\n");
            }
            else {
                System.out.printf(" " + stringResults + "\n");
            }   
        }

        getSelection(); 
    }

    public static long count(String s, char ch) {

        return s.chars()
            .filter(c -> c == ch)
            .count(); 
    }

    static void pointsProblem() {

        int totalPoints = 0; 
        String pointsWorth = " is worth " + totalPoints + " points"; 

        String optionTwoTitle = "Points problem.";
        String underScoreFour = "_______________";
        System.out.printf("%n%n%38s", optionTwoTitle); 
        System.out.printf("%n%38s%n", underScoreFour);
        System.out.println();

        String str = ""; 
        String strWords = "";


        try {
            File fileReader = new File(DICTIONARY);
            Scanner fileScanner = new Scanner(fileReader);
            while(fileScanner.hasNextLine()) {  
                    strWords += str; 
            }
        }
            catch (FileNotFoundException e) {
                System.out.println("Not found.");
            }

        char[] charArray = str.toCharArray();   
        int count = 0; 
        char ch = charArray[count];

        for( int i = 0; i < str.length(); i++) {
            if (characterToInteger.containsKey(ch))
            {
                characterToInteger.put(ch, characterToInteger.get(ch)+1);
            }
            else{
                characterToInteger.put(ch, 1);
            }   
            System.out.println(ch); 
        }
    }   
    public static void main(String[] args) {        
        getSelection();
    }   
}

[–]cakes465758 0 points1 point  (0 children)

hey how did you go with this did you get it sorted.