There is a file containing a number: test.txt

1234523432 

How to count a number into an array, character-by-character, and determine if there are even numbers in a given number and how many are there?

Closed due to the fact that it was off topic by the participants default locale , 0xdb , aleksandr barakin , user192664, Dmitry Kozlov Oct 26 '18 at 15:26 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- aleksandr barakin, Dmitry Kozlov
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    You can use Stream Api, the res.txt file should be in the resources:

      public static void main(String[] args) throws URISyntaxException, IOException { List<Integer> list = Files.lines(Paths.get(Main.class.getResource("res.txt").toURI()), StandardCharsets.UTF_8) .map(s -> s.split("")) .flatMap(Arrays::stream) .map(Integer::valueOf) .collect(Collectors.toList()); list.forEach(System.out::println); int chet = (int) list.stream().filter(s -> s % 2 == 0).count(); System.out.println(chet!=0?"Число четных: " + chet:"Нет четных"); } 

      This can be done as follows:

       import java.io.*; public class script { public static void main(String[] args) throws Exception{ FileInputStream io = new FileInputStream("test.txt"); int countOfEven = 0; BufferedReader r = new BufferedReader(new InputStreamReader(io)); String line; while( (line = r.readLine()) != null ){ for(char ch : line.toCharArray()){ if(Integer.parseInt(String.valueOf(ch)) % 2 == 0){ countOfEven++; } } } if(countOfEven != 0){ System.out.println("Число имеет четные цифры"); System.out.println("Количество - " + countOfEven); }else{ System.out.println("Число не имеет четных цифр"); } } }