What's going on with CheckStyle's line length check? - java

I have CheckStyle set to check for lines over 80 characters in Eclipse, and I have a margin line set up in my editor at 80 characters. If I put my cursor at the end of a line of code in my editor, the co-ordinates read (for example) 1433, 77, indicating the 77th character column from the left - yet when I run CheckStyle over the same line it says the line is 88 characters long! There are no extraneous tabs or other whitespace characters at the end of the line, it's definitely 77 long. Is CheckStyle broken?

From the Checkstyle documentation:
The calculation of the length of a line takes into account the number of expanded spaces for a tab character ('\t'). The default number of spaces is 8. To specify a different number of spaces, the user can set TreeWalker property tabWidth which applies to all Checks, including LineLength; or can set property tabWidth for LineLength alone.
http://checkstyle.sourceforge.net/config_sizes.html#LineLength
To do this in Eclipse:
Open Window->Preferences from the Menu.
Select Checkstyle.
Type TreeWalker into the search box under Known modules.
Select TreeWalker on the list on the right.
Click Open.
Change the tabWidth to 4.
Click OK, and OK again.

Is CheckStyle broken?
Probably not.
I expect you/Eclipse and CheckStyle have a different idea of the width of a TAB character. It sounds you think it means 4 spaces, whereas CheckStyle thinks it means 8 characters (its default).
One way around this is to configure Eclipse to not use TAB characters in your source file, then re-indent your source files.
Another way is to make use that CheckStyle and Eclipse agree on the TAB width; e.g. see #MartinEllis's answer.
(I prefer the first approach because it means that my source code will look correctly indented, irrespective of the platform's default TAB width; i.e. Windows versus Linux/Unix. Hard TAB characters in source code are a bad idea.)

Related

EDI Hex Character Issues

So we're exporting records from a Filemaker Pro database and submitting those files to our vendor via EDI, but their system cannot accept our files because of some really wonky ".." (Hex 0B) characters that show up at the beginning of every new line in the text document.
I've read that there are issues with how Filemaker Pro exports their files and because of that these Hex 0B characters can't be deleted beforehand, from within Filemaker. I have a limited understanding of Java, would there be an adequate Java based run-time solution that could be created to fix this problem?
I've tried exporting the text file in question in every available export method, including .mer, .csv, .tab, etc, with every combination of output file character set available for each export type, some create even more issues with hidden characters. I ended up settling on .tab (ASCII [DOS]) as it had the lowest amount of residual information, but I still need to get rid of these 0B characters for our EDI integration partner to be able to accept our files without any issue.
EDIT: Added more information for clarity.
The hex value "0B" in ascii is a vertical tab.
This chart is a good reference point for figuring out the hex values for specific ascii values and vice versa.
Getting the vertical tab out of the text field is a bit irritating but do-able. I had to do it for a series of documents we imported into filemaker as text and then moved to a postgresql database. What I did was the following:
Downloaded the free application Hex Fiend (this app allows you to enter text and see it's hex value or hex and see it's text equivalent)
Enter the value 0B into the left panel of Hex Fiend (the hex side)
Click into the right panel of Hex Fiend (the text side) and then click the menu command Edit -> Select All to select the entire contents of the text side and then Edit -> Copy to copy the contents (your vertical tab text character) to the clipboard
In FileMaker, create a script with script step:
Set Field[myTable::myField ; Substitute ( myTable::myField ; "*pasteVerticalTabCharacterHere*" ; "" )]
Paste your vertical tab character in between the first set of quotes of the substitute function
Note that when you do this, your substitute function will probably break to two lines
Leave the second set of quotes in the substitute function blank
When you fire this script on a record that contains vertical tabs in myfield it will substitute them for a blank string "". You could also change the substitute function to switch the vertical tab out with a carriage return like so:
Set Field[myTable::myField ; Substitute ( myTable::myField ; "*pasteVerticalTabCharacterHere*" ; ¶ )]
If you have a number of records that need this set field step run on you could create a script that loops through all of the records and fires this step.
You could also do it the quick and dirty way of:
Show all records that need to be cleaned
Click into the field to be cleaned
From the Records menu, select Replace Field Contents
Enter the substitute calculation above as the replace calculation
Hit replace
This will go through and clean the vertical tabs out of all of the records in one shot.
In the end, if you have a particular field or fields that you know will be likely to have vertical tabs in them, I would suggest putting an auto-enter calculation on the fields that contain a modified version of the substitute calculation above:
Substitute ( Self ; "*pasteVerticalTabCharacterHere*" ; "" )
This way, whenever you import records into the table that contain vertical tabs, the vertical tab character is cleaned out automatically.
I should also note that hexfiend is a mac app. I think on a windows computer you can just type the vertical tab character out by pressing control+K, but I haven't tried it.
Hope this helps!

Eclipse HotKey: How to switch between indenting levels based with spaces?

Switching from Tabs to Spaces in our Java project, I am looking for the Eclipse shortcut so basic that I can't remember it which allows, on a given line, to move the caret to the next "group of spaces".
With tabs, it is pretty easy, because they count as a single character, so just using arrow keys is okay, but with spaces, if, let's say, the indentation level is based on 4 spaces, Left or Right arrows must be pushed down 4 times before accessing the next indentation level.
Thanks!
Default indentation: Ctrl+I
Right indentation: Tab
Left: Shift+Tab

Insert line break in editor

I'm trying to add a line break with the Find function in eclipse - What I want to do is put a line after each method in all my classes [I liked them all close together but now its hard to read through]. so I figure theres 2 ways to do this.
1. Find all with "public" and change to "\npublic", \n being the new line in a string to give you the idea of what I mean.
2. A convenient eclipse shortcut that separates all the methods in a class with an empty line.
Any ideas on how to achieve this? I'll be happy as long as it works and I don't have to go through every method in the project myself and push enter .-.
For #2, you can set the Eclipse auto-formatter to do so for you.
Look under the Preferences panel; in the left side (tree), select Java Code Style (click the arrow to expand the section), then Formatter. You can then decide to use a single formatting style for all of your projects, or check the box to apply a special set of formatting rules to just the current project. Some of the options include where to locate whitespace and newlines.
You can also use the “Save Actions” panel to re-apply the same formatting rules every time you save your files. Personally, I use (and team members as well) this method, to ensure that the formatting for the files is always consistent. By mechanically reformatting in this way, changes to the file will always show up “properly” in Subversion/Git — I don't accidentally have whitespace-only changes being introduced into files showing up as “false positive” changes.
Check box 'Regular Expressions' and use '\R' for Linebreak in the 'Replace with' box in eclipse
configure the auto-formatter its a bit of work for the first time but its worth, when ever you get ugly formatted code you can fix this in seconds ..

Why does my source code (written in Eclipse) look different in other text editors?

I've been using Eclipse to do my CS assignments, as recommended by my professor. However, I've noticed that if I open my source code in a different text editor, my beautiful, perfect formatting looks wrong. I believe the problem lies in tabs. A tab character seems to take up less space in eclipse than in other text editors.
A good chunk of our grade is determined by the neatness of our code. I'm not sure if our programs are graded in eclipse or not, so I'd like to figure out how to make source code have the same formatting regardless of text editor.
Is this a problem with Eclipse? Are there settings I can fiddle with?
This is probably due to your settings for the tab symbol. If you really want to indent using tab, make sure it is set to 8 spaces everywhere.
From the Java coding convention:
Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).
Personally I always use spaces to indent my code due to the fact that some people have their tab symbol set to show as 4 spaces.
To set Eclipse to always use spaces, go to
Window -> Preferences -> Java -> Code style -> Formatter -> Edit
and set Tab policy to Spaces Only.
The tab size is probably different in other editors. It still is in its raw form a \t. Depending on what the editor will display when it encounters one is probably found in the preference. Either way it should be consistent in size all the way across the file.
You could also convert tabs to spaces so it is always the same regardless of editor.
The tab character does not have a defined display width. In notepad, it is displayed as the same width as 8 spaces wide. All code editors should allow the viewer to change the displayed tab width. The convention for code is normally a width equivalent to 4 spaces.
If you're desperate, you could replace all tabs with 4 spaces. However, this is frowned upon by some, and may lose you marks. I'm pretty sure that the advised java coding style advocates the use of tabs, not spaces.
At the end of the day, the marker is a fool if he'll turn down syntax highlighting, and the code editor that comes with it.

