Untangling Inherited Methods

64 views Asked by At

Okay, so this is my first time implementing classes, and everything's going wrong. I'm implimenting a different class, PhraseGenerator, and the method inherited which I wish to define here is getPhrase(). It needs to return theArcha. Instead of working within it, I chose to wrap its braces around my work afterwards, and now, no matter where I put it, a different error arises. Before dealing with any of these, I want to make sure I'm putting it in the right place. To my understanding, it would go within public....FromFile implements PhraseGenerator. Any thoughts on where it should go?

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PhraseGeneratorFromFile implements PhraseGenerator {
    private ParserHelperImpl parserHelper;
    public String getPhrase() {


    public PhraseGeneratorFromFile(String filename) {
        // read file
        StringBuilder fileContent = new StringBuilder();
        BufferedReader br = new BufferedReader(new FileReader(filename));
        try {

            String line = br.readLine();

                while (line != null) {
                fileContent.append(line);
                fileContent.append('\n');
                line = br.readLine();
            }
            String everything = fileContent.toString();
        } finally {
            br.close();
        }


        parserHelper = new ParserHelperImpl();


        List<String> phraseCollection = parserHelper.getPhrases(fileContent,"phrases:");
        String archetype = parserHelper.getRandomElement(phraseCollection);
        boolean flagga = true;
        while(flagga = true){
        Pattern ptrn = Pattern.compile("#[^#]+#");
        Matcher m = ptrn.matcher(archetype);
        String fromMatcher = m.group(0);
        String col = ":";
        String token = fromMatcher+col;
        List<String> pCol = parserHelper.getPhrases(fileContent, token);
        String repl = parserHelper.getRandomElement(pCol);
        String hash = "#";
        String tk2 = hash + token + hash;
        archetype = parserHelper.replace(archetype, tk2, repl);
        flagga = m.find();
        }
        String theArcha = archetype;


            return theArcha;
    }


    }
    }
3

There are 3 answers

6
Robin Green On

Yes, it is in the right place but you are missing the closing }, which should come directly after the {. You can't put a method inside another method like that.

Because you want to return theArcha, you should instead make it what we call "an instance variable" - you may not have heard of this? If not, look it up.

0
Sameer Sawla On

A good practice while posting a question here is :

(1). Explain in brief what you expect off your code to do.

(2). If you are experiencing certain errors, copy them here so that it can be understood what is going wrong in your code.

I seriously did not understood what you were trying to achieve but I see a missing closing bracket in

public String getPhrase()

It should be :

public String getPhrase()
 {
 //logic here
 }

Hope this helps

0
rocketlad On

Your interface is probably like this

interface PhraseGenerator {
String getPhrase(); 
}

Then the implementing class you wrote will take the form

class PhraseGeneratorImpl implements PhraseGenerator  {

private ParserHelperImpl parserHelper;


@Override //Used for an overridden or implemented method
public String getPhrase() {
//Put all the code you want to implement here..

//If you want to make use of a helper Class the clean way is to use an instance of it(You tried it with Helper)    
//If you want to make use of a utility method within the same class, 
//say reading something from the file system define a private method below this method
String filePhrase = phraseGeneratorFromFile();
//Now use the filePhrase do do other stuff

}

//
private String phraseGeneratorFromFile(){
//Do all the stuff and return phrase/string so declare return type. you havent done it in the code above
}



}