How to generate a MS Word document (docx) from Java
In 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 Morelearn to code by examples
In this category there are examples of code and articles on java.
Choose the section you need and start learning programming.
In 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 MoreHow to convert int to Integer in java. This is the most popular question asked on the forums. Here are 3 examples of how to do this.
Read MoreSuppose we have the following problem. User input some value, how to check that this value is a String is an integer: 1. Check by Exception
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void main(String[] args) { System.out.println("Is int? " + IsIntCheckByException("12")); } private static boolean IsIntCheckByException(String str) { try { Integer.parseInt(str); return true; } catch (NumberFormatException nfe) { return false; } } |
In this article, we’ll look at the String array convert to String. This can be useful for example to log query parameters.
Read MoreJxls is a small Java library to make generation of Excel reports easy. Jxls uses a special markup in Excel templates to define output formatting and data layout.
Read MoreLet’s assume we have a Java collection of Car objects that we want to output into Excel. The Car class may look like this
1 2 3 4 5 6 |
public class Car { private String brand; private BigDecimal price; // ... constructors // ... getters/setters } |
To use Jxls to output …
Read More