problem with comparing a name from getter method to user input string
i'm having trouble comparing in my if statement, in C programming i am using "==" double equal sign to compare two string...
how about comparing a string using getter method to a new string... i try to use the double equal sign but i was prompted to change it into this:
if (entry[i].getName().equals(EName))
by the way this is my whole code:
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!");
}
}
开发者_JAVA技巧}
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!");
}
}
}
}
and this is my other class:
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;
}
}
PLEASE PARDON MY CODE... i am new at java.... please help....
what i want to is traverse to all the array and edit the specific detail if the user input was equals to the entry[i].getName()
thanks a lot in advance...
Use equals()
if you want to compare the representation of the string and not its object identity.
Assume we have: 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
There are at least 3 things to understand:
Java: == tests whether two references point to the same object, whereas equals tests whether two objects have the same content. So, even if two Strings have the same content, == may give false whereas s1.equals(s2) will give true. You can find loads about this on google.
C: In C, you shouldn't compare two strings using == either. Strings in C generally are char* (or const char*), and you should compare them with strcmp (or else you will run into the same problems as in Java).
C++: instances of std::string can be compared using ==.
精彩评论