### Thread: Multiplying individual numbers in a string?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
5
Rep Power
0

#### Multiplying individual numbers in a string?

I have a string that holds numbers (i.e. 123456789 ).

What I need to do is extract each individual number from the string, and multiply each number by 2. After doing this, I add together the new values of each number to get their sum.

Alternatively, I could add together each individual number from the string, then multiply their sum by 2.

I'm not allowed to use any class other than the standard String class, and arrays are not allowed.

I was thinking I'd somehow use a for loop to assign each position in the string to a new character, but I don't know how to convert character to int without importing a new class.

Any suggestions? Thanks!
2. Remember that a char is just an unsigned integer type.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
5
Rep Power
0
I can't believe I forgot that simple fact.

Thank you!
4. The conversion from char to int can be done by subtracting the value of '0' from the numeric char: '5' - '0' = 5
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
Originally Posted by jayemes22
I have a string that holds numbers (i.e. 123456789 ).

What I need to do is extract each individual number from the string, and multiply each number by 2. After doing this, I add together the new values of each number to get their sum.

Alternatively, I could add together each individual number from the string, then multiply their sum by 2.

I'm not allowed to use any class other than the standard String class, and arrays are not allowed.

I was thinking I'd somehow use a for loop to assign each position in the string to a new character, but I don't know how to convert character to int without importing a new class.

Any suggestions? Thanks!
You need the Logic here

Code:
```import java.util.Scanner;
import java.math.*;
public class baseConvert {
public static void main(String[] args)
{
double tab2,num,onluk,num2,tabson,
k;
double tab1,i;

double[] dig=new double;
double[] tab2lik=new double;
int[] tab2likint=new int;
while(true){
Scanner in = new Scanner(System.in);
System.out.println("  ");

System.out.println(" enter base 1 ");
tab1 = in.nextDouble();

System.out.println(" enter base 2 ");
tab2 = in.nextDouble();

System.out.println(" enter number 1 ");
num = in.nextDouble();
for(int i1=0;i1<=19;i1++)
{
tab2lik[i1]=0;dig[i1]=0;
}
onluk=0;
num2=num;
k=0;
for(int i1=0;i1<=19;i1++)
{
k=k+1;
dig[i1]=num % 10;
//if(dig[i1]<0){dig[i1]=dig[i1]+10;}
num=(num-dig[i1])/10;

//    System.out.println(dig[i1]);
}

//   onluk=dig[k]*tab1^k;

onluk=0;
// for(k=0;k<=50;k++){
// }
i=0;
for(int i1=0;i1<=k;i1++)
{
i=Math.pow(tab1,i1);
/* System.out.println(i);
*/

onluk=dig[i1]*(i)+onluk;

//

}
if(tab1==10){
onluk=num2;
}
if(num2<10){
onluk=num2;
}
// System.out.println(onluk);
tabson=0;

for(int i1=0;i1<=k;i1++)
{
tab2lik[i1]=onluk % tab2;
//  if(tab2lik[i1]<0)
//{
// tab2lik[i1]=tab2lik[i1]+tab2;
//}
onluk=((onluk-tab2lik[i1]))
;
onluk=onluk/tab2;
//  System.out.println(tab2lik[i1]);
// tabson=tabson+tab2lik[i1]*(10^i1);

//   System.out.println(tabson);

}

tabson=0;
System.out.println(num2+" in base "+tab1+" = "+" in base "+ tab2+" equals the number below :");

for( int i1=50;i1>=0;i1--)
{
//
//tabson=tabson+tab2lik[i1]*(10^i1);
tab2likint[i1]= (int) tab2lik[i1];

System.out.print("" + tab2likint[i1] );

//    System.out.println(tabson);
}
//  System.out.println(num2+" in base "+tab1+" = "+ tabson+ " in base "+ tab2);
}}}```

#### Comments on this post

• NormR disagrees : Don't spoonfeed code to OP