dimanche 5 novembre 2023

Random number not writing to file [closed]

package ag;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.PrintWriter;

public class CowPath {
    
    private static String newNum;
    
    private static JTextField textField;

    private static void createAndShowGUI() throws IOException {

        JFrame frame = new JFrame("Bessie's Paths");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(400, 120));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1, 0, 10));

        JLabel label = new JLabel("How many paths surround Bessie");
        textField = new JTextField(10);
        JButton doneButton = new JButton("Done");

        label.setHorizontalAlignment(JLabel.CENTER);
        textField.setHorizontalAlignment(JTextField.CENTER);
        doneButton.setPreferredSize(new Dimension(150, 30));

        panel.add(label);
        panel.add(textField);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(doneButton);

        doneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    doneButtonactionPerformed(evt);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });

        frame.add(panel, BorderLayout.NORTH);
        frame.add(buttonPanel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);



    }

    private static void doneButtonactionPerformed(ActionEvent evt) throws IOException {
        int max3 = 1000;
        int min3 = 100;
        int num2 = new Random().nextInt(max3 - min3) + min3;
        
        newNum = textField.getText();

        if (newNum != null) {
    
            int max13 = Integer.parseInt(newNum);
            int min13 = 1;
            int randInt26 = new Random().nextInt(max13 + 1 - min13) + min13;
            
            String filePath = "/Users/arnavgupta/eclipse-workspace/Q4Assesment2ArnavGuptaSoftware2023/src/ag/CowPaths" + randInt26 + ".txt";
            
            FileWriter writer = new FileWriter(filePath);

            int max = 200;
            int min = 10;
            int randInt2 = new Random().nextInt(max + 1 - min) + min;

            writer.write(String.valueOf(num2));
            
            for (int i = 1; i < Integer.parseInt(newNum); i++) {
                String filePath2 = "/Users/arnavgupta/eclipse-workspace/Q4Assesment2ArnavGuptaSoftware2023/src/ag/CowPaths" + i + ".txt";
                File myObj = new File(filePath2);
                if (myObj.createNewFile()) {
                    for (int i1 = randInt2; i1 > 0; i1--) {
                        Random rand = new Random();
                        int rand_int1 = rand.nextInt(100);
                        writer.write(String.valueOf(rand_int1) + "  ");
                        System.out.println(rand_int1);
                    }
                  } 
                BufferedReader reader = new BufferedReader(new FileReader(filePath2));
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] numberStrings = line.split("  ");
                    for (String numStr : numberStrings) {
                        double num = Double.parseDouble(numStr);
                        if (num > 100) {
                            JOptionPane.showMessageDialog(null, "Bessie found the path, in total she went through " + i + " paths and walked " + randInt2 + " Meters");
                            for (int i2 = 1; i2 < Integer.parseInt(newNum); i2++) {
                                String filePath3 = "/Users/arnavgupta/eclipse-workspace/Q4Assesment2ArnavGuptaSoftware2023/src/ag/CowPaths" + i2 + ".txt";
                                FileWriter fwOb = new FileWriter(filePath3, false); 
                                PrintWriter pwOb = new PrintWriter(fwOb, false);
                                pwOb.flush();
                                pwOb.close();
                                fwOb.close();
                            System.exit(0);
                        }
                        }
                    }
                reader.close();
            }

        }           writer.close();
        }
        
        

    }
    

    public static void main(String[] args) throws IOException {
        createAndShowGUI();
    }

}

This code is supposed to mimic the cow path problem and the way it works is that it should plant a bunch of numbers below 100 into each file, then seed a number above 100 in one of the files then find the number above 100. however, the numbers below 100 are not being added to the file. why is that the case?




Aucun commentaire:

Enregistrer un commentaire