how to turn off break line in eclipse

Does anybody know how to turn line breaking in eclipse after you press CTRL + SHIFT + F (code format), ex :
System.err
.println("Incorrect file name, make sure you include .extension with your file name");
Go to Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit and then pick tab Line Wrapping and set Line Wrapping policy to Do not wrap.
The default line wrapping in Eclipse is really rather irritating and the main reason why is that the 'line width' is set as 80 characters, clearly a hangover from the nineteen eighties. This really should be updated now that everyone uses high resolution monitors instead of 80 char wide punch cards.
You probably don't want to turn it off entirely but instead you want to change the 'line width' to something more sensible (otherwise when you use the auto formatter you may get lines of code that go on forever).
I would suggest a value of 140, which seems to work nicely for most of us now using 1080p monitors.
In order to do this you need to...
With-in Preferences, navigate to...
> Java
> Code Style
> Formatter
You can't edit the default profiles so you have to hit New... and type a name like 'Eclipse 140 Line Width' for your profile and OK.
Then Edit the profile
Change to the Line Wrapping tab
Change the Maximum Line width from 80 to 140.
Then OK your way out.
If you then want to fix up your existing code, select your target code and use the Source > Format menu option.
In Helios, set 'Line Wrapping/Function Call/Indentation Policy' to 'Indent on Column' for all cases. This seems to stop the pathological case where it splits a line to try and fit within a fixed width but ends up with it further to the right than it would have been unsplit.
Or, as suggested above, disable line wrapping, or set a very wide line width. But I find it better to have it wrap and get it right than not to wrap at all.
There is an option "never join wrapped lines". This will help to keep our custom wrapping unchanged. Great Feature.

Categories

Resources