How would I go about renaming a variable in only part of the code?
For example:
System.out.println("Rectangle 1: " + "\n" + "Width: " + r1.width + "\n" + "Height: " +
r1.height + "\n" + "Color: " + r1.color + "\n" + "Area and Perimeter: " +
r1.getArea(r1.width, r1.height) + ", " + r1.getPerimeter(r1.width, r1.height));
So if I want to type out the same for a second rectangle using r2 as the refVar, is there a way I can quickly do this? I tried copy and pasting then using Alt + Shift + R, but, it ends up changing all of the r1 refvars.
I suggest you to use Find/Replace dialog for this problem it suites for your need. Select a set of statements then press Ctrl + F . A Find/Replace dialog will popup, note that in Scope group Selected lines option is selected.
You can use Replace All or Replace/Find . But be careful that it also replaces the string in comments if found.
Refer picture below.
Take a look at don't repeat yourself (DRY) principle which was designed to prevent situations like yours. Instead of renaming variables create separate method which will accept any Rectangle and print its details.
public static void printRectangleDetails(Rectangle r){
System.out.println("Rectangle 1: " + "\n" + "Width: " + r.width + "\n" + "Height: " +
r.height + "\n" + "Color: " + r.color + "\n" + "Area and Perimeter: " +
r.getArea(r.width, r.height) + ", " + r.getPerimeter(r.width, r.height));
}
Now you can use it with your r1
and r2
rectangles when needed
printRectangleDetails(r1);
...
printRectangleDetails(r2);
If for some reason where you can't create separate method and use DRY principle you can do something like this:
lets say we have
String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);
String bar = "bar";
System.out.println(foo+" hello wordls"+ foo);
and you want to replace foo
in second print statement to bar
. Using Alt + Shift + R (or from menu: Refactor
-> Rename..
) on second printing statement would rename all foo
references . To prevent it redeclare your foo
reference (compiler will give you error, but don't worry, we will later remove it, it is helpful only while renaming process) just before statements from which you want to change foo
to bar
like
String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);
String bar = "bar";
String foo = "whatever";
// ^^^
System.out.println(foo+" hello wordls"+ foo);
Now use Alt + Shift + R on this new duplicate foo
and eclipse will look for foo
from this new reference, and ignore earlier foo
s, so you should be able to see something like
(as you can see first two foo
are not selected for renaming)
so you can change it to bar
like
String foo = "foo";
System.out.println(foo+" hello wordls"+ foo);
String bar = "bar";
String bar = "whatever";
System.out.println(bar+" hello wordls"+ bar);
and after that just removed this additional String bar = "whatever";
since you don't need it any more.
BUT BE CAREFUL. This way you will rename all foo
variables after duplicate foo
, not only those in System.out.println(foo+" hello wordls"+ foo);
which you wanted to rename. To make sure you are not changing anything you don't want to, place code you want to change at the end of your method (where you are sure there is no foo
which shouldn't be changed after it). After you are done move your changed code to place you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.