Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 19th, 2013, 07:52 PM
jayemes22 jayemes22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 jayemes22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Any suggestions? Thanks!

Reply With Quote
  #2  
Old February 19th, 2013, 08:32 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,824 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 8 h 43 m
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
Remember that a char is just an unsigned integer type.

Reply With Quote
  #3  
Old February 19th, 2013, 08:41 PM
jayemes22 jayemes22 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 jayemes22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 14 sec
Reputation Power: 0
I can't believe I forgot that simple fact.

Thank you!

Reply With Quote
  #4  
Old February 20th, 2013, 06:24 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
The conversion from char to int can be done by subtracting the value of '0' from the numeric char: '5' - '0' = 5

Reply With Quote
  #5  
Old February 23rd, 2013, 08:52 PM
burakaltr burakaltr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 46 burakaltr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
                            }}}             
Comments on this post
NormR disagrees: Don't spoonfeed code to OP

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Multiplying individual numbers in a string?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap