I have tried to create the solution in this way but I can not find where is the mistake. Because I think it remove the odd numbers and I create a int[] to use Arrays.sort().
But it still not working,
Thanks all
package com.codegym.task.task13.task1326;
/*
Sorting even numbers from a file
*/
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
File file = new File(name);
InputStream fl = new FileInputStream(file);
List<Integer> list = new ArrayList<>();
int number = fl.read();
while(number != -1){
list.add(number);
number = fl.read();
}
//remove odd number
for(int i = 0; i < list.size(); i++){
int even = list.get(i);
if(even % 2 != 0){
list.remove(i);
}
}
//convert List to int[]
int[] sortArray = new int[list.size()];
Iterator<Integer> iterator = list.iterator();
for(int i =0; i< sortArray.length;i++){
sortArray[i]= iterator.next().intValue();
}
//sort array
Arrays.sort(sortArray);
//print
for(int i : sortArray){
System.out.println(i);
}
fl.close();
}
}