I am trying to write a program which reads a txt file in which there are 4000 or (x) words. So far i manage to write a program which you see below. It kinda words it adds (random) number of stars (*) to a randon place in a word. However here is the problem. It always add a *. How can i change that so it would add random numbers of stars to a word.
For example: lets take word Mother. I want a program too add a random number of * to a random place in this word.That random number of stars would be either 0, 1, 2, 3, 4, 5 or 6 in this example. And if i would run program multiple times i would get different result like: (* are shown as ? below)
M??her , Mot??r, Mother, ?other, ??????, M????r ,ect.
Any ideas would be welcome.
public class random_menjava_z_zvezdico {
public static void main(String[] args) {
String test;
int dolzina = 0;
String outputFile = "random_z_zvezdico.txt";
ArrayList<String> list = new ArrayList();
try {
File file = new File("4000_random_besed.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);
System.out.println("4000 besedam je bila dodana * random krat na random mestu!");
for (int idx = 0; idx < list.size(); ++idx) {
test = list.get(idx);
dolzina = test.length();
Random rGenerator = new Random();
StringBuilder beseda = new StringBuilder(test);
for (int i = 0; i < dolzina; ++i) {
int randomInt = rGenerator.nextInt(dolzina);
beseda.setCharAt(randomInt, '*');
}
out.println(beseda);
}out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire