29

I'm using Checkstyle for Java in Eclipse IDE. I don't know in every java file, at second line, I always receive warning in Checkstyle : (although I'm pretty sure that I don't press tab key)

File contains tab characters (this is the first instance).

public class Percolation {
    private boolean[][] grid;
...
}

When I have typed : public class Percolation {, press enter, Eclipse will make an indent for me, after that, I type private boolean[][] grid;. But, I still receive this warning.

Please tell me how to fix this. (I don't want to turn off this warning, because this is a must. Another people will check again my file).

Thanks :)

hqt
  • 29,632
  • 51
  • 171
  • 250

8 Answers8

27

step 1 In eclipse, Preference > Java > Code Style > Formatter. Edit the Active profile.(If you don't wish to edit the built-in profile , create a new profile). Under "Active profile:" there is a drop down dialogue box .. beside it, click "Edit..." which opens up the edit dialog box. Under the "Indention" tab, you'll find Tab Policy. Change the Tab Policy from Mixed to Space only & apply the changes.

step 2 Back to your Eclipse Perspective, navigate via the menu bar: Source > Format Element (not "Format") and save.

Run checkstyle you won't find "File Tab Character: File contains tab characters (this is the first instance)." warning anymore.

To visualize the difference by enabling whitespace character that you'll find in tool bar.

compski
  • 709
  • 3
  • 13
  • 28
JToddler
  • 790
  • 1
  • 6
  • 16
25

In eclipse, go to Preferences > General > Editors > Text Editors and check the box for "Insert spaces for tabs". Then it will indent with spaces instead of tabs.

TimK
  • 4,635
  • 2
  • 27
  • 27
  • Sorry. I have changed to this option, but still receive this notice :( – hqt Aug 30 '12 at 05:13
  • 4
    Do you have the problem in new source files or in ones that already had the problem? For existing files you will need to re-indent them: select all of the text, then from the context menu, Source > Correct Identation. – TimK Aug 30 '12 at 05:21
  • 1
    I have problem in source that already had problem. I have selected all, correct indentation, and recheck again, it still has this warning :( – hqt Aug 30 '12 at 06:03
  • I deleted some first lines of code and typed again, still no improve – hqt Aug 30 '12 at 06:03
  • After above setting, eclipse's autoformatting ctrl+shift+F replaced tabs with spaces itself then problem solved. – Min Naing Oo Aug 11 '14 at 04:24
  • Unfortunately, I stil have the problem. I have hundreds of [ERROR] MyClass.java[808:1] (whitespace) FileTabCharacter: Line contains a tab character. I have Checkstyle configuration plugin for M2Eclipse 1.0.0.201503101518 com.basistech.m2e.code.quality.checkstyle.feature.f‌​eature.group : Eclipse Checkstyle Plug-in 6.11.1.201510102004 net.sf.eclipsecs.feature.group eclipse-cs.sf.net So what I have to do? – Aerox Oct 17 '15 at 18:46
5

another possibility is find and replace: - copy a tab - Got to Find and Replace Window find: paste the tab replace: type 4 whitespaces (or number of whitespaces a tab consists of) -> replaceAll

Felix Schmidt
  • 321
  • 1
  • 3
  • 13
4

For Java code:

step 1 In eclipse, Preference > Java > Code Style > Formatter. Create a new profile and initialize it with default profile. There is a drop down dialogue box .. beside it, click "Edit..." which opens up the edit dialog box. Under the "Indention" tab, you'll find Tab Policy. Change the Tab Policy from Mixed to "Space only" & apply the changes.

step 2 Correct Indentation of your code by ctrl+I. It will replace Tab with Space.

ST3
  • 8,826
  • 3
  • 68
  • 92
Gagan Mani
  • 151
  • 1
  • 3
3

To fix this Navigate to Preferences > Java > Code Style > Formatter. Then click on New > give name for the formatter > click on ok.

enter image description here

Once you complete this step, on the automatically popped up window which is the formatter you created, click on indentation > tap policy > select space only. Or if you come back later time you could click on edit and follow the same step.
enter image description here

in the indentation size I prefer to put 4 > click apply and close.

After that right click on the class file you want to format > source > format. This will cause the indentation to be spaces instead of tabs.

Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30
2

I was also facing the problem but I got the solutions.

Step 1 : Go to Window > Preferences > Checkstyle.

Step 2 : In the right side You will see the Global Check Configurations. There you will find that Two configurations are available. Select the configuration with Eclipse and click on Set as Default button and click on OK.

Hope this will solve your problem.

Nitesh Gupta
  • 215
  • 1
  • 11
0

For Java Editor

Click Window » Preferences Expand Java » Code Style Click Formatter Click the Edit button Click the Indentation tab Under General Settings, set Tab policy to: Spaces only Click OK ad nauseum to apply the changes.

For other editors refer here How do I change Eclipse to use spaces instead of tabs?

Community
  • 1
  • 1
-2

Simply fixed that style issue (triggered by scalaStyle) by removing TAB with SPACE indent :)

27P
  • 1,183
  • 16
  • 22