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 8 of 8

Thread: Why the Random pre-defined functions create an error in the code?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Why the Random pre-defined functions create an error in the code?

    Hi all,
    I have a problem with my code when I add this line:

    inject=aRandom.nextBoolean();

    I have a loop more than what it is supposed to be. Iterations should stop at 10 but it continues until it reaches 13 or 15.
    Here is my code:
    import java.util.Random;
     
    public class Test1_FT1 {
       static boolean inject=true; 
       static boolean previnj=true;
     
       static Random aRandom = new Random();
     
       public static boolean check(int v1, int v2){
          inject=aRandom.nextBoolean();
          if(v1<v2){
             if(inject){
                return false;
             }
             else
                return true;
          } 
          if(inject)
             return true;
          return false;	  
       }
     
       static int nmax=10;
     
       public Test1_FT1() {
     
       int i=0;
       while((check(i,nmax))!=(check(i,nmax)));
     
       while((check(i,nmax))) {
          System.out.print("nmax: ");
          System.out.print(nmax+"\n");
          i++;
          }
       }
     
       public static void main(String[] args) {
     
          new Test1_FT1();
     
          }
    }
    Last edited by jps; November 15th, 2012 at 12:51 AM. Reason: Parts of Code was Missing *added code tags*


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    Iterations should stop at 10 but it continues until it reaches 13 or 15.
    The posted code does not compile. Make sure you post the same version of the code you are having problems with.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    Quote Originally Posted by jps View Post
    The posted code does not compile. Make sure you post the same version of the code you are having problems with.
    OOOOPS .. Am so sorry, I just edited the code. Thank You.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    What is the code supposed to do?

    What does the first while loop do?
    while((check(i,nmax))!=(check(i,nmax)));

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    Quote Originally Posted by jps View Post
    What is the code supposed to do?

    What does the first while loop do?
    This is only to make sure that no Error occurs. Kind of fault avoidance that's why I compute it twice.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    Quote Originally Posted by Sharmeen View Post
    This is only to make sure that no Error occurs. Kind of fault avoidance that's why I compute it twice.
    I think you do not understand what happens then. Consider the first loop:
    while((check(i,nmax))!=(check(i,nmax)));
    So while (this) does not equal (that) where 'this' and 'that' are calls to the same method with the same parameters (0 and 10)
    Now inside the method, inject=aRandom.nextBoolean(); places a boolean value in inject
    if(v1<v2){ Well 0 is less than 10 every time so this is always true.
    if(inject){ inject holds the random boolean value assigned above and in theory has a 50/50 chance of being heads or tails.
    So what happens is the method now returns either true or false depending on the value of the random boolean.
    Pretend this time it returns true. Now the whole process starts over again at calling the method for the second time in the first while loop.
    The steps take place with 0 and 10 again, and this time if the boolean generated is false, the while loop ends. If the method returns true this time, the while loop goes to it's body, which is just a semicolon so it does nothing, and the iteration starts over again. This loop continues to run until the random boolean returns a true and a false value on the same iteration. When that finally happens (silently and quickly behind the scenes) everything else continues as if nothing happened. There are no side effects of the line of code other than wasted processor time.

    If you do not follow what I am saying I suggest you do some reading about while loops in java.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    Thank you very much for the reply. But even if I took off that line, it doesn't print correctly. Any other suggestions??

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why the Random pre-defined functions create an error in the code?

    Well you still have not told me what you expect the code to do, so I can't say much about what might not be cooperating...

Similar Threads

  1. how to wrap a pre-generics library
    By frumious in forum Object Oriented Programming
    Replies: 2
    Last Post: April 9th, 2012, 09:58 PM
  2. Help with using pre-created objects and Vectors **First Post :)**
    By jimmyo88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 18th, 2011, 08:08 AM
  3. Need help with some random java code.
    By shootingrubber in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 5th, 2011, 12:07 AM
  4. Inserting pre programmed UI elements
    By luisp88 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 20th, 2011, 12:56 PM
  5. Print Content on Pre Printed Sheet
    By kalees in forum Java Theory & Questions
    Replies: 0
    Last Post: February 24th, 2010, 03:26 AM