将getter方法的名称与用户输入字符串进行比较的问题

时间:2011-02-18 14:52:00

标签: java if-statement conditional-operator getter

我无法在if语句中进行比较,在C编程中我使用“==”双等号来比较两个字符串......

如何使用getter方法将字符串与新字符串进行比较...我尝试使用双等号,但我被提示将其更改为:

if(entry [i] .getName()。equals(EName))

这就是我的整个代码:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class AddressBook {

    private AddressBookEntry entry[];
    private int counter;
    private String EName;

    public static void main(String[] args) {
        AddressBook a = new AddressBook();
        a.entry = new AddressBookEntry[100];
        int option = 0;
        while (option != 5) {
            String content = "Choose an Option\n\n"
                    + "[1] Add an Entry\n"
                    + "[2] Delete an Entry\n"
                    + "[3] Update an Entry\n"
                    + "[4] View all Entries\n"
                    + "[5] View Specific Entry\n"
                    + "[6] Exit";
            option = Integer.parseInt(JOptionPane.showInputDialog(content));
            switch (option) {
                case 1:
                    a.addEntry();
                    break;
                case 2:

                    break;
                case 3:
                    a.editMenu();
                    break;
                case 4:
                    a.viewAll();
                    break;
                case 5:
                    break;
                case 6:
                    System.exit(1);
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Invalid Choice!");
            }
        }
    }

    public void addEntry() {
        entry[counter] = new AddressBookEntry();
        entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
        entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
        entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
        entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
        counter++;
    }

    public void viewAll() {
        String addText= "";
        for (int i = 0; i <  counter; i++) {
            addText = addText+(i+1)+ entry[i].getInfo()+ "\n";
        }
        JOptionPane.showMessageDialog(null, new JTextArea(addText));
    }

    public void editMenu() {
        int option = 0;
        while (option != 6) {
            String content = "Choose an Option\n\n"
                    + "[1] Edit Name\n"
                    + "[2] Edit Address\n"
                    + "[3] Edit Phone No.\n"
                    + "[4] Edit E-mail address\n"
                    + "[5] Back to Main Menu";
            option = Integer.parseInt(JOptionPane.showInputDialog(content));
            switch (option) {
                case 1:
                    editName();
                    break;
                case 2:
                    editAdd();
                    break;
                case 3:
                    editPhoneNo();
                    break;
                case 4:
                    editEmail();
                    break;
                case 5:
                    return;
                default:
                    JOptionPane.showMessageDialog(null, "Invalid Choice!");
            }
        }
    }

    public void editName() {
        EName = JOptionPane.showInputDialog("Enter name to edit: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(EName)) {
                //JOptionPane.showMessageDialog(null, "found");
                entry[i].setName(JOptionPane.showInputDialog("Enter new name: "));
            }else {
                JOptionPane.showMessageDialog(null, "Entered Name not Found!");
            }
        }
    }

    public void editAdd() {
        EName = JOptionPane.showInputDialog("Enter name to edit: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(EName)) {
                //JOptionPane.showMessageDialog(null, "found");
                entry[i].setAdd(JOptionPane.showInputDialog("Enter new Address: "));
            }else {
                JOptionPane.showMessageDialog(null, "Entered Name not Found!");
            }
        }
    }

    public void editPhoneNo() {
        EName = JOptionPane.showInputDialog("Enter name to edit: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(EName)) {
                //JOptionPane.showMessageDialog(null, "found");
                entry[i].setPhoneNo(JOptionPane.showInputDialog("Enter new Phone No.: "));
            }else {
                JOptionPane.showMessageDialog(null, "Entered Name not Found!");
            }
        }
    }

    public void editEmail() {
        EName = JOptionPane.showInputDialog("Enter name to edit: ");
        for (int i = 0; i < counter; i++) {
            if (entry[i].getName().equals(EName)) {
                //JOptionPane.showMessageDialog(null, "found");
                entry[i].setEmail(JOptionPane.showInputDialog("Enter new E-mail add: "));
            }else {
                JOptionPane.showMessageDialog(null, "Entered Name not Found!");
            }
        }
    }
}

这是我的另一堂课:

public class AddressBookEntry {

    private String name;
    private String add;
    private String phoneNo;
    private String email;
    private int entry;

    public String getAdd() {
        return add;
    }

    public String getEmail() {
        return email;
    }

    public int getEntry() {
        return entry;
    }

    public String getName() {
        return name;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setAdd(String add) {
        this.add = add;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setEntry(int entry) {
        this.entry = entry;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public String getInfo() {
        String Info = "NAME\tADDRESS\tPHONE NO.\tE-MAIL ADD\n"
                + name + "\t " + add + "\t " + phoneNo + "\t " + email + "\n";
        return Info;
    }

    public String getInfo2() {
        String content = "INFORMATION:\n\n"
                + "Name: " + name + "\n"
                + "Address: " + add + "\n"
                + "Tel. No: " + phoneNo + "\n"
                + "Email Add: " + email + "\n\n";
        return content;
    }
}

请原谅我的代码...我是java的新手....请帮助......

如果用户输入等于条目[i] .getName()

,我想要遍历所有数组并编辑特定细节

提前多多感谢...

2 个答案:

答案 0 :(得分:3)

如果要比较字符串的表示形式而不是其对象标识,请使用equals()

假设我们有:String s = "hello";

s == s
=> true // they are the *same* object
"hello" == new String("hello") // see comment below...
=> false // they are different objects representing the same string of text
"hello".equals("hello")
=> true
s.equals("hello")
=> true

答案 1 :(得分:1)

至少要理解3件事:

Java:==测试两个引用是否指向同一个对象,而equals测试两个对象是否具有相同的内容。因此,即使两个字符串具有相同的内容,==可能给出错误,而s1.equals(s2)将给出true。你可以在谷歌上找到关于此的负载。

C:在C中,你不应该使用==比较两个字符串。 C中的字符串通常是char *(或const char *),你应该将它们与strcmp进行比较(否则你会遇到与Java相同的问题)。

C ++:可以使用==。

比较std :: string的实例