Sorting even numbers from a file

  • 16
  • Locked
If one ordered isomorph isn't sure if another ordered isomorph is sober, he asks him to pass a simple test: read a file name from the console, read a set of numbers from the file, sort only the even numbers in ascending order, and then display them on the screen. Can you pass?
You can't complete this task, because you're not signed in.
Comments (11)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Karas Java Developer
29 April 2022, 00:35
Three times is the charm right?... I did not use InteliJ or anything like that, just out of thin air you can verify the task. Amazing advice.
Level 15 , Old Town, United States
3 June 2021, 17:45
whew! had to look up a lot of stuff and learned a lot. Found that you can solve by either using a Scanner or not. Using a Scanner is nice, b/c it can work with bytes & has the nextInt() method. Not using a Scanner, all you have to do is convert String to int and evaluate that value to remainder equals zero int value = Integer.parseInt(reader.readLine()); Hope that helps without giving an answer. Happy coding! :)
Dawid Bujnicki Junior Developer
8 May 2021, 19:43
Don't be lazy guys, I was trying to insert part of the path to target file in Solution class (e.g C:\codegym\(...)\task1326\ + read.readLine();) and even if program worked correctly, the validator wouldnt pass it. I was struggling for hours thinking what was wrong with this, as validator showed piont 3 not fulfilled as well ("The program should display data on screen."). Once I removed that first part of the path, it passed. I realised that user should be able to send any path, not only the one in the folder where Solution class is located.
yehuda b
Level 23 , Beersheba, Israel
21 May 2020, 18:30
Interesting, when I used Scanner to read the file, (after getting the filename, and creating a FileInputStream with the filename passed as an argument) even though the code was doing exactly the same thing as when I used BufferedReader in the end, it didn't validate because of too many lines. I used Scanner because the logic was simple: while Scanner hasNext, add nextInt to a list etc. which you can't do with BufferedReader.
Level 25 , Bexleyheath, United Kingdom
10 October 2020, 21:09
Buffered Reader had too many lines because it read the numbers as ASCII code, not as the numbers they are.
Justin Smith
Level 39 , Greenfield, USA, United States
4 August 2021, 23:01
Although the conditions require FileInputStream, it's worth noting that you can do this with BufferedReader by using: BufferedReader input = new BufferedReader(new FileReader(filename)). You can then use the method input.readLine() just the same as reading input from the keyboard. I had originally done it this way as I didn't notice we were required to use FileInputStream.
Level 14 , Apeldoorn, Netherlands
6 February 2020, 14:48
I have tested it myself and it works, but the tests claim that it doesn't print anything. even though it does do it in ascending order
Level 14 , Apeldoorn, Netherlands
5 February 2020, 15:09
Does anyone know what the issue with my
might be?
Level 32 , Kyiv, Ukraine
28 February 2019, 12:36
Lost bunch of time before my first try to verify task on fixing bug. But to the surprise verifier doesn't even check this scenario. The problem was that last line of file didn't read. My algorithm was next: 1. Read file by bytes; 2. Save byte to the buffer if it doesn't equal 10 (ASCII's end of line) ; 3. If byte equals 10 then convert buffer to the string (and then to the int) 4. Add converted buffer to the arraylist. 5. erase buffer So as you can see, if there is no empty line in the end then last number doesn't add to the arraylist either. How can i check end of file, if i am using P.S. Should i create this question in the help section even i already completed the task?
Level 22 , Nope, Bulgaria
3 December 2019, 08:09
You can do so, it is always helpful to have guidance on tasks!
Jonaskinny Java Developer at Sandmedia
27 February 2022, 00:27
While (