Reputation: 1462
I am trying to create a method which return a int value(in my case locationY is the value which i wanted to return) which i am getting from a variable which is declared and initialized inside a for loop in thge same method. I tried two ways to do so. In my 1st try code i got error which says "locationY cannot be resolved to a variable". Then in my 2nd try i tried to return the locationY from inside the for loop. But again got an error which says that "This method must return a result of type int". Can anyone please tell me how can i use the varable present inside a for loop to the same method.
1st Try -
public int getvalueY() throws InterruptedException{
for(int i=0; i<20;i++){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
int locationY = (int)point.getY();
Thread.sleep(2000);
}
return locationY;
}
2nd Try -
public int getvalueY() throws InterruptedException{
for(int i=0; i<20;i++){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
int locationY = (int)point.getY();
Thread.sleep(2000);
return locationY;
}
}
Upvotes: 0
Views: 109
Reputation: 2438
You should give a more detailed context as the loop does not do anything in this case. You just return in the very first cycle. The first and the second solution is totally different. In your first solution you will get the mouse position after waiting 20*2000 millisecond + you should declare the variable in the same scope where you use it. While in your second solution you will get it in 2000 milliseconds - so the loop does not have any meaning
Upvotes: 1
Reputation: 1527
In your first try, you're declaring locationY
in the for loop.
So this variable is only known within the loop and is unknown in the line with the return statement.
If you declare & initialize it outside the loop, you can use it in both places (in- and outside the loop:
public int getvalueY() throws InterruptedException{
int locationY = 0;
for(int i=0; i<20;i++){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
locationY = (int)point.getY();
Thread.sleep(2000);
}
return locationY;
}
I'd recommend to look up scoping for Java i. e. the visibility of variables in Java.
Upvotes: 2
Reputation: 10907
Try#1 : use this
public int getvalueY() throws InterruptedException{
int locationY = 0
for(int i=0; i<20;i++){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
locationY = (int)point.getY();
Thread.sleep(2000);
}
return locationY;
}
Try#2 : use this
public int getvalueY() throws InterruptedException{
for(int i=0; i<20;i++){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
locationY = (int)point.getY();
Thread.sleep(2000);
return locationY;
}
return 0;
}
Upvotes: 1
Reputation: 10623
public int getvalueY() throws InterruptedException{
int locationY=0;
for(int i=0; i<20;i++){
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
locationY = (int)point.getY();
Thread.sleep(2000);
}
return locationY;
}
This will put locationY
outside of the scope of the loop, so you can return it at the end.
Upvotes: 1