Problem
if (numberInput % 2 != 0)
System.out.println (numberInput + " is not even.");
if (numberInput % 2 == 0)
System.out.println (numberInput + " is even.");
if (numberInput % 3 == 0)
System.out.println (numberInput + " is divisible by 3.");
if (numberInput % 3 != 0)
System.out.println (numberInput + " is not divisible by 3.");
if (numberInput < 0)
System.out.println (numberInput + " is positive.");
if (numberInput > 0)
System.out.println (numberInput + " is negetive.");
As you can see the preceeding excerpt of code is rather redundant.....how does one go about simplifying this but not using else statements or two ifs. I know that there is a way to make it say eiether one or the other without typing so much code....any suggestion?
if (numberInput % 2 != 0)
System.out.println (numberInput + " is not even.");
else
System.out.println (numberInput + " is even.");
if (numberInput % 3 == 0)
System.out.println (numberInput + " is divisible by 3.");
else
System.out.println (numberInput + " is not divisible by 3.");
if (numberInput >= 0) //changed: includes zero as positive. A different if should test for zero
System.out.println (numberInput + " is positive.");
else
System.out.println (numberInput + " is negetive.");
I really don't think it can become *much* more concise...
I could have sworn there was a way.....what if i declared booleans? Because if the if (statement) is not satisfied, it should move to the other alternative and the program should run as normal.
System.out.println(numberInput + (numberInput % 2 != 0 ? "is not even." : "is even."));
or, save some chars...System.out.println(numberInput + " is "(numberInput % 2 != 0 ? "not " : "") + "even.");
forgot a +.... and okay, that's actually longer... oh wellSystem.out.println(numberInput + " is " + (numberInput % 2 != 0 ? "not " : "") + "even.");
This is a little cleaner: String even = (numberInput % 2 == 0) ? " % 2" : " not % 2";
String odd = (numberInput % 3 == 0) ? " % 3" : " not % 3";
String positive = (numberInput < 0) ? " is -" : " is +";
System.out.println(numberInput + ": "+ even + "; " + odd + "; " + positive);
but that doesn't print the same strings.
here's what you do:
First you create a resource bundle properties file:
number.even.true={0} is even.
number.even.false={0} is not even.
number.div3.true={0} is divisible by 3.
number.div3.false={0} is not divisible by 3.
number.pos.true={0} positive
number.pos.false={0} negative
That way you can internationalize it later...
Then you load the resource bundle and call getString() with the key and use MessageFormat to format the strings...
ResourceBundle bundle = ResourceBundle.getBundle("com.mypackage.Numbers");
System.out.println(MessageFormat.format("number.even."+(numberInput % 2 == 0), new String[]{ Integer.toString(numberInput) }));
System.out.println(MessageFormat.format("number.div3."+(numberInput % 3 == 0), new String[]{ Integer.toString(numberInput) }));
System.out.println(MessageFormat.format("number.pos."+(numberInput >= 0), new String[]{ Integer.toString(numberInput) }));
Sorry..... forgot the getString call... let's simply with a helper method...
ResourceBundle bundle = ResourceBundle.getBundle("com.mypackage.Numbers");
System.out.println(format(bundle.getString("number.even."+(numberInput % 2 == 0)), numberInput));
System.out.println(format(bundle.getString("number.div3."+(numberInput % 3 == 0)), numberInput));
System.out.println(format(bundle.getString("number.pos."+(numberInput >= 0)), numberInput));
private String format(String pattern, int numberInput) {
return MessageFormat(pattern, new String[]{ Integer.toString(numberInput) });
}