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

Thread: Help with While loop to continuously divide

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

    Default Help with While loop to continuously divide

    Hello, I'm working on a program that will accept a positive number typed by user and then tell you how many times that number can be divided by two before it reaches a number less than 1. If a negative number is entered by the user it should tell them it is invalid. This is what I have so far, but the loop isn't behaving like I need it to, not sure what's wrong. Any suggestions would be greatly appreciated.

    package project6a;
     
    import java.util.Scanner;
     
    public class Bits {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Scanner input = new Scanner (System.in);
     
    		System.out.println("Enter a positive number: ");
    		int userNumber = input.nextInt();
    		int count = 0;
    		int tempNum = userNumber / 2;
     
     
     
     
    		while (tempNum > 1){	
    			tempNum = tempNum / 2;	
    			count++;
     
    			System.out.println("The number " + userNumber + " is divisible by two " + count + " times");
    		}
     
    		if (userNumber < 0)
    			System.out.println("Invalid");	
     
     
    		input.close();		
    	}
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Help with While loop to continuously divide

    The while loop must contain all the behaviors that need to be repeated, and that includes getting user input, checking input, and warning if input is bad (negative). Your while loop doesn't have all these things inside it.

Similar Threads

  1. While Loop to divide by 2 continiously
    By jahshuwuh in forum Loops & Control Statements
    Replies: 1
    Last Post: November 22nd, 2012, 08:13 PM
  2. How come 1 divide by 2 is less than 0.5 ?!
    By raabhim in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 12:43 PM
  3. Plotting updated graph continuously
    By sudhan in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2011, 09:28 PM
  4. How to divide elements in two different arrays
    By BuhRock in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2011, 06:38 PM
  5. How can i divide a string?
    By noFear in forum Java Theory & Questions
    Replies: 9
    Last Post: September 1st, 2010, 08:45 AM