- 5
The program accepts two integers separated by delimiter and print sum. in java
example 1:5 output: 6
4 Answers
+ 2
//Aishwarya E read comments for modifications
import java.util.Scanner;
//you forgot to import Scanner class
public class Program
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String A = sc.next();
//nextInt() read only a single integer, you need to read a string so next() method works for that. split function works on strings only .
String str[]= A.split(":");
//now your array contains both numbers but in string farmat.
//Integer.parseInt method converts to integer from string farmat
int B = Integer.parseInt(str[0]) +Integer.parseInt(str[1]);
System. out. println(B);
//if you have more numbers, more than to use a loop to convert first to Integer and add using loop....
}
}
- 2
What you have tried? Pls Post your try...
Pls add only relevant tags.. Like language name, topic,... etc.