Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-23-2008, 05:07 PM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Writing a countdown array to a file.
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.

Code:
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.
Attached Images
File Type: jpg error.jpg (36.2 KB, 2 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-23-2008, 09:13 PM
Member
 
Join Date: Apr 2007
Location: USA
Posts: 50
derrickD is on a distinguished road
If you look at the error, it relates to line 25 and it is an unhandled exception.
You need either a try/catch block around it or else add a throw to the method which will just need to be caught someplace else.
Code:
PrintWriter outFile; try { outFile = new PrintWriter("C:\\Users\\Philip\\Desktop\\JavaProgramming\\GUI Array\\program.txt"); } catch (FileNotFoundException e1) { //handle this exception somehow }
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-24-2008, 04:25 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Writing a countdown array to a file
Ok I thought I did the right thing, but apparently not. If you could have a look and maybe see what I have done. Here is the code, and then the new error that I am receiving.

Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import java.io.*; import javax.swing.JOptionPane; 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; try { outFile = new PrintWriter ("C:\\Users\\Philip\\Desktop\\JavaProgramming\\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 e1) { JOptionPane.showMessageDialog(null, "" + e1.getMessage() + " File Not Found ", "Invalid Input", JOptionPane.INFORMATION_MESSAGE); } outFile.close(); } } public static void main(String[] args){ new AnArrayTest(); } }
the error pic is at the bottom
Attached Images
File Type: jpg error.jpg (48.3 KB, 1 views)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-24-2008, 05:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,574
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-24-2008, 03:53 PM
Member
 
Join Date: Apr 2007
Location: USA
Posts: 50
derrickD is on a distinguished road
Dude, the exception says the outfile is not initialized.
All you need to do is this:
Code:
PrintWriter outFile=null;
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-25-2008, 05:03 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Thank you
Thank you. I have to study the import.logging thing, I didn't learn this in my class.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-25-2008, 07:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,574
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by derrickD View Post
Dude, the exception says the outfile is not initialized.
All you need to do is this:
Code:
PrintWriter outFile=null;
In my code it's there. If our friend use that code, not a case at all to him.
__________________
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing text to file notwist New To Java 3 04-25-2008 05:20 AM
Writing to a file (at the end) Java Tip Java Tips 0 02-08-2008 10:22 AM
Writing UTF to file using writeUTF Java Tip Java Tips 0 01-22-2008 09:19 PM
Reading/Writing a File using byte array Java Tip Java Tips 0 01-16-2008 11:41 AM
writing to a file bugger New To Java 1 11-11-2007 03:49 AM


All times are GMT +3. The time now is 08:47 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org