java - problem with comparing a name from getter method to user input string -
i'm having trouble comparing in if statement, in c programming using "==" double equal sign compare 2 string...
how comparing string using getter method new string... try use double equal sign prompted change this:
if (entry[i].getname().equals(ename))
by way 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 = new addressbook(); a.entry = new addressbookentry[100]; int option = 0; while (option != 5) { string content = "choose option\n\n" + "[1] add entry\n" + "[2] delete entry\n" + "[3] update entry\n" + "[4] view 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= ""; (int = 0; < 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 option\n\n" + "[1] edit name\n" + "[2] edit address\n" + "[3] edit phone no.\n" + "[4] edit e-mail address\n" + "[5] 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 edit: "); (int = 0; < 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 edit: "); (int = 0; < 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 edit: "); (int = 0; < 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 edit: "); (int = 0; < 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 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 code... new @ java.... please help....
what want traverse array , edit specific detail if user input equals entry[i].getname()
thanks lot in advance...
use equals()
if want compare representation of string , not object identity.
assume have: string s = "hello";
s == s => true // *same* object "hello" == new string("hello") // see comment below... => false // different objects representing same string of text "hello".equals("hello") => true s.equals("hello") => true
Comments
Post a Comment