Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need guidance on adding characters

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Need guidance on adding characters

    Right now I have three stacks. Each stack holds characters, with the normal operations of pushing and popping. I am trying to compare two characters from stack1 and stack2(each character is a digit character). I would like to be able to do arithmitic on the digit characters and the push the answer on to a result stack.
    Ex.) I pop character '3' from stack1 and '5' from stack2. Then I add the two. equaling '8' and pushing that character on to the result stack. This will hopefully in turn allow me to add arbitrary large numbers that integers will not support.

    I am having trouble because I believe I am getting some ascii character values when I pop off the result stack. here is a small piece of my code

    public void addition()
    	{
    		char temp1 ,temp2;
    		int i = s1.getSize();
     
    		for(int j= 0;j<i;j++)
    		{
    			temp1 = s1.pop();
    			temp2 = s2.pop();
    			if(temp1+temp2>=10)
    			{
    				this.carry=true;
    				result.push(((char)((temp1+temp2)%10)));
    			}
    			//If the carry is not present
    			else
    			{
    				if(this.carry)
    			    result.push((char)(temp1+temp2+1));
     
    				result.push((char)(temp1+temp2));
    			    this.carry=false;
    			}
    		}
    	}

    Can anyone give me some pointers on how to go about doing this?




    EDIT: NVM figured it out


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need guidance on adding characters

    You will want to use Character.getNumericValue(temp1) to convert temp1 and temp2 into integer values.
    - Jim

Similar Threads

  1. Eliminating Unicode Characters and Escape Characters from String
    By bilalonjavaforum in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 19th, 2013, 05:26 AM
  2. How to replace characters in Array with user input Characters
    By gdoggson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 4th, 2013, 05:53 AM
  3. I Require some guidance :)
    By Vryizen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 12th, 2013, 03:13 PM
  4. Hi Every One Sahaja Here!! Need Guidance
    By sahaja in forum Member Introductions
    Replies: 6
    Last Post: July 3rd, 2012, 09:52 AM
  5. Looking for guidance and instructions
    By coyboss in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2011, 10:57 AM