Eclipse Autoformat Parenthesis - java

I have a problem whenever I use the Scanner class in my java code:
Say I type
Scanner in = new Scanner(System.in)
As soon as I press the semicolon, it becomes/autoformats to this:
Scanner in = new Scanner(System.in;
This is the only case where this issue occurs, I have never seen it happen anywhere else when coding. I also haven't found anything in the autoformat preferences that sounds like a solution to this.
I would like to know how to fix this.

I can reproduce this behaviour both in Eclipse Mars and Eclipse Neon, and for me it looks like a bug.
In particular, when I press ) to over-type the auto-inserted ), the content assist window suggesting System.in stays open, and ; then selects it and eats the ).
Therefore I opened a bug report for it:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=506729
To work around it (until it is fixed), you can disable one of the involved features: auto-inserting closing parenthesis, content assist (i. e. auto completion) or that content assist triggers on typing a semicolon. (All of these are available in the global preferences unter Java -> Editor). Alternatively you can live with the bug and close content assist either by accepting System.in with Enter, or by pressing Esc any time after the suggestion window opens but before typing the semicolon.

Related

How to break {} naturally when I press Enter in Netbeans

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.

Eclipse - pressing space deletes the parenthesis before caret

It is hard to describe so see this GIF:
When I press space to continue typing, Eclipse Java editor decides to delete the second half of a parenthesis pair. I can press ESC to prevent this, but it is annoying.
I must have missed/tweaked some settings without knowing it, because in my working station, same as in my home, I am with Eclipse Mars 2 and I had never seen this.
It also happens if I press ; to end the line, and there is a parenthesis before the ;.
What should I change in Preference to stop it?
This is is a overtype mode in eclipse.
To disable it, press the Insert/Ins key on your keyboard and let me know if it helps.

How do I rename all Scanners or object?

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.

Eclipse isn't overwriting parenthesis

This isn't really a coding problem, but more of just an Eclipse problem.
Say you're typing x = (1), Eclipse will make the closing parenthesis for you the moment you type '('
It also normally writes over the closing parenthesis as you are typing as well.
However, for some reason when I'm typing, it doesn't automatically write over it and I'm confused because I'm pretty sure I didn't hit any special setting so I have no idea what to do to revert this.
So normally you'll type x = (1), and it'll show as x = (1), but right now it's showing x(1)). How do I change it back??
Thanks for the help!
Check in the Settings Content Assist for your Editor. It has a setting to overwrite or not. And in my case it specifically mentions you can toggle that with Ctrl Key while the assist is active. The other setting related is Editor -> Typing. It also has some brace-close settings.
1 way to solve this problem is by turning off Automatically Closed parentheses. Open the preferences for editing, then go to Java -> Editor -> Typing and turn off parentheses.

Eclipse content assist not working in some parts of code

I am using Eclipse Juno and have never had any problems with it, until its content assist stopped working only in some parts of my code. The code below shows what I am talking about:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
mWTBatch.setText(mRTBatch.getText());
mWTExp.setText(mRTExp.getText());
mWTName.setText(mRTName.getText());
mWTQuantity.setText(mRTQuantity.getText());
/* Here content assist is not working */
}
});
mWTBatch.setText("Here it is working again");
That piece of code is just inside one method of my class. The problem is that it works fine inside the methods of the class, but inside objects (new ActionListener(){}) it returns "No default proposals". It has this behavior when I either use "Ctrl+space" or type in "." after the object I want to get suggestions of. I've already searched here for the solution and googled it, but couldn't find a solution. What I have already tried:
Window->Preferences->Editor->Content Assist->Advanced and checked Java Proposals. It didn't work. I've even tried checking it myself, without just using Restore Defaults. I also tried the Java Proposals from the other table. Nothing. At last I tried checking all the fields with Java on them. Nothing worked.
Deleted my workspace folder completely, created a new one and imported my project. As my project is from an SVN repository, I used the project folder inside "trunk" folder, but I didn't copy the content of the folder to the workspace when importing (I need it to be in the repository). Still nothing, it continues with the error.
I reproduced the same situation of the code above into another class of another different project, which was also imported to the workspace, but having its content copied to it. The content assist worked for that one.
I then re-imported the project with the original code from above, but this time copying its content to workspace (which means it had nothing to do anymore with the SVN repository). It didn't work either.
I also checked if the ctrl+space is bind to the content assist on Eclipse settings and it is ok. I also checked if it had nothing to do with advanced key settings of Windows 7 language bar. I had seen on the internet that could be problem, but it was not my case.
I even created a new Java project on the workspace and copied the .java files one by one from the old project, set all the build path manually and in the end it still doesn't work.
For all the first four cases above I also cleaned the projects and closed and reopened them, not forgetting to refresh it after each action. Could anyone tell me how to fix this? I would like to find a solution to this problem, so that others may not be like me on it for 2 days. I think my question is important because I am gathering here many probable solutions found by googling the problem and from StackOverflow, and nothing solved it.
I stumbled into this issue the other day, and after seeing this post thought that I'd have to just live with it.
However, I did find a "hack" around it:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener( //<---------------------- Problem arises because we're inside a function declaration ...
new ActionListener()
{ // <--------------------------------------------- ... yet we're trying to write a function
public void actionPerformed(ActionEvent e)
{
/* Here content assist is not working */
}
}
);
mWTBatch.setText("Here it is working again");
From the content assistants point of view, this is just plain wrong, so we need to give it a little help:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(
new ActionListener()
// <--------------------------------------------- CURLY BRACKET MISSING
public void actionPerformed(ActionEvent e)
{
/* Here content assist IS WORKING */
}
}
);
mWTBatch.setText("Here it is still working");
This will obviously give you an error upon compilation time, but it gives you full access to the content assist for the rest of the function declaration.
Also, it doesn't matter which curly bracket you remove from inside the function declaration, so long as it's an opening curly bracket.
One other point, if you only remove the opening curly bracket like I did above, then eclipse will NOT add in another closing curly bracket automatically for most cases (because by the time you enter the new opening curly bracket, you've equalised the opening vr closing of curly brackets). You can get around this by deleting the closing curly bracket as well, but then you have to remember to put two curly brackets back.
Hope that helps the 1165 views this question has garnered over the past year =)
Do a search for "Change display language" on the start menu. 'Change keyboards' > Look to see if you have any non English keyboards under 'General' > 'Installed services'. If you have more than just English then pressing Ctrl+Spacebar is changing your focus from Eclipse to the language selector on the taskbar. Remove any other keyboard languages from the list if that is your problem.

Categories