【问题标题】:Searching Arrays in Java: equals( ) Versus ==在 Java 中搜索数组:equals() 与 ==
【发布时间】:2012-01-17 03:50:14
【问题描述】:

我有一个包含 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();

}

}

【问题讨论】:

  • 请尝试 if( nameJob.equals(empTitle[x]) ) 而不是 if(nameJob == empTitle[x]) if (nameJob == empTitle[x]) 在与 equals() 比较之前,请检查 nameJob 是否为 Nullity
  • 我怎么不记得了。谢谢你的澄清!现在这是我永远不会忘记的事情哈哈。多谢你们!并感谢您的链接。

标签: arrays search loops equals


【解决方案1】:

这个

nameEmp == empName[x]

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

nameEmp.equals(empName[x])

【讨论】:

    【解决方案2】:

    将用户输入的值与数组中的值进行比较时,您需要使用equals 函数而不是==Read why here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2018-06-13
      相关资源
      最近更新 更多