Searching for Y value in an array of point

Hopefully, someone can enlighten me ?

I have an array of point

int[] depth ={25, 30, 35, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190};

int[] minutes ={595, 405, 310, 200, 100, 60, 50, 40, 30, 25, 20, 15, 10, 10, 5, 5, 5, 5, 5};

Point[] points =new Point[depth.length];

for(int i = 0; i < points.length; i++)

{

points[i] =new Point(depth[i], minutes[i]);

}

JTextField tfXvalue =new JTextField();

String getXvalue = tfdepth.getText();

Say the input of getXvalue is 40. Then I do all of the necessary conversions from string to double etc...

I need to tell the program to look in the array, and return the Y value where X=40.

I haven't the slightest idea where to start. !!!!

Would someone please point me in the right direction ?

[1248 byte] By [va97a] at [2007-11-26 20:15:13]
# 1

one option is to use the usual searching algorithms, the easiest one being (but not the cheapest):

int foundY = -1;

boolean found = false;

for (Point p : points) {

if (p.getX() == 40) {

found = true;

return p.getY();

}

}

Clem1986a at 2007-7-9 23:21:50 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you I see how to go about doing this now.Thanks again
va97a at 2007-7-9 23:21:50 > top of Java-index,Java Essentials,Java Programming...
# 3
If the user enters 41 which is not in the array. how can i say since 41 is not in the array, go to the next number which will be 50 in this case ?Help from anyone ?
va97a at 2007-7-9 23:21:51 > top of Java-index,Java Essentials,Java Programming...
# 4

You can do the same thing as the first search but keep track of when the X goes from being less than the input value to being greater than. It would take longer to interate the array twice though so you might want to think about keeping track of that while you are doing the first search if the X values will always be in ascending order.

MrPicklesa at 2007-7-9 23:21:51 > top of Java-index,Java Essentials,Java Programming...
# 5

> If the user enters 41 which is not in the array. how

> can i say since 41 is not in the array, go to the

> next number which will be 50 in this case ?

> Help from anyone ?

Sort the point array by x values,

If you find the x value, return the y value,

If you pass the x value without finding it, return the next y value.

How to return the fact that the specified value wasn't found depends on your design.

hunter9000a at 2007-7-9 23:21:51 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks very very much for the hints guys.
va97a at 2007-7-9 23:21:51 > top of Java-index,Java Essentials,Java Programming...