# Java Exercises: Compute and print sum of two given integers.

## Java Basic: Exercise-220 with Solution

Write a Java program to compute and print sum of two given integers (more than or equal to zero). If given integers or the sum have more than 80 digits, print "overflow".

Pictorial Presentation: Sample Solution:

Java Code:

`````` import java.*;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Input two integers:");
String s1 = new String();
String s2 = new String();
s1 = sc.nextLine();
s2 = sc.nextLine();
BigInteger b1 = new BigInteger(s1);
BigInteger b2 = new BigInteger(s2);
BigInteger result = new BigInteger("0");
String s3 = ""+result;
System.out.println("\nSum of the said two integers:");
if(s1.length()>80 || s2.length()>80 || s3.length()>80)
System.out.println("Overflow");
else
System.out.println(result);
}
}
```
```

Sample Output:

```Input two integers:
25
46

Sum of the said two integers:
71
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Java: Tips of the Day

How to remove leading zeros from alphanumeric text?

Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).

```s.replaceFirst("^0+(?!\$)", "")
```

The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!\$) negative lookahead ensures that not the entire string will be matched.

Test harness:

```String[] in = {
"01234",         // ""
"0001234a",      // "[1234a]"
"101234",        // ""
"000002829839",  // ""
"0",             // ""
"0000000",       // ""
"0000009",       // ""
"000000z",       // "[z]"
"000000.z",      // "[.z]"
};
for (String s : in) {
System.out.println("[" + s.replaceFirst("^0+(?!\$)", "") + "]");
}```

Ref: https://bit.ly/2Qdcl8a