I am using Java's MessageFormat
class to create a string that takes several condition statements as parameters using the format
method. With it I have created a thermometer using ASCII characters which takes in a random number as celsius and depending on that number, inserts asterisk characters into the string at specific locations depending on a condition, e.g. ((celsius >= 25) ? "*" : " ")
. In other words, the asterisks represent the temperature level in a thermometer depending on the temperature value.
At the moment I am using similar if statements in each of the 14 parameters which then determines if it should add an asterisk in said location temp >= location
or an empty space temp <= location
. I'm sure there is a much more efficient and cleaner way in doing this using another method, e.g. embedded for loop, regex, function, built-in method, etc.
Here's an example of the code (beware escape characters):
// random value between -35 and 40
double celsius = Math.round(Math.random() * (40 - 35) - 35);
// ASCII Thermometer
String meter = MessageFormat.format(
" ______________________"
+ "\r\n | ^F _ ^C |"
+ "\r\n | 100 - |{0}| - 40 |"
+ "\r\n | 90 - |{1}| - 30 |"
+ "\r\n | 80 - |{2}| - 25 |"
+ "\r\n | 70 - |{3}| - 20 |"
+ "\r\n | 60 - |{4}| - 15 |"
+ "\r\n | 50 - |{5}| - 10 |"
+ "\r\n | 40 - |{6}| - 5 |"
+ "\r\n | 30 - |{7}| - 0 |"
+ "\r\n | 20 - |{8}| - -5 |"
+ "\r\n | 10 - |{9}| - -10 |"
+ "\r\n | 0 - |{10}| - -20 |"
+ "\r\n | -10 - |{11}| - -25 |"
+ "\r\n | -20 - |{12}| - -30 |"
+ "\r\n | -30 - |{13}| - -35 |"
+ "\r\n | '***` |"
+ "\r\n | (*****) |"
+ "\r\n | `---' |"
+ "\r\n |____________________|"
+ "\r\n\r\n",
((celsius >= 35) ? "*" : " "),
((celsius >= 30) ? "*" : " "),
((celsius >= 25) ? "*" : " "),
((celsius >= 20) ? "*" : " "),
((celsius >= 15) ? "*" : " "),
((celsius >= 10) ? "*" : " "),
((celsius >= 5) ? "*" : " "),
((celsius >= 0) ? "*" : " "),
((celsius >= -5) ? "*" : " "),
((celsius >= -10) ? "*" : " "),
((celsius >= -15) ? "*" : " "),
((celsius >= -20) ? "*" : " "),
((celsius >= -25) ? "*" : " "),
((celsius >= -30) ? "*" : " "));
Do you want this?
public static void main(String[] args) {
// random value between -35 and 40
double celsius = Math.round(Math.random() * (40 - 35) - 35);
final String s = " ______________________"
+ "\r\n | ^F _ ^C |"
+ "\r\n | 100 - |{0}| - 40 |"
+ "\r\n | 90 - |{1}| - 30 |"
+ "\r\n | 80 - |{2}| - 25 |"
+ "\r\n | 70 - |{3}| - 20 |"
+ "\r\n | 60 - |{4}| - 15 |"
+ "\r\n | 50 - |{5}| - 10 |"
+ "\r\n | 40 - |{6}| - 5 |"
+ "\r\n | 30 - |{7}| - 0 |"
+ "\r\n | 20 - |{8}| - -5 |"
+ "\r\n | 10 - |{9}| - -10 |"
+ "\r\n | 0 - |{10}| - -20 |"
+ "\r\n | -10 - |{11}| - -25 |"
+ "\r\n | -20 - |{12}| - -30 |"
+ "\r\n | -30 - |{13}| - -35 |"
+ "\r\n | '***` |"
+ "\r\n | (*****) |"
+ "\r\n | `---' |"
+ "\r\n |____________________|";
final int[] celsiusDegreeCompare = new int[]{
35, 30, 25, 20, 15, 10, 5, 0, -5, -10, -20, -25, -30, -35
};
final String[] parameters = new String[14];
IntStream.range(0, parameters.length).forEach(i -> {
parameters[i] = (celsius >= celsiusDegreeCompare[i]) ? "*" : " ";
});
// ASCII Thermometer
String meter = MessageFormat.format(s, parameters);
System.out.println("celsius: " + celsius);
System.out.println(meter);
}
Result:
celsius: -31.0
______________________
| ^F _ ^C |
| 100 - | | - 40 |
| 90 - | | - 30 |
| 80 - | | - 25 |
| 70 - | | - 20 |
| 60 - | | - 15 |
| 50 - | | - 10 |
| 40 - | | - 5 |
| 30 - | | - 0 |
| 20 - | | - -5 |
| 10 - | | - -10 |
| 0 - | | - -20 |
| -10 - | | - -25 |
| -20 - | | - -30 |
| -30 - |*| - -35 |
| ***` |
| (*****) |
| `--- |
|____________________|