0

Given a string I #eat# #hamburgers# and a StringBuilder eat: [eat, consume, like] hamburgers: [hamburgers, spinach, bananas], I want to randomly replace the words within hashmarks with randomly chosen ones from their wordbanks, so that phrases such as I like bananas and I consume spinach will be generated. Code to randomly select another word, given a token (i.e. eat, hamburgers) has been written.

I need to use this regex #[^#]+# to find words within the initial string contained by hashmarks, pass them to the replace method, and then put their random correlates back inside the initial string. I tried using StringTokenizer, but realized it's not the tool for the job.

I need to somehow extract the first word within hashmarks and pass it to the method calling for its replacement before calling the method archetypeString(#[^#]+#, replacement) in such a way so that when the loop runs again, both the word grabber&passer-to method and the replacement method are then working with the second hashed word.

tokenizer dead-end:

StringTokenizer stt = new StringTokenizer(archetype);
    while(stt.hasMoreTokens()){
        String temp = stt.nextToken();
        if(temp.charAt(0)=='#');
    }

and the getPhrase method:

public List<String> getPhrases(StringBuilder fileContent, String token) {
      StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(fileContent.toString()));
      List<String> list = new ArrayList<String>();
      try {
        while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            if (tokenizer.sval.equals(token)) {
              tokenizer.nextToken();  // '[' 
              do {
                tokenizer.nextToken();  // go to the number
                list.add(String.valueOf(tokenizer.sval));
              } while (tokenizer.nextToken() == ',');
              break;
            }
          }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
      return list;
    }
  • 4
    Nope. Way too much text, too few paragraphs and definitely way too few code. Please edit your post with the relevant code. If you do not have any code yet then your question is not appropriate for StackOverflow in its current state. – Jeroen Vannevel Nov 30 '13 at 20:16
  • Hey I had code and it didn't work as I said in the post; I began trying with StringTokenizer and only learned it would be futile. I understand more code would be nice; I'm not even asking for a code answer, though, just an answer as to how to approach this time of simultaneity issue. – the weakest link Nov 30 '13 at 20:19
  • I don't quite understand why you'd use `tokenizing`. Please [read this blog post](http://www.frischcode.com/2013/11/mocking-up-text.html). – Elliott Frisch Nov 30 '13 at 20:28
  • Yeah I realize that's not helpful at all--I only posted the attempt made that used tokenizing to show Jeroen what I'd already coded. – the weakest link Nov 30 '13 at 20:42
  • This user keeps posting the same question again and again in different guises. – Robin Green Nov 30 '13 at 21:07
  • I feel more that in my 12 hour struggle to answer the question, between people's help here and by reading about coding online, I've refined the question to different questions. So yes, you caught me. And I'm not intentionally wasting anyone's time; I'm very bad at coding and have been sitting at a computer all day yesterday and today trying to do something you could probably do in a second, so I feel very bad and desperate as well as unwanted and unhelped. – the weakest link Nov 30 '13 at 21:09
  • Actually, the whole thing can be done in one line of code, a simple while loop and a Map declaration. I leave it to the reader as an exercise... – Bohemian Dec 01 '13 at 02:41

1 Answers1

1

I need to use this regex #[^#]+# to find words within the initial string contained by hashmarks, pass them to the replace method, and then put their random correlates back inside the initial string. I tried using StringTokenizer, but realized it's not the tool for the job.

It is not clear from your question whether this is part of some sadistic homework assignment or just the first way you thought of to solve whatever problem you're trying to solve. This is not a regular expression problem any more than it's a StringTokenizer problem.

Look at String.format(), and the formatting capabilities of Formatter. I do not understand why you would ever need to know what the last string you generated was if your object is to generate the next one at random. Just pick a new random value and format it with String.format().

--

After reading your comment to this answer and looking at the question you referred to, I'm going to make a couple of recommendations.

(1) start with a simpler coding assignment or two, something without regular expressions. Make sure you absolutely understand the following concepts: instance variables. variable scope. public methods versus private methods. passing parameters to methods, and returning values from methods. You can do quite a bit with just that much. You don't need to study inheritance until you have all of those down cold, and I recommend that you do not try.

(2) for each coding assignment for at least your first 5, make sure you have written out what your program is to be provided as data and what output it is supposed to produce. List any constraints someone has given you separately (must use class X, must display error message, whatever).

(3) Put opening braces and closing braces on lines by themselves; match each opening brace with a closing brace indented the same amount. Indent code within each pair of braces another 2 or 3 spaces further to the right. This means that brace pairs inside other brace pairs will be indented further. I know this is not the way you see most code, and plenty of people will tell you that it is "wrong". But until you get comfortable with scope and whether a given place in your code is inside or outside a method or a loop, I think it best that you give yourself these extra visual cues. For someone not familiar with other ways of doing things, this is easiest.

(4) be careful of your terms when posting here. In the other question you refer to, you say it is about inheritance, but it uses "implements", indicating that it is implementing an interface, not inheriting from a class. It is confusing to those of us trying to help you if you get the terminology wrong.

(5) when you post here: post the entire program (these early assignments should all be under 100 lines total, no reason not to post all of it). Make sure it is properly indented; use spaces instead of tabs. In text, and maybe also in comments, point out the place in the code where you seem to have the problem (if you know). If there is an error message, post the entire error message (don't tell us what it is, and don't try to interpret it for us). Work on your code until you have a specific question: why do I get a compile error here? Why do I get (or fail to get) this output? The program outputs X but I expected Y, why is that? etc.

We're not a tutorial shop; most of us need instruction to learn to program, and you need to get most of that somewhere besides here. We are willing to help with your questions, given that your questions are specific and reasonable and you aren't expecting us to provide the instruction. By itself, "I'm lost and need help" is a bit beyond StackOverflow's normal way of operating.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • It's hard to explain why, but check here http://stackoverflow.com/questions/20306480/untangling-inherited-methods for the code I'm using now—either it will reveal why I need to do what I did, or how utterly incompetent at coding I am and how that led to me doing a lot more work than was necessary. – the weakest link Nov 30 '13 at 22:47