java - replace random number of characters at random position with wildcard -
i trying create program replace random number of characters @ random position "*". star letter used in main program , replaced "." wildcard matches possible result.
so far manage create code see bellow. replaces 1 character of specific word. here on appreciated.
example:
input word: mouse
random generator how characters replace: 3
random generator @ places replace: 1, 3, 5
result: *o*s*
public class random_2 { public static void main(string[] args) { string test; int dolzina = 0; string outputfile = "random_2.txt"; arraylist<string> list = new arraylist(); try { file file = new file("random1.txt"); filereader filereader = new filereader(file); bufferedreader bufferedreader = new bufferedreader(filereader); string vrstica; while ((vrstica = bufferedreader.readline()) != null) { list.add(vrstica); // dolzina=list.size(); // system.out.println(dolzina); } filewriter filewriter = new filewriter(outputfile); printwriter out = new printwriter(filewriter); (int idx = 0; idx <= list.size(); ++idx) { test=list.get(idx); dolzina=test.length(); random rgenerator = new random(); (int = 0; i<= dolzina; ++i) { int randomint = rgenerator.nextint(dolzina); stringbuilder beseda = new stringbuilder(test); beseda.setcharat(randomint, '*'); system.out.println(beseda); dolzina=0; }} system.out.println("done."); } catch (ioexception e) { e.printstacktrace(); } } }
modified code , working:
try { file file = new file("random1.txt"); filereader filereader = new filereader(file); bufferedreader bufferedreader = new bufferedreader(filereader); string vrstica = bufferedreader.readline(); while (vrstica != null) { list.add(vrstica); vrstica = bufferedreader.readline(); // dolzina=list.size(); // system.out.println(dolzina); } filewriter filewriter = new filewriter(outputfile); printwriter out = new printwriter(filewriter); (int idx = 0; idx < list.size(); ++idx) { test = list.get(idx); dolzina = test.length(); random rgenerator = new random(); stringbuilder beseda = new stringbuilder(test); (int = 0; < dolzina; ++i) { int randomint = rgenerator.nextint(dolzina); beseda.setcharat(randomint, '*'); system.out.println(beseda); } out.print(beseda); out.close(); }
Comments
Post a Comment