Perform a learning task. Help advice, and how best to implement.

Task:

The user enters the number of bodies, the program reads until the number is correct (in my case, only the length of the number is checked, although the more checks, the better). After entering a valid number, it is necessary to sum all the numbers until one remains (Example: 0671111112, sum 21, result 3). And as a result, print the given number in a string (Example: result 3, answer "Three").

My current code is:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /** * Created by Dima on 22.10.2016. */ public class InputNumber { public static void main(String[] args) throws IOException { int sum = 0; int sum1 = 0; int res = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Введите номер телефона. В формате: ----------"); while (true) { String line = reader.readLine(); char[] array = line.toCharArray(); if (array.length < 10) System.out.println("Ошибка. Недостаточно цифр."); else if (array.length > 10) System.out.println("Ошибка. Много цифр."); else if (array.length == 10) { for (final char c : array) { if (!Character.isDigit(c)) { System.out.println("Ошибка. Вы ввели символ вместо цифры."); throw new IllegalArgumentException(); } sum = sum + Character.getNumericValue(c); } //System.out.println("Sum " + sum); while (sum != 0) { //Суммирование цифр числа sum1 = sum1 + (sum % 10); sum /= 10; } //System.out.println("Sum1 " + sum1); while (sum1 != 0) { //Суммирование цифр числа res = res + (sum1 % 10); sum1 /= 10; } break; } } // System.out.println("res " + res); switch (res){ case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); break; case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); break; case 6: System.out.println("Six"); break; case 7: System.out.println("Seven"); break; case 8: System.out.println("Eight"); break; case 9: System.out.println("Nine"); break; } } } 
  • And what is your question? - Roman
  • @Roman, The TS Question was deleted when the issue changed. - post_zeew
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

I would check the phone number with regular expressions:

 public static boolean isNumberValid(String number) { Pattern pattern = Pattern.compile("^[\\d]{10}$"); Matcher matcher = pattern.matcher(number); return matcher.matches(); } 

The isNumberValid(...) method isNumberValid(...) entered number matches the mask of the type XXXXXXXXXX (10 digits).

Enter the phone number can be implemented as follows:

 String stringNumber; Scanner reader = new Scanner(System.in); do { System.out.println("Введите десятизначный номер телефона"); stringNumber = reader.nextLine(); if (!isNumberValid(stringNumber)) { System.out.println("Введены некорректные данные: номер должен состоять из 10 цифр"); } else { break; } } while (true); 

The calculation of the sum of the digits of the entered number is as follows ( if I understood correctly, it is necessary to sum all the digits, except the first one ):

 long number = Long.parseLong(stringNumber.substring(1)); int firstSum = 0; do { firstSum += number % 10; number /= 10; } while (number > 0); 

Counting the sum of the secondSum digits of the received number firstSum as follows:

 int secondSum = 0; do { secondSum += firstSum % 10; firstSum /= 10; } while (firstSum > 0); 

And I will leave the last task to you. Think about how you can get a finite number, which must be displayed in words. Maybe something more complicated is needed here than switch - case ? ...

And also: see above two practically identical code fragments? In order to get rid of duplication, this code needs to be put into a separate method, and then just use this method.

And in general, it is not necessary to write everything like this in one method. Divide the problem to be solved into subtasks, each method will solve a separate subtask.