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.
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.
i've been looking around and haven't been able to find any solution to this problem: i have a JTextField and i want to do some things when the user paste something in there, i've found this: What event to use when pasting something in a JTextField?
which works ok, except that i want only to do things when the user paste something, not when it writes on the text field, i've though of saving the previous value of it and compare it with the new, and if it was empty and now is not, do things, but this won't work since it will enter in that condition when the user types the first letter in the text field.
If anyone knows how to do it whit the documentListener or whit any other listener it would be of grate help.
Update: since various people has asked, the reason i want to do this is because the text will come from a bar code reader or some similar device.
except that i want only to do things when the user paste something
Why should pasted text be treated any different than typed text? Sounds like a design issue. If you specify a better reason/requirement for doiong this we might be able to come up with a better solution.
i want to do some things when the user paste something in there
You might be able to override the paste() method of the JTextField. Just override the method to invoke super.paste() and then add your custom code.
how to do it whit the documentListener
Maybe you would consider a "paste" to mean more than one character is added at a time. In which case you just test the length of the String that is added to the Document.
I was able to fix my problem by configuring my bar code scanner and making it send a "new line" after each reading, and executing my code every time this happens with the actionPerformed of the JTextField. Thanks to everybody who tried to help.
I will do my best to explain-- I am trying to make a choose-your-own-adventure type game while using a TextField and a TextArea, where what is written in the TextField is appended into the TextArea (this I know how to do via ActionListener).
However, I need to have the TextArea start with a pre-written 'intro', which asks the user at the end if they want to continue or not. Therefore, I need it to be able to scan the user's response ('yes' or 'no') and choose the appropriate selection of pre-written text to follow.
I don't want to overwrite what is already in the TextArea, I want to add to it. I suppose what I'm confused about it how I'm supposed to lay out the entire file so that it functions properly, because the different choices for the adventure span different methods. Having
"String text = textField.getText();" only within the actionPerformed method means I can't use 'text' elsewhere, but moving that line up with my other variables tells me it can't reference the field before it's defined.
I am fairly new to Java and am working on this as a project for a non-programming school course. I've been through many iterations thus far and this is what seems to be my final attempt, as I've remade it repeatedly and don't have much time left. :(
Your questions/comments and my attempts to answer:
I am trying to make a choose-your-own-adventure type game while using a TextField and a TextArea, where what is written in the TextField is appended into the TextArea (this I know how to do via ActionListener).
As per my comment, be sure to create a Swing GUI which would use a JTextField and a JTextArea. You would then add your java.awt.event.ActionListener to the JTextArea, and the ActionListener would respond whenever the user pressed <ENTER> within the JTextField.
However, I need to have the TextArea start with a pre-written 'intro', which asks the user at the end if they want to continue or not. Therefore, I need it to be able to scan the user's response ('yes' or 'no') and choose the appropriate selection of pre-written text to follow.
This can be done easily, but sounds as if you may be trying to shoe-horn a linear console type program into a GUI. If so, consider reconsidering your program design since what works best for one often doesn't work well for another. If you do re-write, then you should consider redoing most including your program flow, but excepting perhaps the "model" portion of your previous program, the "business logic" that underlies everything.
I don't want to overwrite what is already in the TextArea, I want to add to it. I suppose what I'm confused about it how I'm supposed to lay out the entire file so that it functions properly, because the different choices for the adventure span different methods. Having "String text = textField.getText();" only within the actionPerformed method means I can't use 'text' elsewhere, but moving that line up with my other variables tells me it can't reference the field before it's defined.
Again as per comments a JTextArea has an append(String text) method that will add new text to existing text that is already displayed in your JText Area. So on that note, your ActionListener's actionPerformed method could be very simple and look something like:
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
textArea.append(text):
}
Although you may need to add line feeds, "\n" either before and/or after the text you are going to append.
I want my Java application to ask for specific arguments (like name and email) before running in Eclipse. How can I make it do that?
It's not part of the Java code - it's part of the run configuration.
Go to the Run drop down (the down-arrow next to the run button), select Run Configurations, and then find your program and select it.
Go to the Arguments tab, and then in the Program Arguments click on "Variables..." to get a list of possible variables for Eclipse to use. For example:
${string_prompt:Foo}
That will prompt the user for a value for "Foo" when you then run that configuration.
Edit the run configuration of the class you want to run, go to the Arguments tab, click the Variables... button under the Program arguments text box, and select one of the *prompt variables. Do that for each argument.
While this approach would not technically ask the user for input before the java application starts running, it could accomplish basically the same thing:
You could use an easy-to-use SWING class, JOptionPane. This way, the input prompts would be a part of the application, but if you put them at the very beginning of the main() method, it have roughly the same effect as prompting before the application starts. Using this, you'd add something like the following to your source code, at the very beginning:
String name = JOptionPane.showInputDialog("Please enter name:");
String email = JOptionPane.showInoputDialog("Please enter email:");
Here is a reference to how the JOptionPane class works.
Using this, a popup box in which the user can enter text will appear when the application is started.
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.)