1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PLEASE READ! Java, do-while loop

Discussion in 'Software Development' started by ElementOfMagic, Jan 2, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Hello. :)

    I was wondering whether anyone could tell me, whether I have written the below program correctly: :confused:

    Code:
    import java.util.*;
    public class Task5bDW
    {
    	public static void main(String[] args)
    	{
    		
    	int d = 0;
    	int i = 0;
    	int n = 10;
    
    	do {
    		d++;
       		n /= 10;
    	   } while ((d == 0) && (n != 0));
    
    	System.out.println(d);
    
    	}
    }
    
    Because 'n' is currently set to 10, the program is supposed to output the result:
    0
    1
    (It should start at 0 and count up 1 for every digit in the value of 'n'.)

    But at the moment, it only outputs 1, no matter what I set 'n' to.

    Any help/assistance would be greatly appreciated. (y)
     
  2. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Let me know if there's any more additional information you need to help me. :)
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    The loop will only run once because one of your conditions in the while statement is 'd==0'. After the code in the do block is run once, d will be 1 (because of d++) and when the code reaches the 'while' part, the condition will equate to false and the do block won't be run again.
     
  4. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52

    Ahh, I see where I went wrong now. :)
     
  5. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    If that is ALL you are doing...

    So... n /= 10 is the same as assigning:
    n=n/10... or 1... Why?

    So, in pseudo you are saying

    Code:
    do
    {
    d++ (while d is 0 and n isnot 0)
    }
    exit loop, print current value of d (1)
    
    So its only going to loop once, and then exit loop and print the value of D, which will be 1 since it only went throught the loop once.

    If you want to loop up to the value of n, then you need to change you while to

    Code:
    while {d < n}
    
    And drop the n /= 10, otherwise you will again be looping only 1 time.
     
  6. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    He has 2 conditions in his && that will both be true after one iteration.
     
  7. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    All that does is make the program print out the value of 'n'.
    What I want to do make it do is this:

    Starting with 0, the program increments the value of "d" by one for each digit in the number assigned as the value of "n".
    For instance; if "n" was set to 1, the code would generate 0 and if "n" was set to 10, the code would generate 0, 1.
     
  8. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    So you want n to increase the number of places each iteration, and d to reflect the number of places - 1?

    d = 0, n = 1
    d = 1, n =10
    d = 2, n =100
     
  9. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Yeah, that's what I want to do. :)
    Not quite sure how to do it, though. :confused:
     
  10. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    Do you want it to actually measure, or just appear to?

    Can you use arrays for this?
     
  11. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Actually measure, if possible.
    And no, I can't use arrays as I haven't covered those yet.

    Basically, I want the program I'm writing now to produce the same results as this one:

    Code:
    public class Task5b
    {
    	public static void main (String[] args)	
    	{
    		int n = 111;
    				
    		for(int d = 0; n != 0; d++)
    				
    		{
    			n /= 10;
    			System.out.println(d);
    			
    		}
    		
    	}
    }
    
     
  12. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    What about taking the log10?

    Code:
            int n = 0;
            int d = 0;
            
            Scanner input = new Scanner(System.in);
            System.out.println("Enter int to measure");
            n = input.nextInt();
            
            System.out.println(n);
            int myLength = (int) Math.log10(n);
            System.out.println("Number of Places: " + (myLength));
    
     
  13. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    This would actually be, in a do loop:
    Code:
    do{
    n /= 10
    d++
    System.out.println(d);
    }
    
    A for loop does
    for(declare counter; while condition, increment counter){code to loop}
     
  14. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    I haven't covered that yet either and that's not quite what I'm trying to do. :confused:

    If you run this program:
    Code:
    public class Task5b
    {
    	public static void main (String[] args)	
    	{
    		int n = 10;
    				
    		for(int d = 0; n != 0; d++)
    				
    		{
    			n /= 10;
    			System.out.println(d);
    			
    		}
    		
    	}
    }
    
    And see what that does, I think you'll get an idea about what I'm trying to do with my program.
    I want my program and this one ^ to have the same functionality, only mine must do it using a 'do-while' loop rather than a 'for' loop.

    For example if 'n' was 10, the program would print 0, 1.
    If 'n' was 101, the program would print 0, 1, 2.
     
  15. loserOlimbs

    loserOlimbs

    Joined:
    Jun 19, 2004
    Messages:
    7,800
    Code:
            n=111;
            d=0;
    do{
    n /= 10;
    System.out.println("D: " + d);
    ++d;
        }while (n!=0);
    
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1083423

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice