- 5

The program accepts two integers separated by delimiter and print sum. in java

example 1:5 output: 6

1st Sep 2020, 10:41 AM
Aishwarya E
Aishwarya E - avatar
4 Respostas
+ 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.... } }
1st Sep 2020, 11:32 AM
Jayakrishna 🇮🇳
1st Sep 2020, 11:08 AM
Aishwarya E
Aishwarya E - avatar
- 2
What you have tried? Pls Post your try... Pls add only relevant tags.. Like language name, topic,... etc.
1st Sep 2020, 10:42 AM
Jayakrishna 🇮🇳