Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have this small programm written in AWT in Java.
But it's not working properly.
Please. can anyone help me out?
It's not finding out the 1st no. as greatest, evrything else is fine.


//WAP TO FIND GREATEST & SMALLEST OF NOS.
//© DHARMiL
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Greatest height = 400 width = 500></applet>*/
public class Greatest extends Applet implements ActionListener
{
	TextField t1,t2,t3,t4;
	Button b1,b2;
	Label l1,l2,l3,l4;
	public void init ()
	{
		t1 = new TextField (5);
		t2 = new TextField (5);
		t3 = new TextField (5);
		t4 = new TextField (25);
		b1 = new Button ("SMALLEST");
		b2 = new Button ("GREATEST");
		l1 = new Label ("Enter no 1");
		l2 = new Label ("Enter no 2");
		l3 = new Label ("Enter no 3");
		l4 = new Label ("RESULT is :-");
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(l3);
		add(t3);
		add(b1);
		add(b2);
		add(l4);
		add(t4);
		b2.addActionListener (this);
	}
	public void actionPerformed (ActionEvent e)	
	{
		if (e.getSource() == b2);
		{
			int a = Integer.parseInt (t1.getText());
			int b = Integer.parseInt (t2.getText());
			int c = Integer.parseInt (t3.getText());
			{
				if((a>b)&&(a>c))
				{
					t4.setText("no.1 is GREATER");
				}
				if ((b>c));
				{
				t4.setText ("n0.2 is GREATER");
				}
				else
				{	
					t4.setText ("n0.3 is GREATER");
				}
			}
		}
	}
}

[edit]Spurious "big" removed, capitalization corrected, txtspeak removed.
Please don't use txtspeak: this is an international site and it can be hard for non-native English speakers to work out what words mean when they aren't in any dictionary. - OriginalGriff[/edit]
Posted
Updated 28-Jan-11 21:16pm
v3

All that is wrong is that you need another "else" clause. At the moment:
if((a>b)&(a>c))
If it passes, then you set t4 to indicate.
But then you set t4 again either to show 2 is greater, or 3 is greater.
Add another else:
C#
if((a>b)&(a>c))
   {
   t4.setText("no.1 is GREATER");
   }
else
   {
   if ((b>c));
      {
      t4.setText ("n0.2 is GREATER");
      }
   else
      {
      t4.setText ("n0.3 is GREATER");
      }
   }
And all should be fine.

BTW: It is worth changing your names: t1, t2, t3, t4 are not very descriptive of what they do. Consider using "input1", "results" and so on - it will make working out what is happening a lot easier when projects get a bit bigger!
 
Share this answer
 
hey one more question.
i M trying to make work the smallest button also, but when i click on any button SMALLEST or GREATEST iT Returns on the result of the SMALLEST.
PlS. help
//WAP TO FIND GREATEST & SMALLEST OF NOS.
//© DHARMiL
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Greatest1 height = 400 width = 500></applet>*/
public class Greatest1 extends Applet implements ActionListener
{
	TextField t1,t2,t3,t4;
	Button b1,b2;
	Label l1,l2,l3,l4;
	public void init ()
	{
		t1 = new TextField (5);
		t2 = new TextField (5);
		t3 = new TextField (5);
		t4 = new TextField (25);
		b1 = new Button ("SMALLEST");
		b2 = new Button ("GREATEST");
		l1 = new Label ("Enter no 1");
		l2 = new Label ("Enter no 2");
		l3 = new Label ("Enter no 3");
		l4 = new Label ("RESULT is :-");
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(l3);
		add(t3);
		add(b1);
		add(b2);
		add(l4);
		add(t4);
		b1.addActionListener (this);
		b2.addActionListener (this);
	}
	public void actionPerformed (ActionEvent e)	
	{
		//if (e.getSource() == b1);
		if (e.getSource() == b2);
		{
			int a = Integer.parseInt (t1.getText());
			int b = Integer.parseInt (t2.getText());
			int c = Integer.parseInt (t3.getText());
			{
				if((a>b)&&(a>c))
				{
					t4.setText("no.1 is GREATER");
				}
				else
				{
					if ((b>c))
					{
						t4.setText ("n0.2 is GREATER");
					}
					else
					{
						t4.setText ("n0.3 is GREATER");
					}
				}
			}
		}
		if (e.getSource() == b1);
			{
				int x = Integer.parseInt (t1.getText());
				int y = Integer.parseInt (t2.getText());
				int z = Integer.parseInt (t3.getText());
				if ((x<y)&&(x<z))
				{
					t4.setText ("no.1 is SMALLER");
				}
				else
				{
					if ((y<z))
					{
						t4.setText ("no.2 is SMALLER");
					}
					else
					{
						t4.setText ("no. 3 is SMALLER");
					}
				}
			}
		}
	}
 
Share this answer
 
Comments
OriginalGriff 29-Jan-11 12:15pm    
Couple of things:
1) Don't post this as an answer: nobody gets notified if you do that. If you add a comment to an answer, then the person who wrote the answer gets an email. Faster that way, because otherwise it's luck if we check back!
2) Since this is a separate question altogether, you should raise it as a new question, rather than tacking it onto an existing one.
3) The answer is: "semicolons".

To elaborate:
"if (e.getSource() == b2);" checks if the source is button b2, and then executes an empty statement (terminated by the ';') if it is. It then carries on with the whole block beneath.

Take the semicolon off each of your tests, and it should work fine!
Your if statements do nothing:
if (e.getSource() == b2);

Notice the semi-colon at the end which is the null statement, so the code will merely continue through until the end, and the last valid test will be a check for the smallest value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900