# Solved: PLEASE READ! Java, do-while loop



## ElementOfMagic (Dec 30, 2012)

Hello. 

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


```
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. :up:


----------



## ElementOfMagic (Dec 30, 2012)

Let me know if there's any more additional information you need to help me.


----------



## andythepandy (Jul 2, 2006)

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.


----------



## ElementOfMagic (Dec 30, 2012)

andythepandy said:


> 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.


Ahh, I see where I went wrong now.


----------



## loserOlimbs (Jun 19, 2004)

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


```
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


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


----------



## loserOlimbs (Jun 19, 2004)

andythepandy said:


> 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.


He has 2 conditions in his && that will both be true after one iteration.


----------



## ElementOfMagic (Dec 30, 2012)

loserOlimbs said:


> If that is ALL you are doing...
> 
> So... n /= 10 is the same as assigning:
> n=n/10... or 1... Why?
> ...


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.


----------



## loserOlimbs (Jun 19, 2004)

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


----------



## ElementOfMagic (Dec 30, 2012)

loserOlimbs said:


> 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


Yeah, that's what I want to do. 
Not quite sure how to do it, though.


----------



## loserOlimbs (Jun 19, 2004)

Do you want it to actually measure, or just appear to?

Can you use arrays for this?


----------



## ElementOfMagic (Dec 30, 2012)

loserOlimbs said:


> Do you want it to actually measure, or just appear to?
> 
> Can you use arrays for this?


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:


```
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);
			
		}
		
	}
}
```


----------



## loserOlimbs (Jun 19, 2004)

What about taking the log10?


```
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));
```


----------



## loserOlimbs (Jun 19, 2004)

ElementOfMagic said:


> 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:
> ...


This would actually be, in a do loop:

```
do{
n /= 10
d++
System.out.println(d);
}
```
A for loop does 
for(declare counter; while condition, increment counter){code to loop}


----------



## ElementOfMagic (Dec 30, 2012)

loserOlimbs said:


> What about taking the log10?
> 
> 
> ```
> ...


I haven't covered that yet either and that's not quite what I'm trying to do. 

If you run this program:

```
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.


----------



## loserOlimbs (Jun 19, 2004)

```
n=111;
        d=0;
do{
n /= 10;
System.out.println("D: " + d);
++d;
    }while (n!=0);
```


----------



## ElementOfMagic (Dec 30, 2012)

loserOlimbs said:


> ```
> n=111;
> d=0;
> do{
> ...


Ahh, thanks a lot, that's *just* what I was looking for! :up:
I can't tell you how grateful I am for all your help, today.


----------



## andythepandy (Jul 2, 2006)

Your original code is good all except that while condition which causes the do block to only run once.

Simply changing 

```
while ((d == 0) && (n != 0));
```
to

```
while (n != 0);
```
will fix the program. You want the code in the do block to run as long as n isn't zero, as when n is zero you have counted all the digits.

Working code:

```
import java.util.*;
public class Task5bDW
{
	public static void main(String[] args)
	{
		
	int d = 0;
	int n = 23452358;

	System.out.print(n + " has ");

	do {
		d++;
   		n /= 10;
	   } while (n != 0);

	System.out.println(d + " digits");

	}
}
```
I added another print statement to give a more user friendly output

Andy


----------

