I either get too many lines or otherwise too few lines.
How can I check the output? I can only work in Intellij with these task and not in the CodeGym enviroment. So how can I see what is going wrong?
package com.codegym.task.task13.task1326;
import java.io.*;
import java.util.*;
/*
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.
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();//read a file from the console
InputStream input = new FileInputStream(s);//create a FileInputStream using the line read from the console.
//BufferedWriter writer = new BufferedWriter(new FileWriter(s));
/*InputStream input = new FileInputStream("c:\\data\\input-text.txt");
int data = input.read();
while(data != -1) {
//do something with data...
doSomethingWithData(data);
data = input.read();
}
input.close();*/
int data = input.read();//read the numbers from the file input
ArrayList<Integer> result = new ArrayList<>();
while (data != -1) {
data = input.read();//reads the input from file
if (data % 2 == 0) {//checks if the number is even
result.add(data);//adds the number to the arraylist result
Arrays.sort(new ArrayList[]{result});
System.out.println(result);
}
//s = reader.readLine();
//int t = Integer.parseInt(s);//convert the string into integer
//ArrayList<Integer> result = new ArrayList<>();
//result.add(t);//only adds even number to result array
//Arrays.sort(new ArrayList[]{result});//sort the numbers in ascending order
//writer.write(t);//only add the even numbers
//how to display in ascending order?
//writer.sort();
}
//for (Integer number : result)//loop over the array with integers
//System.out.println(number);//prints the results
//The program should display data on the screen.
//The program displays too many lines. change println into print => The program displays too few lines.
reader.close();
input.close();
//writer.close();
}
}