Counting Number of Digits

Output problem

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6829 Views - Last Post: 22 August 2009 - 09:43 PM Rate Topic: -----

#1 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Counting Number of Digits

Posted 21 August 2009 - 08:46 PM

I got problem with the output, I cant get the right one.

Program needs to output the number and how many digits the number have. Example:

1000

4 Digits

Here is the code:

Method class:
import javax.swing.JOptionPane;

public class digit
{
	String output="";
	public int count_digits(int x)
	{
		output+=x+"\n";
		if (x<10)
			return 1;
			else
				return 1+count_digits(x/10);
	}
	public void Result()
	{
		JOptionPane.showMessageDialog(null,output,"output",JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
}


Main class:
public class digmain
{
	public static void main(String args[])
	{
		digit x=new digit();
		x.count_digits(9999);
		x.Result();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Counting Number of Digits

#2 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Counting Number of Digits

Posted 21 August 2009 - 10:59 PM

Here is my latest code and output:

import javax.swing.JOptionPane;

public class digit
{
	String output=" ";
	public int count_digits(int x)
	{
		output+=x+"\n";
		if (x<10)
			return 1;
			else
				output+=1+count_digits(x/10);
				return 1+count_digits(x/10);
	}
	public void Result()
	{
		JOptionPane.showMessageDialog(null,output,"output",JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
}


And the output looks like this:

9999
4999
399
29

How do I get rid of the 999 after the number 4 and what ever figures that follows it.

It should look like this this:
9999
4
Was This Post Helpful? 0
  • +
  • -

#3 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Counting Number of Digits

Posted 21 August 2009 - 11:28 PM

do you mean? :
9999
4
999
3
99
2
if it is what you mean then just try this code:
	public int count_digits(int x)
	{
		output+=x+"\n";
		if (x<10)
			return 1;
			else
				output+=1+count_digits(x/10)+"\n";
				return 1+count_digits(x/10);
	}


i just changed output+=1+count_digits(x/10); to i just changed output+=1+count_digits(x/10)+"\n";

This post has been edited by oQMr FoxQo: 21 August 2009 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Counting Number of Digits

Posted 21 August 2009 - 11:39 PM

output+=x+"\n"; take out the x so that it is just output+="\n";
Was This Post Helpful? 0
  • +
  • -

#5 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Counting Number of Digits

Posted 21 August 2009 - 11:43 PM

View Postsyfran, on 21 Aug, 2009 - 10:39 PM, said:

output+=x+"\n"; take out the x so that it is just output+="\n";

if he will take it out it will look like this i think :
4
3
2
Was This Post Helpful? 0
  • +
  • -

#6 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Counting Number of Digits

Posted 22 August 2009 - 12:24 AM

View PostoQMr FoxQo, on 21 Aug, 2009 - 10:43 PM, said:

View Postsyfran, on 21 Aug, 2009 - 10:39 PM, said:

output+=x+"\n"; take out the x so that it is just output+="\n";

if he will take it out it will look like this i think :
4
3
2


oh, my bad.

I vote you completely scrap the whole output variable thing. You are returning the number of digits. Just print what the original call to the function returns.
Was This Post Helpful? 0
  • +
  • -

#7 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Counting Number of Digits

Posted 22 August 2009 - 12:33 AM

The output should look like this

9999
4

Which means that there are 4 digits in the number 9999.
Was This Post Helpful? 0
  • +
  • -

#8 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Counting Number of Digits

Posted 22 August 2009 - 12:45 AM

get rid of the output variable and all references to it. then in your main method put,
int x = 9999;
System.out.print(x + "\n" + (new digit()).count_digits(x));

Was This Post Helpful? 0
  • +
  • -

#9 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Counting Number of Digits

Posted 22 August 2009 - 12:54 AM

View Postmetalloid, on 21 Aug, 2009 - 11:33 PM, said:

The output should look like this

9999
4

Which means that there are 4 digits in the number 9999.

do you want it in a recursion function ?
otherwise what Syfran wrote is the right thing
Was This Post Helpful? 0
  • +
  • -

#10 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Counting Number of Digits

Posted 22 August 2009 - 01:37 AM

Quote

do you want it in a recursion function ?
otherwise what Syfran wrote is the right thing


What i wrote is still recursive...
Was This Post Helpful? 0
  • +
  • -

#11 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Counting Number of Digits

Posted 22 August 2009 - 02:38 AM

It wont compile. I did what syfran suggested but it won't compile.
Was This Post Helpful? 0
  • +
  • -

#12 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Counting Number of Digits

Posted 22 August 2009 - 04:08 AM

ok try this code:
public class digit
{
	int x=9999;
	int h=1;
	public int count_digits(int x)
	{
				
		if (x<10)
			return 1;
			else
				{
				h+=count_digits(x/10);
				return count_digits(x/10);
	}
		}
	public void Result()
	{		
		JOptionPane.showMessageDialog(null,"There is "+h+" in the digit "+x,"output",JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
	public static void main(String[] args)
	{
		digit d=new digit();
		d.count_digits(d.x);
		d.Result();

	}
}

with me it worked perfectly
plz let me know if it helpful

This post has been edited by oQMr FoxQo: 22 August 2009 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Counting Number of Digits

Posted 22 August 2009 - 05:41 AM

The methods below are only for positive numbers and 0.
The recursive version is really simple, just do this,

public static int countDigits(int x){
		if (x < 10) return 1;
		else return 1 + countDigits(x/10);
	}



If you're not allowed to have it static, do this,

public int countDigits(int x) {
		if (x < 10) 
			return 1;
		else 
			return 1 + this.countDigits(x/10);
	}



The later would seem really stupid though.

Alternatively if you don't want recursion,

public static int countDigits(int x){
  int k = 1;
  while (x >= 10) {
	x = x / 10;
	k++;
  }
  return k;
}


This post has been edited by Gloin: 22 August 2009 - 05:47 AM

Was This Post Helpful? 0
  • +
  • -

#14 metalloid  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 11-September 08

Re: Counting Number of Digits

Posted 22 August 2009 - 06:35 AM

I made revisions with what Mr. Fox made, The only thing lacking now is the entry number output. What ever figure I put in the main class should also come out in the output. Output needs to look like this:

9999
4 digit

But when I changed the figure in the main class into 99999 on what Mr. Fox made it looked like this:

There is 5 in the digit 0

Here is my code after I made the revisions:

Method class:
import javax.swing.JOptionPane;

public class digit
{
	int h=1;
	String output=" ";
	public int count_digits(int x)
	{
		output+=x+"\n";
		if (x<10)
			return 1;
			else
				{
				h+=count_digits(x/10);
				return count_digits(x/10);
				}
	}
	public void Result()
	{		
		JOptionPane.showMessageDialog(null,""+h+" digit ","output",JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
}
}


Main Classs:
public class digmain
{
	public static void main(String args[])
	{
		digit x=new digit();
		x.count_digits(99999);
		x.Result();
	}
}


Output should have been:

99999
5 digit

But it only showed this:

5 digit

This post has been edited by metalloid: 22 August 2009 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#15 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Counting Number of Digits

Posted 22 August 2009 - 10:36 AM

dude the methode posted previousld by me is recursion x should have been initialized up there because after it enters the recursion methode it will change gradually to ~ between 10-1 (not 0 because the minimum number you will have to divide will be 11 with will gice the result 1(int number)) and if you will put this String output inside the recursion it will also change gradually because change_count(x) is changing and x is changing in the same time and the main pupose of this recursion is to find the how many digits there are and this is what you are acheiving and don't change the x from count_digits(here) just change it from : int x=here
just like this exactly you should write it:

public class digit
{
	int x=9999;
	int h=1;
	public int count_digits(int x)
	{
		
		
		if (x<10)
			return 1;
			else
				{
				h+=count_digits(x/10);
				return count_digits(x/10);
	}
		}
	public void Result()
	{
		
		JOptionPane.showMessageDialog(null,x+"\n"+h,"output",JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
	public static void main(String[] args)
	{
		digit d=new digit();
		
		d.count_digits(d.x);
		d.Result();
	}
}


This post has been edited by oQMr FoxQo: 22 August 2009 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2