Guest User

Untitled

a guest
Nov 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Sum {
  4.  
  5. public static void main(String[] args) {
  6. BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
  7.  
  8. while (true) {
  9. String s;
  10.  
  11. try {
  12. s = bufferedreader.readLine();
  13. } catch (IOException e1) {
  14. e1.printStackTrace();
  15. break;
  16. }
  17.  
  18. int i;
  19.  
  20. try {
  21. i = Integer.parseInt(s);
  22. } catch (NumberFormatException e) {
  23. if (s.equalsIgnoreCase("quit")) {
  24. break;
  25. }
  26.  
  27. System.out.println("Integers only!");
  28. continue;
  29. }
  30.  
  31. String s1 = Integer.toString(i);
  32. int j = 0;
  33. for (int k = 0; k < s1.length(); k++)
  34. j += Integer.parseInt("" + s1.charAt(k));
  35.  
  36. System.out.println("You typed: " + i);
  37. System.out.println("Total of all digits is: " + j);
  38. System.out.println("Type \"quit\" to quit.");
  39. }
  40. }
  41.  
  42. }
Add Comment
Please, Sign In to add comment