0

How to match a string of array by if method. Means i take some value in a string array and take user input and then input match that string array is right or wrong if right give a value or wrong give other value.And I want to take input use it in loop. I have such a code and i do it without loop can match but can not use loop.

public class third {

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);

        System.out.println("Enter a name:\n");
        String first_name;
        first_name=user_input.next();
        String last_name;
        last_name=user_input.next();


        if(first_name.matches("Elory"))
        {
            System.out.println("MR. " + first_name + " " + last_name);
        }
        else if(first_name.matches("Fred"))
        {
            System.out.println("MR. "+ first_name+" "+last_name);
        }
        else if(first_name.matches("Graham"))
        {
            System.out.println("MR. "+ first_name+" "+last_name);
        }
        else if(first_name.matches("Amy"))
        {
            System.out.println("MS. "+ first_name+" "+last_name);
        }
        else if(first_name.matches("Buffy"))
        {
            System.out.println("MS. "+ first_name+" "+last_name);
        }
        else if(first_name.matches("Cathy"))
        {
            System.out.println("MS. "+ first_name+" "+last_name);
        }
        else 
        {
            System.out.println(first_name+" "+last_name);
        }

    }

}
Rustam
  • 6,485
  • 1
  • 25
  • 25
Anjan
  • 62
  • 11

4 Answers4

1

Use equals() method on String, i.e.:

first_name.equals("Amy")

Also, since you're duplicating the logic, you might benefit from logical operator (such as ||):

if(first_name.equals("Amy") || first_name.equals("Buffy") || first_name.equals("Cathy")) {
    System.out.println("MS. "+ first_name+" "+last_name);
}

And if you're using Java SE7 or later, then you can also benefit from case statement supporting String values:

switch(first_name) {
    case "Elory": 
    case "Fred":
    case "Graham":
        System.out.println("MR. " + first_name + " " + last_name); 
        break;
    case "Amy": 
    case "Buffy":
    case "Cathy":
        System.out.println("MS. " + first_name + " " + last_name); 
        break; 
    default:
        System.out.println(first_name+" "+last_name);
        break; 
}
Norbert Radyk
  • 2,608
  • 20
  • 24
0

Insert Amy, Fred, Buffy,... Into a list of Strings and say if(list.contains(firstName)) ....

Edit

Or you can group them onto 2 lists one for MR and another for MS

Omar.Nassar
  • 379
  • 1
  • 3
  • 14
0

I think the solution proposed by @norbert-radyk is the best one. But if you use Java 6 you could do something like this:

public class third {
    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        String[] female_names = {"Amy", "Buffy", "Cathy"};
        String[] male_names = {"Elory", "Fred", "Graham"};
        System.out.println("Enter a name:\n");
        String first_name;
        first_name=user_input.next();
        String last_name;
        last_name=user_input.next();

        if(Arrays.asList(female_names).contains(first_name)) {
            System.out.print("MS. ");
        } else if(Arrays.asList(male_names).contains(first_name)) {
            System.out.print("MR. ");
        }
        System.out.println(first_name+" "+last_name);
    }
}
Petr Shypila
  • 1,449
  • 9
  • 27
  • 47
0

I'd recommend using a Map and store the salutation against each name rather than two lists. Reasoning:

  • Less code
  • No assumptions on male/female prefixes in case some are Miss, Mrs, Ms etc.
  • By using String.toLowerCase() on the key and the lookup it will deal with mixed case AMY, aMy etc unlike switch/case solution or the 2-list solution

Code

public class third {

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);

        System.out.println("Enter a name:\n");
        String first_name = user_input.next();
        String last_name = user_input.next();

        Map<String, String> salutations = new HashMap<String, String>();
        salutations.put("Amy".toLowerCase(), "MS");
        salutations.put("Buffy".toLowerCase(), "MS");
        salutations.put("Cathy".toLowerCase(), "MS");
        salutations.put("Elory".toLowerCase(), "MR");
        salutations.put("Fred".toLowerCase(), "MR");
        salutations.put("Graham".toLowerCase(), "MR");

        String salutation = null;
        if (first_name != null) {
            salutation = salutations.get(first_name.toLowerCase());
        }

        if (salutation != null) {
            System.out.println(String.format("%s. %s %s", salutation, first_name, last_name));
        } else {
            System.out.println(first_name + " " + last_name);
        }

    }
}
Adam
  • 35,919
  • 9
  • 100
  • 137