Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Boolean always true?? What am I doing wrong?

    First let me say that I've found a way to achieve my goal not using a boolean anyway, but I still can't let go of this problem until I figure it out and it feels cheap if I just forget about it.

    This program determines if a number is a prime number. After a loop which tests if the given number is divisible by 1 to 1 less than the given number (and is not 1 itself, and is not being divided by 1), it should switch the boolean value to false and break the loop. Otherwise, it keeps going until the loop breaks itself (the counter matches the number being tested) and the boolean value stays true.

    This sounds great and logical to me, but the boolean is ALWAYS staying true! The program is saying every number you enter is a prime number! What am I doing wrong?

    Code:
    Java Code:
    //declare and construct variables
    		int iNumber = 0, iPrimeCheck = 0;
    		String sOutput = "", sNumber = "";
    		boolean bIsPrime = true;
    
            sNumber = JOptionPane.showInputDialog("Enter a number: ");
    
    		iNumber = Integer.parseInt(sNumber);
    
    		for (iPrimeCheck = 1; iPrimeCheck < iNumber; iPrimeCheck++)
    		{
    			if (iNumber % iPrimeCheck == 0 && (iNumber != 1 && iPrimeCheck != 1))
    			{
    				bIsPrime = false;
    			    break;
    		    }
    		 }
    
    		 if (bIsPrime == true)
    		    sOutput = "Prime!";
    		 else
    		    sOutput = "Not Prime!";
    
            //display message to user with sOutput
    		JOptionPane.showMessageDialog(null,
    		                              sOutput,
    		                              "Prime Numbers",
    		                              JOptionPane.INFORMATION_MESSAGE);
    
    	    System.exit(0); //exit

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    This seems to work okay for me. It just told me that 8 and 10 are not prime. What values are you using to generate incorrect output?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try starting the loop at 2 and only test
    Java Code:
    if(number % i == 0)
    The code however looks fine, although it can be made to look a bit better.
    Last edited by sunde887; 03-02-2011 at 06:07 PM.

  4. #4
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    It also seemed to be working for me.

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    What the . .

    This thing is just frying my brain. For some reason I thought 3 was not a prime number and assumed I was getting bad results. Wow.

    These little Math assignments are the hardest part of learning JAVA. You have to go back to stuff you haven't learned about in 10 years. Its like building a deck on mud.

    Sorry!

Similar Threads

  1. actionListener & setSelected(true)
    By new2java2009 in forum New To Java
    Replies: 11
    Last Post: 01-13-2012, 01:46 PM
  2. while(true)
    By ravian in forum New To Java
    Replies: 7
    Last Post: 06-29-2011, 06:05 AM
  3. classes, true purpose of
    By mac in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 04:34 PM
  4. true && !true
    By SirFalcon in forum New To Java
    Replies: 3
    Last Post: 10-29-2009, 02:33 AM
  5. which one of the following is true about interface?
    By makpandian in forum New To Java
    Replies: 3
    Last Post: 06-30-2009, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •