Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Sum {
- public static void main(String[] args) {
- BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- String s;
- try {
- s = bufferedreader.readLine();
- } catch (IOException e1) {
- e1.printStackTrace();
- break;
- }
- int i;
- try {
- i = Integer.parseInt(s);
- } catch (NumberFormatException e) {
- if (s.equalsIgnoreCase("quit")) {
- break;
- }
- System.out.println("Integers only!");
- continue;
- }
- String s1 = Integer.toString(i);
- int j = 0;
- for (int k = 0; k < s1.length(); k++)
- j += Integer.parseInt("" + s1.charAt(k));
- System.out.println("You typed: " + i);
- System.out.println("Total of all digits is: " + j);
- System.out.println("Type \"quit\" to quit.");
- }
- }
- }
Add Comment
Please, Sign In to add comment