View Single Post
  #4 (permalink)  
Old 05-24-2008, 06:43 AM
Eranga's Avatar
Eranga Eranga is offline
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Few errors are there.

In the original code, you have formated the file path in wrong way.

Handling the try-catch block is on wrong place.

You have close the outFile twice there in your code. It's useless. Sometime it can give an exception, that instance is already destroyed.

In finally clause, close() not handle IOException. Actually you no need to handle exceptions there according to your code. Even you don't do it garbage collector do it for you.

So here is the code I have done for you.

Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; 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 = null; try { 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"); } catch (FileNotFoundException ex) { Logger.getLogger(AnArrayTest.class.getName()).log(Level.SEVERE, null, ex); } finally { outFile.close(); } } } public static void main(String[] args){ new AnArrayTest(); } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote