Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Array loop problem which returns the difference between the value with fixed value

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Array loop problem which returns the difference between the value with fixed value

    Good Morning

    I hope I can explain a problem that I've tried to code but having problems with.

    I have an array of 20 numeric values and the each value is passed (in a loop) along with a fixed value (1.50) to a function that returns the difference between the two.

    I have the function coded and working correctly.

    for instance

    array[0] - 1.50 = 3
    array[1] - 1.50 = 2
    array[2] - 1.50 = 0.5

    etc etc

    This has to be done in the loop until the the program finds the first element in the array with a difference of less than 1.

    So for instance in the example above if the value of array[2] = 2. then the loop would stop there as it's the first element in the array with a difference < 1.

    I can't get the loop structure coded correcty, I trust I need a while loop and a for loop.

    Hope you can see the problem and help if possible. Please let me know if you need clarificatiion.

    Regards

    JS


  2. #2
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: Array Loop Problem

    It would help if I saw your function but ill give it a go:

    for(i=0;i<array.length;i++)
    {
    if(array[i]-1.50< 1)
        {
           break;
    }
     
    }
    Last edited by Fendaril; May 14th, 2009 at 02:52 PM.

  3. The Following User Says Thank You to Fendaril For This Useful Post:

    uplink600 (May 15th, 2009)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Array Loop Problem

    Hey uplink600,

    Can you post the code you have so far please?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    uplink600 (May 15th, 2009)

  6. #4
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array Loop Problem

    Thanks Fendaril

    I think I'll be OK with that. At the break point I would like to assign a value to a variable that equals the element in the array where it breaks.

    Such as var breakPoint = point in array where break occured.

    Incidentally the user has now requested I code this in Javascript but I can adapt any code you advise.

    Thanks Again

    JS

  7. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Array Loop Problem

    for(i=0;i<array.length;i++){
       if(array[i]-1.50< 1){
            x = array[i];
            break;
       }
    }
    Please don't ever get confused between Java and JavaScript.

    Chris

  8. #6
    Junior Member
    Join Date
    May 2009
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array Loop Problem

    Thanks Chris

    I won't confuse them and I know they arent the same. It's just the user decided he wants to distribute the code as an HTML file for users to run in Firefox.

    Cheers for your help.

    Regards

    JS

Similar Threads

  1. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  2. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM
  3. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM
  4. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 01:32 PM