-3

I am doing FIFO LRU and Optimal. I have a problem for Optimal ( replace it with the one that we will not use in the longest time, so the furthest one). I got it to replace my fram number with the one that i dont use in the longest. But the problem is that when the number that i want to replace it with doesn't even exist HOW DO I STOP MY IF statesman? And im not sure which "if" to stop? i tried this:

public int toss( int pr ) 
{
// if we get the same number just return -1

for (int s=numberOfFrames-1; s>=0; s--) 

{
    // if we get the same number dont kick anything 
    if ( pr == fram[s]) 

    {

        return -1;
    }
}

// find which frame to replace

int look2 = 0; // this is the co for the number you want to replace with.
// next time it is used.
int co = 0; // this is index to pra.
int r = -1;

   for ( int d=numberOfFrames-1; d>=0; d--) 
   {
       lookFor(d,r);



   }


   if (r == -1) 

   {
       //item not found, handle however you want, one suggestion is:
       return -1;  //have the caller handle this correctly
   }
   else

   {
       int q = fram[r]; // remember 2 which is page we are getting raid off
       fram[r] = pra[co]; //  fram one we want to get raid off and replace it with the                       pr
       return q; // return the one we kicked
   }

}

int co = 0; int look2 = 0;

public int lookFor(int d, int r) {

   for( int f=co; f>=0; f++) // f looping for pra

   {
       co = tossCallCount++;

       System.out.println("here");

       if( fram[d] == pra[f])

       {
           System.out.println("by"+pra[f]);

           if(look2<=f) 

           {
               System.out.println("hi"+fram[d]);
               r = d;
               look2 = f;
               break;

           }
       }
   }
   return d;

} }

user2989685
  • 11
  • 1
  • 8
  • As a note - if you going to deal with java further, please read [java code convention](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html). Applying this rules to your code will made it shorter and clearer. – msangel Nov 21 '13 at 16:32
  • 2
    `System.exit(0);` will terminate the JVM. Is that what you want? – Thomas Nov 21 '13 at 16:36
  • 1
    `break;` is likely what you want to stop the looping – WOUNDEDStevenJones Nov 21 '13 at 16:36
  • Initialize `r` with -1 and after the loop check whether it is still -1, if so you haven't found the value you're looking for. – Thomas Nov 21 '13 at 16:38
  • @Thomas, I want to terminate my IF statement, so when I dont get the number, I want it to stop? – user2989685 Nov 21 '13 at 16:41
  • You can't _stop_ an if statement, so I assume you want to stop the loops, right? Look for "labeled break", since you most likely want to break the outer loop from within the inner one. But I also assume you want to stop _if_ you find the number, since if you don't find it you'd have to continue looking for it until both loops terminate normally. – Thomas Nov 21 '13 at 16:45
  • @WOUNDEDStevenJones, um so do i put the break; in the same place as the System.exit(0); – user2989685 Nov 21 '13 at 16:46
  • Thomas, yes stop the loop. But what I know is I keep on going when I find the number and when I dont find the number then stop or maybe continue until i find a number and replace with? – user2989685 Nov 21 '13 at 16:49
  • That logic is wrong for searching. If you look at the first number and it doesn't match, then you must!! continue to search for it. You can't stop 'when you don't find the number' or you would only ever look at the first number. You need to exhaust your list before determining that you didn't find it. – WOUNDEDStevenJones Nov 21 '13 at 16:57
  • Ok so from what i understood i need to add the break at the end after everything is done? – user2989685 Nov 21 '13 at 17:06

1 Answers1

0

If you break out of your loop at the place you have the System.exit(0), you will only ever look at pra[co] (because f=co, rather than looping through with pra[f]). You likely want to put the break inside of the if, so when you find the match, you stop this loop. But this will just exit you from the inner for loop, and you probably want to stop looping entirely. See Breaking out of nested loops in Java for more information on breaking multiple loops.

int r = -1;
for ( int d=numberOfFrames-1; d>=0; d--) {
    for( int f=co; f>=0; f++) {
        if( fram[d] == pra[f]) {
            if(look2<=f) {
                r = d;
                look2 = f;
                break;
            }
        }
    }
}
if (r == -1) {
    //item not found, handle however you want, one suggestion is:
    return -1;  //have the caller handle this correctly
} else {
    int q = fram[r]; // remember 2 which is page we are getting raid off
    fram[r] = pra[co]; //  fram one we want to get raid off and replace it with the pr
    return q; // return the one we kicked
}
Community
  • 1
  • 1
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
  • Ok, that where i put it and now it is returning -1 the whole time? – user2989685 Nov 21 '13 at 17:14
  • @user2989685 -1 is meant to be a special value indicating that nothing was found, so at the end you need to check whether you get -1 or not and act accordingly. – Thomas Nov 21 '13 at 17:35
  • What is one way to check whether i get -1 or not? Also when i had my break; out side it gave me -1s and 9s? – user2989685 Nov 21 '13 at 17:38
  • outside I meant outside the loop – user2989685 Nov 21 '13 at 17:39
  • See my edit, you'll likely want to set `r = -1` initially, then handle the result after the loop. Remember that currently the `break` is only exiting the inner loop, the outer loop is still running to completion. – WOUNDEDStevenJones Nov 21 '13 at 20:37
  • Thank you. I did it but i still get -1 every time even when i shouldn't get it? – user2989685 Nov 22 '13 at 01:08
  • My only suggestion at this point then is to either print out each value that you're comparing against or use breakpoints to actually view the data set you're using to make sure that what you're looking for is actually there. It may be a code error, or it may be that the value truly isn't in the set, so it's returning -1. – WOUNDEDStevenJones Nov 22 '13 at 17:01
  • So I did some changes to the code if you done mind looking at it. And I put the double nested for loop in another method and called it, then i added lookFor(d,r); And I am not sure how to go from there? – user2989685 Nov 26 '13 at 00:16