Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    What is wrong with this applet?

    I'm trying to make an applet that finds the maximum and minimum numbers out of five text boxes.

    Here's what I have so far
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.lang.Math.*;
    
    public class finalpart3 extends Applet implements ActionListener
    {
    TextField statusField1;
    
    TextField statusField2;
    
    TextField statusField3;
    
    TextField statusField4;
    
    TextField statusField5;
    
    TextField resultField;
    
    Button maxButton;
    
    Button minButton;
    
    
    
    String rst;
    
    Double v1, v2, v3, v4, v5,v6,v7,v8,v9,v10;
    
    double maxValue= Double.MAX_VALUE;
    double minValue= Double.MIN_VALUE;
    
    
    public void init()
    {
    	setLayout(new FlowLayout());
    	maxButton= new Button("MAX");
    	minButton= new Button("MIN");
    	statusField1= new TextField("Type here");
    	maxButton.addActionListener(this);
    	minButton.addActionListener(this);
    	statusField1.addActionListener(this);
    	statusField2= new TextField("Type here");
    	statusField2.addActionListener(this);
    	statusField3= new TextField("Type here");
    	statusField3.addActionListener(this);
    	statusField4= new TextField("Type here");
    	statusField4.addActionListener(this);
    	statusField5= new TextField("Type here");
    	statusField5.addActionListener(this);
    	resultField = new TextField("Result displayed here");
    	resultField.setEditable(false);
    	
    	add(maxButton);
    	add(minButton);
    	add(statusField1);
    	add(statusField2);
    	add(statusField3);
    	add(statusField4);
    	add(statusField5);
    	add(resultField);
    }
    public void actionPerformed(ActionEvent act){
    	v1=Double.parseDouble(statusField1.getText());
    	v2=Double.parseDouble(statusField2.getText());
    	v3=Double.parseDouble(statusField3.getText());
    	v4=Double.parseDouble(statusField4.getText());
    	v5=Double.parseDouble(statusField5.getText());
    	if(act.getSource()==maxButton){
    
    	
    	v6=Math.max(v1,v2);
    	v7=Math.max(v6,v3);
    	v8=Math.max(v7,v4);
    	v9=Math.max(v8,v5);
    	
    	resultField.setText(Double.toString(v9));
    	}
    	
    	else if(act.getSource()==minButton)
    	v6=Math.min(v1,v2);
    	v7=Math.min(v6,v3);
    	v8=Math.min(v7,v4);
    	v9=Math.min(v8,v5);
    	
    	resultField.setText(Double.toString(v9));
    }
    
    
    }
    But all that seems to do is to find the maximum/minimum of the first two text boxes.

    What am I doing wrong?

    I also tried a different approach but I got some weird results with it too. In place of the Math.max parts I put this instead:
    Code:
    	if (v1>=v2&&v1>=v3&&v1>=v4&&v1>=v5){
    	resultField.setText(Double.toString(v1));
    	}
    	else if(v2>=v1&&v2>=v3&&v2>=v4&&v2>=v5){
    	resultField.setText(Double.toString(v2));
    	}
    	else if(v3>=v1&&v3>=v2&&v3>=v4&&v3>=v5){
    	resultField.setText(Double.toString(v3));
    	}
    	else if(v4>=v1&&v4>=v2&&v4>=v3&&v4>=v5){
    	resultField.setText(Double.toString(v4));
    	}
    	else{
    	resultField.setText(Double.toString(v5));
    	}
    That one almost seemed to just grab a random number most of the time.
    Last edited by chewy; 12-16-2010 at 11:36 PM.

  • #2
    New Coder
    Join Date
    Sep 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I attempted to figure out how to add validation for numbers only.

    In trying to do that, the program now works as intended. Not sure what I did to fix it.

    Code:
    public void actionPerformed(ActionEvent act){
    	v1=Double.parseDouble(statusField1.getText());
    	v2=Double.parseDouble(statusField2.getText());
    	v3=Double.parseDouble(statusField3.getText());
    	v4=Double.parseDouble(statusField4.getText());
    	v5=Double.parseDouble(statusField5.getText());
    	if(act.getSource()==maxButton){
    	try{
    	v6=Math.max(v1,v2);
    	v7=Math.max(v6,v3);
    	v8=Math.max(v7,v4);
    	v9=Math.max(v8,v5);
    	
    	resultField.setText(Double.toString(v9));
    	}catch (NumberFormatException E){
    		resultField.setText("Please enter only numbers");
    	}
    
    	}
    	else if(act.getSource()==minButton)
    	try{
    	v6=Math.min(v1,v2);
    	v7=Math.min(v6,v3);
    	v8=Math.min(v7,v4);
    	v9=Math.min(v8,v5);
    	
    	resultField.setText(Double.toString(v9));
    	}catch (NumberFormatException E){
    		resultField.setText("Please enter only numbers");
    	}
    }
    That's the only thing I changed, is added the try and catch parts. Which don't even work as I intended them to. But everything else does work.

    EDIT: Just got the actual validation working too by adding try and catch around the parsing part. Not sure what was stopping this from working before but it is fully functioning now.
    Last edited by chewy; 12-16-2010 at 11:35 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
    •