Counting Number of Digits

Output problem

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:11 PM

All right, I'm really getting sick of this.

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



You return the int for a goddamn reason. If you can't make that work then I don't know what will help you.

Edit: tested the code, works perfectly.

This post has been edited by syfran: 22 August 2009 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#17 Atspulgs  Icon User is offline

  • D.I.C Addict

Reputation: 89
  • View blog
  • Posts: 507
  • Joined: 29-July 09

Re: Counting Number of Digits

Posted 22 August 2009 - 07:11 PM

I think your giving the guy to shortened versions. He should start doing everything in smaller steps so he could better see how the program and java works. Im saying this because im having some trouble following your logics as well. Give the guy a simple yet smaller step example. like mine here.

import java.util.Scanner;

class numCount
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();
		System.out.println("The number you entered is " +num);
		short count = 0;
		while(num>0)
		{
			num/=10;
			count++;
		}
		System.out.println("There are/is " +count +" digits in that number");
	}
}


Was This Post Helpful? 0
  • +
  • -

#18 destructo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-July 09

Re: Counting Number of Digits

Posted 22 August 2009 - 09:43 PM

How about this, will this work for you??

public static void main(String[] arg) {
		
		
		Scanner input = new Scanner (System.in);
		System.out.println("enter a number: ");
		String num = input.nextLine();
		
		countN(num);
		
		
		
	}

	public static void countN(String number) {
		
		System.out.println(number +" has " + number.length() +" digits.");
	}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2