I have coded a program to randomly spit out characters until it hits the right word. For example, if I put the word "hello", it would spit out random five letter words until it hit "hello." I have all guesses printed in the console, which is currently working; however, I also wanted it written in a txt file, which isn't working. It won't print the guesses in the txt file. I even checked to make sure I have the right file. Here is my code:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class RandomeWords {
public static void main(String[] args) throws IOException {
File file = new File("hey.txt");
if(file.exists())
System.out.println("The File Exists");
FileWriter writer = new FileWriter(file);
writer.write("go");
Scanner scanner1 = new Scanner(System.in);
System.out.println("Enter Words");
String words = scanner1.nextLine();
int letters = words.length();
for(int i1 = 1; i1 > 0; i1++) {
if(words.contains(" "))
{
String[] alpha = " abcdefghijklmnopqrstuvwxyz".split("");
StringBuilder sb = new StringBuilder("");
for( int i3 = 0; i3 < letters; i3++) {
sb.append(alpha[(int)(Math.random()*72)]);
}
String finall = sb.toString();
if(!finall.equals(words)) {
System.out.println(finall);
writer.write("\n" +finall);
}
if(finall.equals(words)) {
System.out.println(finall);
System.out.println(i1);
System.out.println("it took " +i1 + " tries to randomly find the word.");
System.out.println("It should have taken " +Math.pow((int) 27, letters) +" times, statistacally");
System.out.println("The difference of statistically, and with the simulation is " +(int)Math.abs((int)i1 - (int)Math.pow(27, letters)));
System.exit(0);
}
}
if(!words.contains(" ")){
String[] alpha1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*?".split("");
StringBuilder sb1 = new StringBuilder("");
for(int i2 = 0; i2 < letters; i2++) {
sb1.append(alpha1[(int)(Math.random()*71)]);
}
String finall1 = sb1.toString();
if(!finall1.equals(words))
System.out.println(finall1);
if(finall1.equals(words)) {
System.out.println(finall1);
System.out.println(i1);
System.out.println("it took " +i1 + " tries to randomly find the word.");
System.out.println("It should have taken " +Math.pow((int) 26, letters) +" times, statistacally");
System.out.println("The difference of statistically, and with the simulation is " +Math.abs((int)i1 - (int)Math.pow(26, letters)));
System.exit(0);
}
}
}
// writer.close();
}}
Can someone please help? Thanks.