Results 1 to 4 of 4
  1. #1
    eeraj is offline Member
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Default 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 07:51 PM. Reason: Added code tags

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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

  1. IOException error
    By eeraj in forum AWT / Swing
    Replies: 0
    Last Post: 08-29-2009, 10:12 AM
  2. IOException error
    By eeraj in forum AWT / Swing
    Replies: 0
    Last Post: 08-29-2009, 10:02 AM
  3. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 05:11 AM
  4. Replies: 3
    Last Post: 04-10-2008, 10:01 AM
  5. Variable passing, Error: IOException
    By fernando in forum New To Java
    Replies: 3
    Last Post: 07-31-2007, 03:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •