0

I want to pun a condition in an multidimesional array and I'm stuck. My method should return a name in a specific format. If find a title like (Mr.,Mrs) return Title , first letter from name , and the full surname. If title is not found return full name + full surname. Eg. Ms. S. T. Mark or Smith T. Rose or Tony Mark.

 String[][]  names = {
        {"Mr. ", "Mrs. ", "Ms. ","Miss"},
        {"Smith ", "Jones "},
        {"Tony ", "Jhon "},
        {"Mark ","Rose "}
    };
 if (names[0].equals(names)&& names[1].equals(names)&&names[2].equals(names)&& names[3].equals(names)){
return names[0][0] + names[1][0].substring(0,1)+". " +  names[2][0].substring(0,1)+". "+names[3][0];}

return"";
Andreea
  • 11
  • 2
  • 4
    What does "I want to pun a condition in an multidimesional array and I'm stuck" mean? I think you need to explain your problem better. Also, what specifically are you stuck on? All you've said is what you want your end goal to be. – Carcigenicate Apr 24 '17 at 10:50
  • 2
    This doesn't make sense: names[0].equals(names). You are comparing String[] (names[0]) to String[][] (names) – LLL Apr 24 '17 at 10:52
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – GhostCat Apr 24 '17 at 10:52
  • What do you mean by "tile not found". You have an hard-coded array, there is always a title in there?! – GhostCat Apr 24 '17 at 10:52
  • 2
    I would suggest you make a Person object, which has name, last name, strings, gender, married flags. that also renders the output. – Tschallacka Apr 24 '17 at 10:53
  • @Tschallacka Your way is perfect for this scenario. Instead of multidimensional array use Person object. – Mayur Bhokase Apr 24 '17 at 10:55
  • @MayurBhokase I don't think so: I think the arrays contain the possible values for the title, last name, middle name and first name. – Maurice Perry Apr 24 '17 at 11:10

1 Answers1

1

As I wrote in the comment this doesn't make sense:

names[0].equals(names)

names[0] is string array (String[]) containing: {"Mr. ", "Mrs. ", "Ms. ","Miss"} While names is array of string arrays (String[][]) containing everything, so you can't compare them in such way, it will always be false.

Moreover you actually shouldn't ever use equals for arrays. Please check this post for more information:

https://stackoverflow.com/a/8777266/7624937

Now, as suggested by @Tschallacka you should probably create Person class, e.g:

class Person {
    String title;
    String name;
    String surname;
}

and then implement functions which are of your interest. So according to your description such function would look like this:

public String introduceYourself() {
    if (title != null) {
        return title + " " + name.charAt(0) + " " + surname;
    } else {
         return name + " " + surname;
    }
}
Community
  • 1
  • 1
LLL
  • 1,777
  • 1
  • 15
  • 31