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.