According to the standard coding practice "Left curly braces should be located at the end of lines of code". e.g.,
/* Not a good coding practice*/
} else
{
}
/* Good coding practice */
} else {
}
Actually I ran Sonar on my Java project and because of the above mentioned Rule, I got more than 6000 errors. Is there any shortcut in eclipse or any other way to fix these errors(at a time)?
Open the java files(where errors occured) and use "Ctrl+Shift+F" key to format. Then code will be formatted according "Formatter" settings
Related
My problem with eclipse's auto format is that it's taking this:
public List<JSON> getIdentityQualifiers(String identity)
throws DatabaseException
{
//....code
}
to this:
public List<JSON> getIdentityQualifiers(String identity)
throws DatabaseException
{
//....code
}
Obviously, I want to keep the previous formatting, but I can't find where to control this.
Go to Project -> Properties -> Java Code Style -> Formatter and you will see the formatting options. There are a lot of different styles.
My company provides us with a custom XML which defines exactly how they want things formatted, which was created internally. Eclipse allows you to define your formatting exactly in this format. There will certainly be tutorials online if this is important to you. You may be able to write your own or find many examples in Eclipse marketplace and online.
You could also try like that , first select segment of code which one you want to indent for e.g
if(true)
{
System.out.println("INDENTED");
}
select and type Ctrl + Shift + F.
After that you get.
if (true) {
System.out.println("INDENTED");
}
For customization you can find from there *Project --> Properties --> Java Code Style --> *
I think basically it may have been my version of Eclipse or something obscure as this stopped happening a long time ago.
I would like to use Emac's org mode for taking notes of java snippets.
I would like the java snippets to be syntax highlighted.
I tried running Org-mode in minor mode and Java-mode in major mode, but I found this lacks a lot of Org-Mode features (e.g links).
I would prefer to run Org-mode in major mode and have some minor mode to do java syntax highlihgting for when it finds java syntax.
I would rather avoid the #+begin_src business as my file would be full of those.
Is this possible?
[Edit] in was thinking along the lines of soft syntax highlighting for none headings and non org-items . I.e general paragraph body?
The only mechanism of which I'm aware that supports syntax-highlighted code blocks in Org-mode is the source code block feature you have already mentioned.
Setting org-src-fontify-natively to t should enable syntax highlighting for such blocks:
(setf org-src-fontify-natively t)
Code blocks should look something like this:
* Pretty sweet Org heading
This is an org-mode file, which is cool for lots of reasons, e.g.
- it's Emacs, and
- it supports syntax-highlighted blocks
- (note that this requires the variable ~org-src-fontify-natively~
to be set to ~t~)
#+BEGIN_SRC java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
#+END_SRC
A few tips:
The quickest way to start a new code block is to type <s and then hit Tab. This expands to
#+BEGIN_SRC |
#+END_SRC
with the cursor represented by |, so you can just type java and start editing.
With point inside such a block, org-edit-special, bound to C-c ' by default, will open the code block up in a separate buffer with the appropriate major mode active. You can use the full power of that mode, then type C-c ' again to update the embedded snippet.
As you can probably understand from the question itself, I'm new to Java.
I was given an exercise to write a Java program which receives a character, prints it and the next character in the Unicode table.
Now, I have the solution to this exercise:
public static void main(String[] args){
char c = args[0].charAt(0);
char c1 = (char)(c + 1);
System.out.println(c + "\t" + c1);
}
I understand basic idea of this code, but I'm trying to run this code in Eclipse I get an annoying error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MainClass.main(MainClass.java:9)
Note: I have yet to run a Java program that actually receives something as a parameter so I guess it's a stupid beginners' mistake... Here is the full code that I tried to compile in Eclipse:
public class MainClass {
/**
* #param args
*/
public static void main(String[] args){
char c = args[0].charAt(0);
char c1 = (char)(c + 1);
System.out.println(c + "\t" + c1);
}
}
Thanks in advance
Select "Run -> Run Configurations" from the menu.
Search for you project in the list on the left and select it.
Select the "Arguments" tab on the right.
Write the argument you want to pass to the programm in "Programm arguments".
Click "Run"
Right click on your java file in project explorer of your eclipse. Then Run As> Run Configuration
Then you will get a window. Like-
Click on Arguments Tabs, and then write some text there, may be a character.
And then Click on Apply button and Run Button.
The default run configuration in Eclipse runs a Java program without any arguments, hence the ArrayIndexOutOfBoundsException. Your code is trying to get first element of the args array when there aren't any!
You can edit the run configuration to provide the arguments to run your program with. Then it should not throw this exception.
However, a good practice is to check the size of array before accessing it's elements, more so when the array is coming as an argument from outside of your code.
This is a great question with some very good answers. I would like to add some pointers about how to debug your own program. Debugging is as important (if not more important) than writing code.
For one thing, Eclipse has some great debugging features. You can use this debugger to find problems in your code. I suggest that you learn how to use it. In particular, you can set watches for variables to see what value they have as you step through the execution of your code.
Alternatively, you can add calls to System.out.println() to print out the values of any variables. For example, adding the following line at the beginning of your code might help you narrow down the problem:
System.out.println(args[0]);
This would also give an ArrayIndexOutOfBoundsException if no command-line arguments are given. Then you could do something like
System.out.println(args.length);
which would print out 0. This then gives you a clue as to where the problem is.
Of course, even when you get to this point, you still might not know how to solve the problem. This is where sites like StackOverflow come in handy.
Good luck with your Java experience. Please come back when you need more help.
If your Run Configurations are in place (as already shown in above answers):
Shortcut to Run a class is:
Ctrl + F11
Some of our code written a while have unnecessary semi-colon. I wonder whats the easiest way to remove them. For example, the last semi-colon in the following
if(i == 2)
{
System.out.println("if statement");
}
else
{
System.out.println("else statement");
};
You can find them easily enough by going into the Java Compiler / Error and Warnings preferences, then under "Potential Programming Problems" change "Empty Statements" to warning or error. Then it'll just be a matter of going through them. There may be a way of automating it, but I wouldn't bother unless there are loads :)
It is probably too late for you but, you can Analyze your code with Intellij IDEA using the Inspection "Unnecessary semi-colon" then after it has found them all you can apply the Fix.
http://www.jetbrains.com/idea/documentation/inspections.jsp
Eclipse can also do this too but it suffers from a bug that doesn't allow you to apply the Quick Fix in bulk.
As the above said, by typing ctrl + f and searching for }; and replacing it with } would work great, but could break the code.
They are not necessary to be removed, as it is just the equivalent of:
if(true){
//...
}
/*Empty Line*/;
It won't effect the code in any way, but is best to remove them just for preference.
It may be too late for you but this could help others:
CTRL + F
write what you want to delete in your case ; and then:
CTRL + ALT + SHIFT + J
This will select all the matches for your search in that file.
Then you just have to delete DELETE
I have to read java file by java code and to determine the greatest nested count of if statements in it.
for example:
if (someCondition)
{
if (someCondition)
{
// Expression
}
}
In this case program should display greatest nested if depth is 2.
Now the problem is that position of curly brace after if is uncertain.
for example it can be like :
Curly brace start and end comes in same line
if (someCondition){}
OR
Curly brace start in next line
if (someCondition)
{
}
OR
Conditions without curly brace
if (someCondition)
if (someCondition) // Single line without curly brace
Can anybody suggest what will be the best way to get the required nested count?
You'll need to parse the Abstract Syntax Tree (AST) of the Java source code. See Java library for code analysis. Once you have the AST, you can do a search to find the longest path of nested conditionals.
As the answer already said, you should rely on the AST rather than viewing code manually for this. The AST will never be wrong, your own reading abilities most often will.
I don't know a complete solution right now, but I suggest you spend some time looking at existing tools for computing software metrics. Nesting depth is a typical metric and there should be tools around.
If you can't find anything, you can at least fall back to writing something like an Eclipse plugin. In that case, you could simply load the Java file in the Eclipse editor, and Eclipse performs all the hard work for you and gives you the AST for free. Determining the nesting depth of a given AST is then rendered a simple task. Developing a prototype for that shouldn't take more than a few hours. And it's easy to extend it to cover your whole project and have it answer questions like "which java file in our project has the maximum nesting depth and what depth is that?". But then again.. someone else will surely point out an existing tool that already does this and much more.
I82Much's answer will certainly get you there, but feels a little like cheating.
Knowing little about your project, I would think that a simple stack mechanism with a max value record would do the trick push on { and pop on }. Once you have that basic model working, simply add the special case of control statements with one line bodies (this is valid for if, for, while ...). In those cases, you'll be looking for those keywords, followed by ( and a ). Once you've encountered that combination, if the scan encounters either another control statement or a semi-colon before it encounters a { then this is one of those special cases and you should push (using a special marker indicating to pop on ; rather than }).