Is it possible to automate line breaks with System.out.println()? - java

I am running a java program from the command line that prints a relatively long string retrieved from a file. Though the string is printed accurately (I am using System.out.println(string)), the line breaks are consistent with the size of my terminal window, which results in words being cut off in the middle and then continued on the next line. Is there a way to change the system print settings so that the string will still fit inside the window but breaks will only occur where there is white space or a hyphenation? Or is the only way to do this to build a simple GUI and use HTML? Thanks!

Related

Java tabs ("\t") not working using FileWritter

As you can see, after every text comes a tab, everything is working fine, but after third tab (see output) it generates a space not a tab.
fileWriter = new FileWriter(indexFile, true);
fileWriter.append(id).append("\t");
fileWriter.append(String.valueOf(idx)).append("\t");
fileWriter.append(String.valueOf(pageCount)).append("\t");
fileWriter.append(postal.toUpperCase()).append("\t"); <-- this one
fileWriter.append(address.toUpperCase());
fileWriter.append("\r\n");
My output:
00000347 1 1 FB-6666 DUMMY STREET 1 LAT
The problem comes after "FB-6666".
Any ideas on this?
No, it really is generating a tab - it's just that whatever you're using to view the file is deciding to handle tabs by aligning them to some boundary or other. If you make your postal value FB-6666x I suspect you'll then see a much larger space.
This isn't a problem with the file content at all.
If you want to enforce a certain number of spaces between columns, you'll need to write that many spaces. Alternatively, something else to view the file...
Tabstops don't work this way. As the wikipedia article states,
Tab stops are set manually, and pressing the tab key causes the carriage to go to the next tab stop. In text editors on a computer, the same concept is implemented simplistically with automatic, fixed tab stops.
This means the tabstop will stop at a predefined position in the textfile, not after let's say the space it would take to insert a specific amount of spaces.
It is writing a tab I promise - to convince yourself of this you should either open the resulting file in a hex editor and look at the value in that position, or add another character to the value before the tab and see how the format of the output changes.

Java: Display changing text on output

Currently, my program prints to System.out:
Processing line 1 of 3...
Processing line 2 of 3...
Processing line 3 of 3...
Is there any way to have it replace the line numbers, instead of printing a new line?
Well there are some hacks you can try, but none of them are guaranteed to work everywhere. Escape characters such as \b (backspace) can erase the last character and \r (replace) can erase the last output.
It may or may not work (depending on IDE), but you could try:
System.out.print("Hello");
Thread.sleep(1000);
System.out.print("\rWorld");
However, generally a Swing box should be used (dynamically updating GUI) to measure progress. See Progress Bars.

JTextArea indentation

I'm having a problem with JTextArea (not a problem though).
I want to create my own college program, one of the function is to write down and save a list of homework each day. It's text area having the main function here, which is to type everything my lecturer said. But text area will act like Windows original notepad, which is not keeping the indentation. So my questions are:
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
If number 1 is possible, then how do my program have behaviour when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Edited: I'm a beginner in Java, in truth, I'm making this program while studying Java in my college.
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
You need to use key bindings to listen for the user typing Shift+Tab.
If number 1 is possible, then how do my program have behavior when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Use a similar piece of logic to capture the Enter key presses. Then, check the previous line in the editor and count the number of tab characters preceding the text. Output a similar number at the start of the new line.
you could use Javascript/jquery for indenting by inserting empty space of specific line. while pressing your hot key combination call function to insert five spaces on your cursor.

JTextArea JLabel compare two txt. files line by line

everybody. I have completed my code two txt files by showing on the same area.
but I want to compare two txt files line by line. and I want to show the differences with Colored lines and finally, The letter or words that are different, different line I'm going to bold.
how can i start?
thanks for now, my code is here.
JTextArea might make a suitable view, but you still need to model the differences. I'd look at the Eclipse or NetBeans platforms, or perhaps svnview.
You might need to ask user to upload both versions of file.
In the server you need to compare line by line, and store the line numbers of the file differences in another file or location
Then, while displaying, get the line numbers and give them proper decoration (i.e bold etc.).

how to set application name with line break?

how is it possible to make a line break for the application title? I would like to have the name on two lines under the icon amongst the installed programs on phone, like:
Harry's
App
In the xml file, setting the app_name pretty much returns a one-line title, or cuts it when the end of space is reached.
Use \n for a line break in your XML. You can use <br> too, but Eclipse might pop that up as a problem
\n didn't work for me \r\n did the trick

Categories