Guest User

Untitled

a guest
Oct 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /**
  2. * This class requests to
  3. * input a number and
  4. * prints each of its digits
  5. * with a new line or
  6. * prints an error if parsing fails.
  7. */
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class NumbersPrintLn {
  12.  
  13. public static void main(String[] args) {
  14. printDigits(requestNumberInput());
  15. }
  16.  
  17. private static String requestNumberInput() {
  18. Scanner scan = new Scanner(System.in);
  19. System.out.print("Input any number: ");
  20.  
  21. while (true) {
  22. try {
  23. int number = Integer.parseInt(scan.nextLine());
  24. return String.valueOf(number);
  25. } catch (NumberFormatException e) {
  26. System.out.print("Incorrect input. Try again: ");
  27. }
  28. }
  29. }
  30.  
  31. private static void printDigits(String digits) {
  32. char numberCharArr[] = digits.toCharArray();
  33. for (int i = 0; i < numberCharArr.length; i++) {
  34. System.out.println(numberCharArr[i]);
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment