Why won't the code write to the txt file, even though the txt file exists

Viewed 35

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.

1 Answers

It looks like writer.write("\n" +finall) is only called inside the if-block, if(words.contains(" ")). This code is only reached if the input contains spaces.

If the input does not contain spaces, the call to writer.write should also be made in the other if-block, if(!words.contains(" ")).

In other words, replace

if(!finall1.equals(words))
    System.out.println(finall1);

with

if (!finall1.equals(words)) {
    System.out.println(finall1);
    writer.write("\n" + finall1);
}
Related