java string digits,Counting the number of digits in an integer string in Java

2017-07-30 14:52:59

0

I have a char array (let's say of size 4). I am converting it into a string using:

String str = String.valueOf(cf); // where cf is the char array of size 4.

This String can contain 01 or 11 or 001 or 011, etc.

Now, I need to calculate the number of digits in this string. But every single time I calculate the number of digits(preferably in Java), it shows 4 as the result(Maybe due to the size 4). How do I calculate the no. of digits according to input string?

Example: If 001 is the input, it should give o/p as 3 and so on.

Here's the coding part :

static long solve(int k, long n)

{

// System.out.println("Entered Solve function");

char[] c = new char[4];

long sum = 0;

char[] cf = {};

for(long i=2;i<=n;i++)

{

cf = fromDeci(c, k, i);

String str = String.valueOf(cf);

//System.out.println(snew);

sum = sum + str.length() ;

}

return sum;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值