1

Given a string as input, convert it into the number it represents. You can assume that the string consists of only numeric digits. It will not consist of negative numbers. Do not use Integer.parseInt to solve this problem.

MyApproach

I converted string to char array and stored the original number but I am unable to convert it into a single number I tried converting individual elements but the digits can be of any length.So,It was difficult to follow that approach.

Hint:I have a hint that the numbers can be added using place values

For e.g if the number is 2300.I stored each number in the form of arrays.Then it should be 2*1000+3*100+0*10+0=2300

But I am unable to convert it into code.

Can anyone guide me how to do that?

Note I cannot use any inbuilt functions.

public int toNumber(String str)
{
   char ch1[]=str.toCharArray();
   int c[]=new int[ch1.length];
   int k=0;
   for(int i=0;i<c.length;i++)
   {
     if(ch1[i]==48) 
     {
        c[k++]=0;
     }
     else if(ch1[i]==49)
     {
        c[k++]=1;
     }
     else if(ch1[i]==50)
     {
        c[k++]=2;
     }
     else if(ch1[i]==51)
     {
        c[k++]=3;
     }
     else if(ch1[i]==52)
     {
        c[k++]=4;
     }
     else if(ch1[i]==53)
     {
        c[k++]=5;
     }
     else if(ch1[i]==54)
     {
        c[k++]=6;
     }
     else if(ch1[i]==55)
     {
        c[k++]=7;
     }
     else if(ch1[i]==56)
     {
        c[k++]=8;
     }
     else if(ch1[i]==57)
     {
        c[k++]=9;
     }

   }
 }
Jason arora
  • 550
  • 3
  • 8
  • 22
  • Not even Math.pow?, loop backwards use an var that you multiple by 10 (to multiple nr with) and sum them up.. – Petter Friberg Dec 05 '15 at 17:11
  • 1
    Consider a switch statement instead if if... it easier to read... – Petter Friberg Dec 05 '15 at 17:13
  • 2
    `if(ch1[i]==48) ` -> [avoid magic numbers](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). `if(ch1[i]=='1')` is much clearer. – Pshemo Dec 05 '15 at 17:19

5 Answers5

4

You don't need to do powers or keep track of your multiplier. Just multiply your running total by 10 each time you add in a new digit. And use c - '0' to turn a character into a number:

int n = 0;
for (int i = 0; i < str.length(); i++) {
    n = n * 10 + str.charAt(i) - '0';
}

So for example for 1234 it goes

0 * 10   + 1 = 1
1 * 10   + 2 = 12
12 * 10  + 3 = 123
123 * 10 + 4 = 1234
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
  • Short, solution-only version of my answer, except I was trying to not just hand the homework answer to OP. Oh well. ;-) – Andreas Dec 05 '15 at 17:24
  • @Andreas hadn't read your answer before posting, otherwise I probably wouldn't have bothered. Wasn't so much trying to hand the OP the homework as point out the other solutions were unnecessarily complex. "Solution-only" is a bit harsh though, I did explain (if briefly) what it does. Anyway, you got my vote :) – CupawnTae Dec 05 '15 at 17:32
3

A digit character ('0'-'9') can be converted into an integer value (0-9) using:

ch - '0'

This is because the digit characters are all consecutive in ASCII/Unicode.

As for calculating the full number, for the input 2300, you don't want:

2 * 1000 + 3 * 100 + 0 * 10 + 0

Instead, you'll want a more incremental approach using a loop:

r = 0
r = r * 10 + 2   (2)
r = r * 10 + 3   (23)
r = r * 10 + 0   (230)
r = r * 10 + 0   (2300)

This is much better than trying to calculate 1000 (Math.pow(10,3)), which your formula would require.

This should be enough information for you to code it. If not, create a new question.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

If you loop through the char array you have and take the last value, put it through an if statement and add to an to number integer whatever that number is (use 10 if statements). Next go to the second to last value, and do the same thing only this time multiply the resulting numbers by 10 before adding it to the total number. Repeat this using 1 * 10^(value away from end) being multiplied to the number gotten from the if statements.

SupremeSteak1
  • 136
  • 11
1

Well what comes to my mind when seeing this problem is to multiply the numbers you are getting with your current code with the place they have in the charArray:

int desiredNumber = 0;
for(int k=1; k<=c.length; k++) {
    desiredNumber += c[k] * (Math.pow(10, c.length - k));
}

If you are not allowed to use the Math.pow() function then simply write one yourself with aid of a loop.

Greetings Raven

Raven
  • 2,951
  • 2
  • 26
  • 42
-1

You can do

int no = 0;
for(int i = 0; i < c.length; i++){
    no += c[i] * Math.pow(10, c.length - 1 - i);
}