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

Thread: Have I lost it?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Have I lost it?

    For some reason my boolean keeps getting changed. Any ideas why and where I screwed up?

    Class one
    public class PrimeTester {
     
    	  public static boolean isPrime ( int num )
    	  {
    	    boolean prime = true;
    	    int limit = (int) Math.sqrt ( num );  
     
    	    for ( int i = 2; i <= limit; i++ )
    	    {
    	      if ( num % i == 0 )
    	      {
    	        prime = false;
    	        break;
    	      }
    	    }
     
    	    return prime;
    	  }
     
    }

    Class Two (Problem Class)

     
     
    public static void main(String[] args) {
     
    		int currentNum = 2;
    		int numOfPrim = 0;
    		int currentPrime = 0;
    		boolean check = false;
     
    		while(currentNum < 10)
    		{
    			System.out.println("1st check " + check);
    			check = PrimeTester.isPrime(currentNum);
    			System.out.println("2nd check " +check);
    			if(check = true){
    				System.out.println("3rd check " +check);
    				numOfPrim = numOfPrim +1;
    				//System.out.println("Number of Prime: " + numOfPrim);
    				currentPrime = currentNum;
    				System.out.println("Current Prime " + currentPrime);
    				System.out.println("");
    			}
    			//System.out.println("3rd check " +check);
    			//System.out.println("Current Num " + currentNum);
    			//System.out.println("");
    			currentNum = currentNum + 1;
     
    		}
     
    		//System.out.println(currentPrime);
     
     
    	}
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Have I lost it?

    if(check = true){

    = is the assignment operator. == is the equality check operator.

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

    pooleman133 (September 7th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Have I lost it?

    Big thanks

Similar Threads

  1. Help, please. I'm lost
    By Borb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2010, 08:50 PM
  2. I'm lost
    By stoptheerrors in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2010, 08:47 AM
  3. Im lost....
    By brale76578 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 31st, 2010, 08:09 AM
  4. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM
  5. Need Help! I'm completly lost!
    By DeMolay8613 in forum Java Theory & Questions
    Replies: 0
    Last Post: January 11th, 2010, 01:21 PM