Checkstyle configuration in maven
What most annoying programmers besides reading someone else’s code? That’s right, formatting! In a large team it is difficult to impart to everyone the same requirements for the style of …
Read Morelearn to code by examples
What most annoying programmers besides reading someone else’s code? That’s right, formatting! In a large team it is difficult to impart to everyone the same requirements for the style of …
Read MoreSuppose you write a project that must have mandatory support for java 1.6 and higher, but you only have java 9 installed and also want the compiled classes to be …
Read MoreLet’s take a simple example. It’s easier, probably, and can not be – create a list of numbers and display it on the screen through the simplest cycle:
1 2 3 4 5 |
List <Integer> numbers = Arrays.asList (1, 2, 3, 4, 5, 6); for (int number: numbers) { System.out.println (number); } |
Very often, when working on projects, you need to get the version of the pom.xml application from the command line. Here’s how you can do it:
Read MoreToday we will discuss working with pom.xml. Suppose you have a web application and you want to print its current version in some part of the page. Of course, you …
Read MoreIn today’s article, we’ll look at generating a document based on the docx document template. This is a very common task in business applications where you service needs to provide …
Read MoreLet’s look at an example of output to template a single value with information. We can take an example from previous article and modify it a little.
Read MoreUsing String.join method (from java 8)
1 2 |
String[] array = new String[] { "a", "b", "c" }; String joined2 = String.join(",", array); |
Using simple for-loop expression
1 2 3 4 5 6 |
StringBuilder sb = new StringBuilder(); for (String n : name) { if (sb.length() > 0) sb.append(','); sb.append("'").append(n).append("'"); } return sb.toString(); |
Read More
Using java.util.formatter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
StringBuilder sb = new StringBuilder(); // Send all output to the Appendable object sb Formatter formatter = new Formatter(sb, Locale.US); // Explicit argument indices may be used to re-order output. formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") // -> " d c b a" // Optional locale as the first argument can be used to get // locale-specific formatting of numbers. The precision and width can be // given to round and align the value. formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E); // -> "e = +2,7183" // The '(' numeric flag may be used to format negative numbers with // parentheses rather than a minus sign. Group separators are // automatically inserted. formatter.format("Amount gained or lost since last statement: $ %(,.2f", balanceDelta); // -> "Amount gained or lost since last statement: $ (6,217.58)" |
See the Formatter docs for other modifiers.
Read More