dimanche 21 février 2021

Why wont my code write to a file? in java

I am trying to get everything in my do-loop to write to the file but it will not work. I am using multiple methods in one class. It is not shown but I did import the random number generator class and the IO exception class. I want to write the data from these different methods into the file. Where did I go wrong?

Here is my code:

public class ArtificialData{ 
    
       public static String getRandomMake(){
          String[] makes = {"Toyota", "BMW", "Dodge", "Carriage", "Ferrari", 
                            "Sled", "Chevrolet", "Hyundai", "Ford", "Mercedes", "Honda"};
          Random rand = new Random();
          int randomIndex = rand.nextInt(makes.length);
          String randomMake = makes[randomIndex];
          System.out.print(randomMake +", ");
          
          return randomMake;       
       }
       
       public static String getRandomModel(){
          String[] models = {"335xi", "Civic", "Camry", "G500", "Huracan", "3500", "Genesis", 
                              "Corvette", "Viper", "Model S", "Corolla"};
          Random rand = new Random();
          int randomIndex = rand.nextInt(models.length);
          String randModel = models[randomIndex];
          System.out.print(randModel +", " );
          
          return randModel;   
       }
       
       public static String getRandomMPG(){
          int lowest = 7, highest = 80;
          Random rand = new Random();
          
          double randomDouble = rand.nextDouble();
          int randomInt = rand.nextInt((highest - lowest)+1)+lowest;
          String randMPG = String.format("%.1f", (randomInt + randomDouble));
          System.out.print(randMPG + ", ");
          
          return randMPG;
       }
       
       public static String getRandomColor(){
          String[] colors = {"Green", "White", "Red", "Purple", "Blue", "Pink", 
                               "Black", "Lime", "Brown", "Maroon", "Orange", "Yellow"};
          Random rand = new Random();
          int randomIndex = rand.nextInt(colors.length);
          String randColor = colors[randomIndex];
          System.out.print(randColor + ", ");
          
          return randColor;
       }
       public static int getRandomNumDoors(){
          Random rand = new Random();
          int randDoors = rand.nextInt(4)+1;
          System.out.print(randDoors + ", ");
                
          return randDoors;
       }
       public static void main(String[] args)throws IOException{
                System.out.println("Finished..");
          
          FileWriter createData = new FileWriter("c:/temp/CarData.txt", true);
          int x = 0;
          
          do{
            ++x;
            
             createData.write(getRandomMake);
             createData.write(getRandomModel);
             createData.write(getRandomMPG);
             createData.write(getRandomColor);
             createData.write(getRandomYear);
             createData.write(getRandomDoors);
             System.out.print("\n");       
           }
           while (x<=3000);
          
              
          //closing file
          createData.close();
    
       }
       
       }



Aucun commentaire:

Enregistrer un commentaire