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?
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?
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
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("Число не имеет четных цифр"); } } } Source: https://ru.stackoverflow.com/questions/894552/
All Articles