Results 1 to 4 of 4
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default check this exception,,, please

    //hi again,, the problem is it doesnt print in the console ,, look inside catch !!
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    public class FileReader extends JFrame{
    	
    	String s1;
    	String s2;
    	double x1;
    	double x2;
    	double result;
    	String resultStr;
    	JTextField field1; 
    	 JTextField field2 ;
    	 JTextField resultField;
    	public FileReader(){
    		
    		Container c = getContentPane();
    		JPanel p1 = new JPanel();
    		JPanel p2 = new JPanel();
    		
    		c.setLayout(new GridLayout(2,1,5,5));
    		p1.setLayout(new FlowLayout());
    		JLabel l1 = new JLabel("number 1");
    		 field1 = new JTextField(8);
    		 field2 = new JTextField(8);
    		
    	
    		 
    	
    		
    		
    		JLabel l2 = new JLabel("number 2");
    		JLabel l3 = new JLabel ("Result");
    		resultField = new JTextField(8);
    		
    		
    		p1.add(l1);
    		p1.add(field1);
    		p1.add(l2);
    		p1.add(field2);
    		p1.add(l3);
    		p1.add(resultField);
    		
    		JButton divide = new JButton("         Divide           ");
    		divide.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				
    				try{
    				s1 = field1.getText();
    				x1 = Double.parseDouble(s1);
    				
    				s2 = field2.getText();
    				x2 = Double.parseDouble(s2);
    				
    				result = x1 / x2;
    				
    				resultStr = Double.toString(result);
    				resultField.setText(resultStr);}
    				 catch (Exception ex) {
    				      	System.out.println("Wrong!"); //why it does not print on the console ??? ??????
    				      }
    				
    			}
    		});
    		p2.add(divide);
    		
    		c.add(p1);
    		c.add(p2);
    		
    		
    		
    		
    		setSize(400,400);
    		setVisible(true);
    	}
    	
    	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The println() statement wasn't executed.

    Add some more println() statements to show values and execution flow to see where the code executes and how values change.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by javanew View Post
    //hi again,, the problem is it doesnt print in the console ,, look inside catch !!

    if you leave one field emty and press divide then a NumberFormatException will be thrown and your String "Wrong!" is printed.

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. Don't know what to check for
    By Chasingxsuns in forum New To Java
    Replies: 8
    Last Post: 08-26-2009, 05:59 AM
  3. pls check it
    By aRTx in forum New To Java
    Replies: 0
    Last Post: 03-24-2009, 02:35 PM
  4. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 02:37 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •