在 Java 中搜索数组:equals() 与 ==

发布于 2024-12-20 20:35:57 字数 2951 浏览 2 评论 0原文

我有一个包含 5 个元素的数组。如果用户输入姓名或头衔,则应输出该人或职位的相应职位和人员。这就是我得到的,我不确定我可能做错了什么。既然它是一个小程序,我就不需要 main 方法,对吗?我是不是把东西搞混了?我可能做错了什么?即使我输入存储在数组中的数据,它总是给我“输入与任何记录都不匹配”,我将不胜感激。提前致谢!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Assignment extends JApplet implements ActionListener

{



String[] empName = {"John Jacobs" , "Will Watts","Kevin Krust", "Allan Ayers", "Sam Smith"};

String[] empTitle = {"Software Engineer" , "Database Administrator", "Network Administrator" , "Head Programmer" ,"Department Manager"};

final int ARRAY_SIZE = 5;

boolean validName = false;

boolean validTitle = false;

String nameOfEmployee;

String titleOfEmployee;





JLabel enterInfo = new JLabel("Enter an Employee Name or Job Title");

JTextField userInput = new JTextField(20);

JButton empButton = new JButton ("Press if you entered a name");

JButton titleButton = new JButton ("Press if you entered a title");

JLabel inputDisplay = new JLabel("");

Container con = getContentPane();

public void init()

{

    con.add(enterInfo);

    con.add(userInput);        

    con.add(empButton);

    con.add(titleButton);

    con.setLayout(new FlowLayout());

    userInput.addActionListener(this);

    empButton.addActionListener(this);

    titleButton.addActionListener(this);

}

public void actionPerformed(ActionEvent event)

{

    Object source = event.getSource();

    if (source == empButton)

    {

        String nameEmp = userInput.getText();

        con.remove(enterInfo);

        con.remove(userInput);

        con.remove(empButton);

        con.remove(titleButton);

        for (int x = 0; x < ARRAY_SIZE; ++x)

            {

              if (nameEmp == empName[x])

                {

                    validName = true;

                    titleOfEmployee = empTitle[x];

                }

            }

            if(validName)

            inputDisplay.setText(nameEmp + "is a" + titleOfEmployee);



            else



            inputDisplay.setText("The title you input did not match any records.");



        con.add(inputDisplay);

        con.setBackground(Color.YELLOW);        



    }



    else

    {

        String nameJob = userInput.getText();

        con.remove(enterInfo);

        con.remove(userInput);

        con.remove(empButton);

        con.remove(titleButton);

        for (int x = 0; x < ARRAY_SIZE; ++x)

            {

              if (nameJob == empTitle[x])

                {

                    validTitle = true;

                    nameOfEmployee = empName[x];

                }

            }

            if(validName)

            inputDisplay.setText(nameOfEmployee + "is a" + nameJob);

            else

            inputDisplay.setText("The name you input did not match any records.");

        con.add(inputDisplay);

        con.setBackground(Color.YELLOW);

    }

    con.invalidate();

    con.validate();

}

}

I have an array with 5 elements. If a user inputs a name or a title, it should output that person's or job title's corresponding job title and person. Here's what I got, I'm not sure what I could be doing wrong. Since it's an applet, I don't need a main method right? Did I get stuff mixed up? What could I be doing wrong? Even if I input data that is stored in the array, it always gives me "input did not match any records" I would appreciate any help. Thanks in advance!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Assignment extends JApplet implements ActionListener

{



String[] empName = {"John Jacobs" , "Will Watts","Kevin Krust", "Allan Ayers", "Sam Smith"};

String[] empTitle = {"Software Engineer" , "Database Administrator", "Network Administrator" , "Head Programmer" ,"Department Manager"};

final int ARRAY_SIZE = 5;

boolean validName = false;

boolean validTitle = false;

String nameOfEmployee;

String titleOfEmployee;





JLabel enterInfo = new JLabel("Enter an Employee Name or Job Title");

JTextField userInput = new JTextField(20);

JButton empButton = new JButton ("Press if you entered a name");

JButton titleButton = new JButton ("Press if you entered a title");

JLabel inputDisplay = new JLabel("");

Container con = getContentPane();

public void init()

{

    con.add(enterInfo);

    con.add(userInput);        

    con.add(empButton);

    con.add(titleButton);

    con.setLayout(new FlowLayout());

    userInput.addActionListener(this);

    empButton.addActionListener(this);

    titleButton.addActionListener(this);

}

public void actionPerformed(ActionEvent event)

{

    Object source = event.getSource();

    if (source == empButton)

    {

        String nameEmp = userInput.getText();

        con.remove(enterInfo);

        con.remove(userInput);

        con.remove(empButton);

        con.remove(titleButton);

        for (int x = 0; x < ARRAY_SIZE; ++x)

            {

              if (nameEmp == empName[x])

                {

                    validName = true;

                    titleOfEmployee = empTitle[x];

                }

            }

            if(validName)

            inputDisplay.setText(nameEmp + "is a" + titleOfEmployee);



            else



            inputDisplay.setText("The title you input did not match any records.");



        con.add(inputDisplay);

        con.setBackground(Color.YELLOW);        



    }



    else

    {

        String nameJob = userInput.getText();

        con.remove(enterInfo);

        con.remove(userInput);

        con.remove(empButton);

        con.remove(titleButton);

        for (int x = 0; x < ARRAY_SIZE; ++x)

            {

              if (nameJob == empTitle[x])

                {

                    validTitle = true;

                    nameOfEmployee = empName[x];

                }

            }

            if(validName)

            inputDisplay.setText(nameOfEmployee + "is a" + nameJob);

            else

            inputDisplay.setText("The name you input did not match any records.");

        con.add(inputDisplay);

        con.setBackground(Color.YELLOW);

    }

    con.invalidate();

    con.validate();

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-12-27 20:35:57

nameEmp == empName[x]

比较对象引用。你想要的是内容的比较,所以你应该检查

nameEmp.equals(empName[x])

This

nameEmp == empName[x]

compares object references. What you want is comparison of contents, so you should check

nameEmp.equals(empName[x])
挽梦忆笙歌 2024-12-27 20:35:57

将用户输入的值与数组中的值进行比较时,您需要使用 equals 函数,而不是 ==
在此处阅读原因

When comparing the value input by the user to the values in your arrays, you need to use the equals function and not ==.
Read why here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文