Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string format condition

I want to write a sentence that is dependent on person's gender, here is what i could do:

String createSentence(String name, boolean isMale) {
    return String.format(isMale ? "I met %s, he was OK." : "I met %s, she was OK.", name);
}

but you already see the fail in that (it works, but code is duplicit), i want something like:

String createSentence(String name, boolean isMale) {
    return String.format("I met %s, %b?'he':'she' was OK.", name, isMale);
}

This ofc doesn't work, but is something like this possible?

EDIT:

Since I will want many sentences to be generated, even in different languages, and they will be stored is some sort or array, thus this solution is unhandy:

static String createSentence(String name, boolean isMale) {
    return String.format("I met %s, "+(isMale?"he":"she")+" was OK.", name);
}
like image 556
kajacx Avatar asked Jan 31 '14 14:01

kajacx


People also ask

What is %d and %s in Java?

%d means number. %0nd means zero-padded number with a length. You build n by subtraction in your example. %s is a string. Your format string ends up being this: "%03d%s", 0, "Apple"

What is %s in Java?

the %s is a 'format character', indicating "insert a string here". The extra parameters after the string in your two function calls are the values to fill into the format character placeholders: In the first example, %s will be replaced with the contents of the command variable.

Can you format strings in Java?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

What does %d do in Java?

%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.


3 Answers

How about

return String.format("I met %s, "+(isMale?"he":"she")+" was OK.", name);

or

return String.format("I met %s, %s was OK.", name, (isMale ? "he" : "she"));

If you can change type of isMale to integer which for instance would represent mapping

  • 0->she,
  • 1->he

you could use MessageFormat and its {id,choce,optionValue}

static String createSentence(String name, int isMale) {
    return MessageFormat.format("I met {0}, {1,choice,0#she|1#he} is fine",
            name, isMale);

}
like image 63
Pshemo Avatar answered Nov 15 '22 18:11

Pshemo


How about using an enum?

enum Gender {

    FEMALE("she"), MALE("he") /* ... */;

    private final String pronoun;

    private Gender(String pronoun) {
        this.pronoun = pronoun;
    }

    public String pronoun() {
        return pronoun;
    }

}

private static String createSentence(String name, Gender gender) {
    return String.format("I met %s, %s was OK.", name, gender.pronoun());
}

public static void main(String[] args) {
    System.out.println(createSentence("Glenda", Gender.FEMALE));
    System.out.println(createSentence("Glen", Gender.MALE));
}
like image 34
Dávid Horváth Avatar answered Nov 15 '22 20:11

Dávid Horváth


You could go for a combination:

String createSentence(String name, boolean isMale) {
    return String.format("I met %s, %s was OK.", name, isMale? "he": "she");
}  
like image 36
Mathias G. Avatar answered Nov 15 '22 19:11

Mathias G.