2

need to create a text file with a random invoice number when clicking on the checkout button. need to do this within a method. I'm not sure on how to create and increment the random file names within the method. I created a method for this but I don't know where to go from here.

    private class checkoutListener implements ActionListener{



    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();

        if(button == CheckoutBtn){



        }
smh
  • 23
  • 3
  • What programing language is this? This does not appear to be tagged correctly. I think you meant to tag your question as `java` which is very different from `javascript`. – Wesley Smith Nov 27 '15 at 05:21
  • Hi smh, DelightedD0D had replaced the tag `javascript` by `java`, but you have changed it back to `javascript`. Can you explain why you have applied this tag, while your code sample is written in `java`? – www.admiraalit.nl Nov 28 '15 at 19:51
  • I'm sorry, I guess I am still a little confused on tagging items. – smh Nov 28 '15 at 20:00

2 Answers2

0

Outside your function, something like this:

String name = "someName";
int counter = 1;

And inside your function:

File f = new File(name+counter+".txt");
// Write...
counter++;

This accesses files someName1.txt, someName2.txt, etc. Then use printwriters or outputstreams to write on that file.

BlueMoon93
  • 2,910
  • 22
  • 39
0

Do you mean something like this:

int randomInvoice = Math.round(Math.random() * 1000000);
File myfile1 = new File("file" + randomInvoice); // create file 1
myfile1.createNewFile();
...
randomInvoice++; // increase
File myfile2 = new File("file" + randomInvoice);
myfile2.createNewFile(); // create file 2

update: Try to use something this:

private class checkoutListener implements ActionListener{
    private int randomInvoice = Math.round(Math.random() * 10000);

@Override
public void actionPerformed(ActionEvent e) {
    JButton button = (JButton) e.getSource();

    if(button == CheckoutBtn){
        randomInvoice++;
        File file = new File("invoice" + randomInvoice + ".txt");
        file.createNewFile();
        ... // working with file
    }
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
  • yes, but every time I execute, I need to create a new file and increment the number by one, soo it needs to be like "invoice####.txt" – smh Nov 29 '15 at 01:22
  • I updated previous post, could you it's what you want? – Slava Vedenin Nov 29 '15 at 01:56
  • yes! I have an array that I need to print to the file, how would I go about that? – smh Nov 29 '15 at 02:16
  • see, http://stackoverflow.com/questions/13707223/how-to-write-an-array-to-a-file-java , http://stackoverflow.com/questions/18871341/writing-a-string-array-to-file-using-java-separate-lines and so on – Slava Vedenin Nov 29 '15 at 02:30