Hello,
I have an array that is empty at first until a user enters a number say 20, then it counts down showing your numbers in a window 20, 19, 18.... Well, what I am trying to do is print those results to a file as well. I don't want to read, just write, and every time I open the program and enter a new number it will overwrite the previous file. This is what I wrote, but I get an error. This is the error I am receiving and the code below.
I tried reading some of the other posts, but they didn't seem to be helpful.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
public class AnArrayTest{
Scanner console = new Scanner(System.in);
JFrame frame;
public AnArrayTest() {
frame = new JFrame("Array");
JButton button = new JButton("Enter Here");
button.addActionListener(new MyAction());
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
PrintWriter outFile = new PrintWriter("C:\\Users\\Philip\\Desktop\\Java
Programming\\GUI Array\\program.txt");
String input = JOptionPane.showInputDialog(null,
"Enter the size of the array: ");
int arraySize = Integer.parseInt(input.trim());
int[] list = new int[arraySize];
for (int i = 0; i < list.length; i++) {
list[i] = i+1;
}
String values = "";
for(int i = 0; i < list.length; i++) {
values += list[i];
if(i < list.length-1)
values += ", ";
if((i + 1) % 40 == 0)
values += "\n";
}
JOptionPane.showMessageDialog(frame, values + " " +
"are your numbers", "Your Silly Numbers",
JOptionPane.INFORMATION_MESSAGE);
outFile.print(values + " " + "are your numbers");
outFile.close();
}
}
public static void main(String[] args){
new AnArrayTest();
}
}
My error message is below.