Skip to content

Instantly share code, notes, and snippets.

@kendhia
Created February 13, 2016 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kendhia/f7503236ece3333d2e89 to your computer and use it in GitHub Desktop.
Save kendhia/f7503236ece3333d2e89 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Scanner;
public class Prob2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int result= 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the two numbers: ");
String num = scanner.next();
String[] nums = num.split(" ");
for (String n : nums) {
try {
result += Integer.parseInt(n);
} catch (NumberFormatException e){
System.out.println("Please enter a valid numbs");
result = 0;
}
}
if (result > 0) System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment