Hi, I need to make a program that can count and solve basic math problems (addition, subtraction) with natural integers using strings.
I'm not aloved to use parseInt so thats basicly where my world started to fall apart...
Would realy appreciate if someone could help me with this.

The code I have been given

import java.util.*;     //Scanner
import static java.lang.System.out;

public class OperationerMedNaturligaHeltalGivnaSomTeckenstranger {

    public static void main(String[] args) {

        out.println ("OPERATIONS WITH NATURAL INTEGES GIVEN " + "AS CHARACTER STRINGS\n");

        // input the natural integers
        Scanner in = new Scanner (System.in);
        out.println ("two natural integers");
        String tal1 = in.next();
        String tal2 = in.next();
        out.println();

        // add up the integers
        String tal = addera (tal1,tal2);

        // set an appropiate length on the integers and the result (this should be here)
        int len1 = tal1.length ();
        int len2 = tal2.length ();
        int len = tal.length ();
        int maxLen = Math.max(Math.max (len1, len2), len);
        tal1 = sattLen (tal1, maxLen - len1);
        tal2 = sattLen (tal2, maxLen - len2);
        tal = sattLen (tal, maxLen - len);

        // show the integers and the result
        out.println (" " + tal1);
        out.println(" " + tal2);
        for (int i = 0; i < maxLen + 2; i++){
            out.println ("-");
        }
        out.println ();
        out.println (" " + tal + "\n");

        // the numbers should also subtract, and show the diferential
        // here I just setup the call to the metod
    //  String diffTal = subtrahera (tal1, tal2);

        // here I just setup how it should be displayed
    //  int diffLen = diffTal.length();
    //  diffTal = sattLen(diffTal, maxLen - diffLen);
    }

    // addera takes two natural integers given as character strings,
    // and returns their sum as a character string
    public static String addera (String tal1, String tal2){
        String summa = "";
        for(int i = 0; i < tal1.length(); i++){
            summa += tal1.charAt(tal1.length()-i-1);

        }
        return (summa);
    }

    // subtrahera takes two natural integers given as character strings,
    // and returns their diferential as a character string.
    // The first integer is not smaller than the second integer.
/*  public static String subtrahera (String tal1, String tal2){

    }
    */
    // sattLen adds a given number of blanks
    // at the start of a given string
    public static String sattLen (String s, int antal){
        StringBuilder sb = new StringBuilder (s);
        for (int i = 0; i < antal; i++){
            sb.insert (0, " ");
        }

        return sb.toString();
    }

}

So what I basicly need is help with addera, cuse if I can get that I think I can solve the rest.

Strings are not meant to be used in mathematical equations. String represent flat text.
why wouldn't you be allowed to parse to a numerical value?

stultuske: its totally obvious that this is a task that he has been set. Yes, it makes no real sense, but as a programming exercise he has no choice but to program it as specified.

Necrozze: One way is to loop through the strings taking one character (ie digit) at a time starting at the right hand end, add those, carrying a 1 forward if necessary, and build the result string.

Yeah it's a stupid task, but like JamesCherrill said I still need to do it.

Yeah I been thinking about that and just found out how our teacher thinks we should do it, this is his pseudocode

public static String addera(String tal1, String tal2)
    {
        StringBuilder sum = new StringBuilder();
        int t1Last = tal1.length() - 1, 
                t2Last = tal2.length() - 1, 
                t1Pointer = t1Last, 
                t2Pointer = t2Last, // Pointervariabels.
                t1Del = 0,
                t2Del = 0, // Numbers to calculate with.
                carry = 0, // Carry.
                calc = 0; // Holder for calculated value.

        while(t1Pointer >= 0 && t2Pointer >= 0)
        {
            t1Del = (int) Character.getNumericValue(tal1.charAt(t1Pointer));
            t2Del = (int) Character.getNumericValue(tal2.charAt(t2Pointer));
            calc = t1Del + t2Del + carry;
            if(calc > 9)
            {
                carry = calc  / 10;
                calc = calc % 10;
            }

            t1Pointer--;
            t2Pointer--;
            sum.insert(0, calc);
        }

        while(t1Pointer >= 0)
        {
            t1Del = (int) Character.getNumericValue(tal1.charAt(t1Pointer));
            calc = t1Del + carry;
            if(calc > 9)
            {
                carry = calc  / 10;
                calc = calc % 10;
            }

            t1Pointer--;
            sum.insert(0, calc);
        }

        while(t2Pointer >= 0)
        {
            t2Del = (int) Character.getNumericValue(tal2.charAt(t2Pointer));
            calc = t2Del + carry;
            if(calc > 9)
            {
                carry = calc  / 10;
                calc = calc % 10;
            }

            t2Pointer--;
            sum.insert(0, calc);
        }

        if (carry != 0)
            sum.insert(0, carry);   

        return sum.toString();
    }

this kinda what you was thinking?

Yes, that's the kind of thing. The programming style is a bit C-like, and the three almost-identical code blocks are in need of rationalisation, but overall the logic is exactly what I expected.

So I guess for the subtraction method I will have to do the same but instead of carrying one over to the next, I take one away from the second one and add 10 to the first one?

Try sitting down with papaer and pen. Do a test case or two by hand. Then you'll know exactly how it works.

alright, ill try that

Okey, im almost one with this just have a quick question. How do I check how much there is left to get to 10? Let's say I have the number 7 and want to check how many numbers there are left to 10, wich is 3, but how do I do that in java?

okey ignore that last bit...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.