Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class requests to
- * input a number and
- * prints each of its digits
- * with a new line or
- * prints an error if parsing fails.
- */
- import java.util.Scanner;
- public class NumbersPrintLn {
- public static void main(String[] args) {
- printDigits(requestNumberInput());
- }
- private static String requestNumberInput() {
- Scanner scan = new Scanner(System.in);
- System.out.print("Input any number: ");
- while (true) {
- try {
- int number = Integer.parseInt(scan.nextLine());
- return String.valueOf(number);
- } catch (NumberFormatException e) {
- System.out.print("Incorrect input. Try again: ");
- }
- }
- }
- private static void printDigits(String digits) {
- char numberCharArr[] = digits.toCharArray();
- for (int i = 0; i < numberCharArr.length; i++) {
- System.out.println(numberCharArr[i]);
- }
- }
- }
Add Comment
Please, Sign In to add comment