java - keeping track of text based hangman game in an output file -
long story short, gameplayword word randomly determined list of words hangman game read (in case easy words file). easycontentlist arraylist of data in file. file 2 columns, first col - words can used, 2nd - number representing how many times word used. (0 in initial file) need keep track of number based on each iteration of game. easyfileupdatelist string arraylist declared @ class level. problem doesn't work:
public void updateeasyfile(string gameplayword, arraylist<string> easycontentlist) { integer used = 0; // represents used word count string strused = ""; (int = 0; < easycontentlist.size() - 1; i++) { string oneline; oneline = easycontentlist.get(i); tokenizer = new stringtokenizer(oneline); string firsttoken = tokenizer.nexttoken(); if (firsttoken.equals(gameplayword)) { used = integer.parseint(tokenizer.nexttoken()); used++; strused = used.tostring(); easyfileupdatelist.add(gameplayword + " " + strused + " " + "todo"); } if (!firsttoken.equals(tokenizer.nexttoken())) // add first token of every line except first easyfileupdatelist.add(oneline); } try { printwriter printwriter = new printwriter(new fileoutputstream(new file("hmeasy.txt"))); (string oneline : easyfileupdatelist) { printwriter.println(oneline); } printwriter.close(); } catch (filenotfoundexception fnfe) { fnfe.printstacktrace(); } } update
thank you, hash sets. let me clarify little more, i'm still not following completely.
correct, file said, if words dog, cat, , fog file like:
dog 0 cat 0 fog 0 there 3rd column of zeros, todo i'll worry later.
you correct problem having is adding new entry word , 1 in 2nd column. if fog randomly picked 3 times in row , method executed say
dog 0 cat 0 fog 1 dog 0 cat 0 fog 1 fog 1 dog 0 cat 0 fog 1 fog 1 fog 1 what i'm looking is: first round:
dog 0 cat 0 fog 1 2nd round:
dog 0 cat 0 fog 2 etc...
i need update number in file each time word randomly chosen.
near can tell if hmeasy.txt file looks this:
firstgameplayword 0 secondgameplayword 0 and if first entry played this:
firstgameplayword 0 secondgameplayword 0 firstgameplayword 1 todo which, suspect, not want. it's doing because use line:
easyfileupdatelist.add(gameplayword + " " + strused + " " + "todo"); since arraylist add() tacks line @ end.
i tell instead use set() i worked hard find encouraging bad design. should stop using arraylist this. wasn't designed lookups.
instead encourage @ using linkedhashset<string, integer>. no need tokenize, lookups fast, , in same order added.
when trying find right data structure in java @ this: 
Comments
Post a Comment