Discuss Multiplying individual numbers in a string? in the Java Help forum on Dev Shed. Multiplying individual numbers in a string? Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Posts: 5
Time spent in forums: 2 h 8 m 14 sec
Reputation 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.
Posts: 46
Time spent in forums: 9 h 55 m 44 sec
Reputation Power: 0
Quote:
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[100];
double[] tab2lik=new double[100];
int[] tab2likint=new int[100];
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);
}}}