Solved: PLEASE READ! Java, do-while loop

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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. :)
 
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.
 
Joined
Jun 19, 2004
Messages
7,800
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

Thread Starter
Joined
Dec 30, 2012
Messages
52
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.
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.
 
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
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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:

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);
			
		}
		
	}
}
 
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));
 
Joined
Jun 19, 2004
Messages
7,800


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

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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));
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Members online

Top