0

In my applet, when the first three letters of the country are entered into the textfield t and when the button is pressed it should display the full country name in textfield r, but I am not getting any output in textfield r. Why is nothing being displayed?

import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Copycat extends Applet implements ActionListener {

    String country[]={"Japan","China","Russia","India","Germany","Iraq"};
    Button n;
    Label l;
    TextField t;
    TextField r;
    String x;
    public void init()
    {
        n=new Button("Click Me");
        l=new Label("Enter the name of the Country");
        t=new TextField();
        r=new TextField();
        add(n);
        add(l);
        add(t);
        add(r);
        n.addActionListener(this);

            }

    public void actionPerformed(ActionEvent e) {    
        if(e.getSource()==n)
    {   
        x=t.getText();
        for(int i=0;i<=5;i++)
        {
            if((country[i].substring(0,3).equals(x)))
                    {
                    r.setText(country[i]);
                    }
        }
    }
    }
}
Matthew0898
  • 263
  • 2
  • 13
Hobbit
  • 11
  • 4
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 22 '15 at 03:48
  • 1
    *"I am not getting any output"* Are you typing (e.g for India) `Ind` or `ind`? BTW it should be possible to replace `country[i].substring(0,3).equals(x)` with `country[i].startsWith(x)` – Andrew Thompson Apr 22 '15 at 03:52

1 Answers1

1

It is Case Sensitive

You probably should specify minimum column lengths and make r non-editable.

t=new TextField(3);
r=new TextField(10);
r.setEditable(false);

If you don't want it to be Case Sensitive, you could use

x=t.getText().toUpperCase();
for(int i=0;i<=country.length;i++){
    if((country[i].substring(0,3).toUpperCase().equals(x)))



My answer is written with the assumption that anything not already imported is not to be used.

That said, you may want to consider using a JFrame or a JPanel. You also should consider using Swing(which you would need to use if you use a JFrame or JPanel).
Matthew0898
  • 263
  • 2
  • 13