Hey guys,
so i looked a other peoples questions here and tried several things...
I´m just going to coping the solution now but maybe someone can point out what´s wrong?
It´s not the sorting nor the adding, i checked that with test input.
I also tried adding all numbers to Arraylist and only display the even ones later.
I pet it is something lame like "no line break after the the last number" or smth....
Thank you for your time.
package com.codegym.task.task13.task1326;
/*
Sorting even numbers from a file
1. Read a file name from the console.
2. Read a set of numbers from the file.
3. Display only the even numbers, sorted in ascending order.
Example input:
5
8
-2
11
3
-5
2
10
Example output:
-2
2
8
10
Requirements:
1. The program must read data from the console.
2. The program must create a FileInputStream using the line read from the console.
3. The program should display data on the screen.
4. The program should display all of the even numbers read from the file, sorted in ascending order.
5. The program must close the input stream used to read the file (FileInputStream).
*/
import javax.imageio.IIOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
public static void main(String[] args) throws IOException{
//write your code here
FileInputStream input = readInput();
BufferedReader bf = new BufferedReader(new InputStreamReader(input));
ArrayList<Integer> list = new ArrayList<>();
while(bf.ready())
{
int a = Integer.parseInt(bf.readLine());
if((a % 2) == 0) list.add(a);
}
Collections.sort(list);
for(int i : list)
{
System.out.print(i);
}
input.close();
}
public static FileInputStream readInput() throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String name = bf.readLine();
FileInputStream input = new FileInputStream(name);
return input;
}
}