Format console text output in Java

I am writing a simple console console program. I can’t understand what is the easiest way to break text input from user. I take a diary note in a line, and then I want it to be able to print this line on the console, but not formatting it, of course, just showing the line in one long line across the terminal, which makes it terribly unfriendly to read. How can I show a line with a new line for each character x or so? All I can find about text formatting is System.out.printf (), but you only have the minimum number of characters to print.

+3
source share
4 answers

I would recommend using some external libraries, such as Apache Commons:

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html

and using

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#wrap(java.lang.String , int)

static final int FIXED_WIDTH = 80; String myLongString = "..."; // very long string String myWrappedString = WordUtils.wrap(myLongString,FIXED_WIDTH); 

This will wrap your string, observing spaces '', with a fixed width

WITHOUT EXTERNAL LIBRARIES

You will have to implement this:

By the way, I don't have a java compiler to test it, so don't get annoyed if it doesn't compile directly.

 private final static int MAX_WIDTH = 80; public String wrap(String longString) { String[] splittedString = longString.split(" "); String resultString = ""; String lineString = ""; for (int i = 0; i < splittedString.length; i++) { if (lineString.isEmpty()) { lineString += splittedString[i]; } else if (lineString.length() + splittedString[i].length() < MAX_WIDTH) { lineString += splittedString[i]; } else { resultString += lineString + "\n"; lineString = ""; } } if(!lineString.isEmpty()){ resultString += lineString + "\n"; } return resultString; } 
+7
source

If you can use the apache common lang library, you can use the WordUtils class (org.apache.commons.lang.WordUtils). if you ex:

 System.out.println("\nWrap length of 20, \\n newline, don't wrap long words:\n" + WordUtils.wrap(str2, 20, "\n", false)); [Source here][1] 

If you cannot use this feature, available on the programerscookbook blog. code for custom text packaging

 static String [] wrapText (String text, int len) { // return empty array for null text if (text == null) return new String [] {}; // return text if len is zero or less if (len <= 0) return new String [] {text}; // return text if less than length if (text.length() <= len) return new String [] {text}; char [] chars = text.toCharArray(); Vector lines = new Vector(); StringBuffer line = new StringBuffer(); StringBuffer word = new StringBuffer(); for (int i = 0; i < chars.length; i++) { word.append(chars[i]); if (chars[i] == ' ') { if ((line.length() + word.length()) > len) { lines.add(line.toString()); line.delete(0, line.length()); } line.append(word); word.delete(0, word.length()); } } // handle any extra chars in current word if (word.length() > 0) { if ((line.length() + word.length()) > len) { lines.add(line.toString()); line.delete(0, line.length()); } line.append(word); } // handle extra line if (line.length() > 0) { lines.add(line.toString()); } String [] ret = new String[lines.size()]; int c = 0; // counter for (Enumeration e = lines.elements(); e.hasMoreElements(); c++) { ret[c] = (String) e.nextElement(); } return ret; } 

This will return a string array, use a for loop to print.

+4
source

You can use the Java java.util.StringTokenizer to separate words with a run character and in a loop, after which you can add "\ n" after your favorite number of words per line.

This is not for every character, but perhaps it is not read as well as to separate words somewhere due to the number of characters that have been achieved.

0
source

Instead, you can use the following codes.

 String separator = System.getProperty("line.separator"); String str = String.format("My line contains a %s break line", NEW_LINE); System.out.println(str) 

You can link to this link. Java multi-line string

0
source

All Articles