Is there an option to do the following in InteliJ IDEA automatically? Or some macro or something?
From:
a.equals(b)
To
b.equals(a)
There are times when there is a NPE in the first operand and so it breaks the flow. Like when comparing some string from an object to an enum value. Clean code says it should be like enum.equals(value) and I want to do this at the press of keyboard shortcut.
If you put your cursor anywhere within a.equals(b) (even if it's after the closing parenthesis) and then press Alt-Enter, then you'll get a menu which includes "Flip '.equals'", which does exactly what you want.
An alternative way to get to that action is to press Ctrl-Shift-A to open the action search menu and enter any part of the name such as "flip".
Related
I have a habit when writing code:
If I want to write a code block inside a {}, I will type {} first, then move the cursor into between { and }, press Enter and write my code.
In IntelliJ IDE, after I press Enter, IDE auto break and format for {} for me to write code (see this image with green arrow).
With Netbeans, it doesn't work like that (see the image with red screen).
My question is how to setting Netbeans IDE to work like IntelliJ to convenient for me to write code.
I have never written in NetBeans before. But I can understand you - it is so inconvenient and painful.
I have been going through all the combinations and haven't found the one needed.
The options that might repeat this behaviour somehow include:
Typing a { and pressing the enter. Code completion will automatically add the second brace and put you in the right place.
Using the combination cmd+enter and enter.
Then I figured out that we can write a macro:
split-line insert-break
The result:
Unfortunately, the macro is contextless. We can't write the condition "if the caret is between braces do our action otherwise, do the standard one". So, it can be assigned to another hotkey (not enter) to make enter work correctly in its cases.
e.g., I have named my Scanner object readsir" throughout my code.
[Scanner readsir= new Scanner(new File("word.txt"));]
However, I am worried my professor would expect a different Scanner or object name. Every time I highlight one "readsir" in the program, all the "readsir"s are highlighted. So, if i want to change all the "readsir"s to objIn, is there any way to do that? I am new to eclipse and java programming so I am sorry if it is too obvious.
I went through the program and changed all the words manually, but one of my programs is 700 lines long, so I would like to discover a new way for long programs.
select the variable -> right click->source->refactor. Then type in the new name of the variable and press enter
Or
Select the variable and press, Alt+Shift+R
Assuming you are using eclipse you have a shortcut for that. Highlight the variable name you want to rename and press alt+shift+r, you'll see a box around the name. Rename it and press enter, the name should change everywhere that same variable is used.
If you're using another IDE you'll have a similar solution. Check with a right click or in the menu bar on top and search for a tool named refactor or refactoring.
In Eclipse, when you click on a method name and press the F3 button, it takes you to that method.
How can I do the reverse of this? I have a method that's overloaded (probably about 12 different instances of it), and I would like to quickly find if this particular one is being called from anywhere else.
I know you can click on the method, press Ctrl+ H, and Search for this Method in the Workspace. Is there a simpler keyboard shortcut for this?
Click on the method and press Ctrl+Shift+G to perform a Search for References in Workspace.
Ctrl+Alt+H opens the Call Hierarchy, which sounds like what you're looking for.
How can I watch the contents of several variables (for example, TreeSet's) simultaneously? I can watch contents of one TreeSet, clicking on it in "Variables" window, but I have no idea how to do that for several variables.
You can use Expressions windows: while debugging, menu window -> Show View -> Expressions, then it has place to type variables of which you need to see contents
You can add a watchpoint for each variable you're interested in.
A watchpoint is a special breakpoint that stops the execution of an application whenever the value of a given expression changes, without specifying where it might occur. Unlike breakpoints (which are line-specific), watchpoints are associated with files. They take effect whenever a specified condition is true, regardless of when or where it occurred. You can set a watchpoint on a global variable by highlighting the variable in the editor, or by selecting it in the Outline view.
You can do so by these ways.
Add watchpoint and while debugging you can see variable in debugger window perspective under variable tab.
OR
Add System.out.println("variable = " + variable); and see in console.
And how about selecting the text you want to watch, and then using the shortcut "Ctrl + shift + I"
When I am coding Java in Eclipse I like the auto-completion feature. With that I mean the popup with method-names that comes when you start typing in a method name for an object. Or maybe it's called something different, i.e. method-suggestions?
But the popup is hidden if I misspells a method name, and it doesn't come back if I delete the misspelled part of the method name. Is there any way to get back the popup after a misspelling without starting to type in the hole method name again?
Press Ctrl+ (Blank). For a complete list of keyboard shortcuts have a look in the eclipse "Preferences" and there "General/Keys".
You should also check out Preferences->Java->Editor->Content Assist. You'll be able to select how it acts; things like if you use it in the middle of a word should it insert or overwrite, should it show deprecated methods, the delay before it automatically appears, and it can even (try to) guess your method parameters based on the variables in the current scope.
I think you're after the Ctrl-Space keyboard shortcut.
(In Eclipse this is called Content Assist. In Visual Studio it's called IntelliSense.)