gpt4 book ai didi

java - 将 getter 方法中的名称与用户输入字符串进行比较时出现问题

转载 作者:行者123 更新时间:2023-12-01 19:19:23 24 4
gpt4 key购买 nike

我在比较 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新手...请帮助...

我想要的是遍历所有数组并编辑特定详细信息(如果用户输入等于entry[i].getName())

提前非常感谢...

最佳答案

如果您想比较字符串的表示形式而不是其对象标识,请使用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

关于java - 将 getter 方法中的名称与用户输入字符串进行比较时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5042726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com