Results 1 to 6 of 6
  1. #1
    love10 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default how to use try and catcth to my program? please help...

    i have this program and it needs to input a quantity but if you didn't input quantity it must warned you using joptionpane. i need to use try and catch and put it on my code but it doesn't work at all... i mean i don't know where i must put it plz help with my code...

    ================================================== =======
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;

    public class bookpractice extends Applet implements ActionListener,ItemListener
    {Label bklbl = new Label("BOOK");
    Choice bkchc = new Choice();
    Label edlbl = new Label("EDITION");
    CheckboxGroup edcbg = new CheckboxGroup();
    Checkbox fichk = new Checkbox("1st",edcbg,true);
    Checkbox sechk = new Checkbox("2nd",edcbg,false);
    Checkbox thchk = new Checkbox("3rd",edcbg,false);
    Checkbox fochk = new Checkbox("4th",edcbg,false);
    Checkbox wdchk = new Checkbox("with discount");
    Label qlbl = new Label("QUANTITY");
    TextField qtf = new TextField("",20);
    Label plbl = new Label("PRICE");
    TextField ptf = new TextField("",20);
    Label slbl = new Label("SALES");
    TextField stf = new TextField("",20);
    Button cbtn = new Button("COMPUTE");
    Button rbtn = new Button("RESET");
    double sales;
    double q;

    public void init()
    {add(bklbl);
    bkchc.add("JAVA");
    bkchc.add("PHP");
    bkchc.add("C#");
    add(bkchc);
    add(edlbl);
    add(fichk);
    add(sechk);
    add(thchk);
    add(fochk);
    add(wdchk);
    add(qlbl);
    add(qtf);
    add(plbl);
    add(ptf);
    add(slbl);
    add(stf);
    add(cbtn);
    add(rbtn);

    fichk.addItemListener(this);
    sechk.addItemListener(this);
    thchk.addItemListener(this);
    fochk.addItemListener(this);
    wdchk.addItemListener(this);
    cbtn.addActionListener(this);
    rbtn.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {String qstr = qtf.getText();
    try
    {if(qstr != "")
    {
    }
    }
    catch(Exception x)
    {JOptionPane.showMessageDialog(null,"An error occurred!","Warning",JOptionPane.ERROR_MESSAGE);
    }

    double amt = Double.parseDouble(ptf.getText());
    double q = Double.parseDouble(qstr);
    {if(e.getSource() == cbtn)
    {if(wdchk.getState())
    {sales = amt*q;
    sales = sales - (sales*0.10);
    stf.setText(String.valueOf(sales));
    }
    else
    {sales = amt*q;
    stf.setText(String.valueOf(sales));
    }
    }
    if(e.getSource() == rbtn)
    {bkchc.select(0);
    fichk.setState(true);
    sechk.setState(false);
    thchk.setState(false);
    fochk.setState(false);
    wdchk.setState(false);
    qtf.setText("");
    ptf.setText("");
    stf.setText("");
    }
    }
    }

    public void itemStateChanged(ItemEvent ex)
    {if(fichk.getState())
    {ptf.setText("200.00");
    }
    if(sechk.getState())
    {ptf.setText("250.00");
    }
    if(thchk.getState())
    {ptf.setText("300.00");
    }
    if(fochk.getState())
    {ptf.setText("350.00");
    }
    }
    }
    ================================================== =======

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  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

    Are you looking to catch warnings? Exceptions and warnings are two different things. You cannot catch warning in try-catch block. Only thing you can do is, on-off those warnings. But disabling them is not a good idea. Because it's alert something that can cause for errors at runtime.

  4. #4
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    maybe you should read this

    Try Catch
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  5. #5
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    i cant understand the example.. :(

  6. #6
    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

    Quote Originally Posted by PritongKandule View Post
    i cant understand the example.. :(
    What example are you talking about? And on what you are not clear? Can you explain it bit more clearly.

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •