Results 1 to 4 of 4
Thread: IOException error
- 08-29-2009, 09:26 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 3
- Rep Power
- 0
IOException error
well i am a newbie in java programming...i want to make a frame and button display program in which when the button is clicked then it prompts user to enter the value...but i am getting a exception throw error..i dont know exactly where to place the file " throws IOException" in code...plzzzzzzzzzzz help me out
here is the code
Java Code:import java.awt.*; import java.awt.event.*; import java.io.*; class ButtonFrame extends Frame implements ActionListener , WindowListener { Button button; Button button1; Button button2; Button button3; double l,w,h; ButtonFrame(String s) { super(s); setBackground(Color.magenta); setSize(500,400); setLayout(new FlowLayout()); addWindowListener(this); button = new Button("Length"); button1= new Button("Width"); button2 = new Button("Height"); button3 = new Button("Volume"); add(button); add(button1); add(button2); add(button3); button.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); setVisible(true); } public void windowClosed(WindowEvent event){} public void windowDeiconified(WindowEvent event){} public void windowIconified (WindowEvent event){} public void windowActivated(WindowEvent event){} public void windowDeactivated(WindowEvent event){} public void windowOpened(WindowEvent event){} public void windowClosing(WindowEvent event) { System.exit(0); } public void actionPerformed(ActionEvent event) { if (event.getActionCommand().equals("Length")) { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader (reader); System.out.println("Enter Length:"); String text= input.readLine(); Double x = new Double(text); l = x.doubleValue(); } if (event.getActionCommand().equals("Width")) { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader (reader); System.out.println("Enter Width:"); String text= input.readLine(); Double x = new Double(text); w = x.doubleValue(); } if (event.getActionCommand().equals("Height")) { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader (reader); System.out.println("Enter Height:"); String text= input.readLine(); Double x = new Double(text); h = x.doubleValue(); } if (event.getActionCommand().equals("Volume")) { System.out.println("The Volume of Figure "); Double volume = l*w*h; System.out.println("is" +volume); } } } class TestButtonFrame2 { public static void main (String[] args) { ButtonFrame buttonFrame = new ButtonFrame("Volume Calculator"); } }Last edited by Eranga; 08-29-2009 at 06:51 PM. Reason: Added code tags
- 08-29-2009, 11:15 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
1.) Please use code tags when posting code to make your post more readable.
2.) Just post per problem is enough. No need to flood the forums with the same question.
3.) Why are you using awt components? Why not use swing components?
4.) You can use Adapters for the listeners so you don't have to put those empty methods.
5.) The throws declaration goes in the method declaration just before the opening brace of the method.
- 08-29-2009, 06:53 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also please don't post your question multiple times. I've found that you post the same question same time in several places. Please keep in mind, that wont help you to find the answer quickly. You have to post in most suitable sub-forum, and if you feel that if you want to ask it another section, please contact one of our moderator including me. So we can move it to the desired place.
- 08-31-2009, 04:05 AM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
Rather than use a throws clause, I would handle the exception in place. (In this case, exceptions will happen during testing or not at all.)
To handle the exception wrap a try-catch around the method body:
Java Code:public void actionPerformed(ActionEvent event) { [INDENT]try { [INDENT]if (event.getActionCommand().equals("Length")) { ... }[/INDENT] } catch(IOException ex) { ex.printStackTrace(); }[/INDENT] }
Similar Threads
-
IOException error
By eeraj in forum AWT / SwingReplies: 0Last Post: 08-29-2009, 09:12 AM -
IOException error
By eeraj in forum AWT / SwingReplies: 0Last Post: 08-29-2009, 09:02 AM -
Error: unreported exception java.io.IOException; ??
By jonsamwell in forum New To JavaReplies: 5Last Post: 08-24-2008, 04:11 AM -
java.io.IOException: CreateProcess: matlab error=2
By Jack in forum Advanced JavaReplies: 3Last Post: 04-10-2008, 09:01 AM -
Variable passing, Error: IOException
By fernando in forum New To JavaReplies: 3Last Post: 07-31-2007, 02:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks