Rounding numbers

  • 20
  • Locked
Read 2 file names from the console. The first file contains real (fractional) numbers, separated by spaces. For example, 3.1415. Round the numbers to integers and write them, separated by spaces, to the second file. Close the streams. The rounding should work like this: 3.49 => 3 3.50 => 4 3.51 => 4
You can't complete this task, because you're not signed in.
Comments (10)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Mike S
Level 28 , Saint Louis, United States
9 February, 23:28
I had to have spent about six to seven hours on this task. The last three were just out of stubbornness and it feels really good to have gotten it. I recommend using their test values as it makes the last digit relevant (i.e. -3.51 rounds to -4 versus -3.5 rounds to -3). Took several hours of tweaking to realize I wasn't processing the VERY LAST DIGIT which, for my random test values, didn't make a difference, but for their test values, it did.
matemate123
Level 37 , Kraków, Poland
24 January, 11:34
A few hours later... but finally. It was a little play with this one, but step after step is doable. 5 star from me too. Difficult level is accurate. Big lesson.
Justin Smith
Level 39 , Greenfield, USA, United States
4 October 2021, 14:41
Not sure why this one was rated Hard. It isn't really asking us to do anything new that we haven't done before. I initially tried to make the code a little more concise by using Files.readAllBytes(Paths.get(string file_name)) but it requires you to use an InputStream. Good stuff. Only 738 completed this one? Fewer and fewer people are seeing this course through to the end the further I get into it. I am determined, I will finish everything.
Liliane Top Backend Developer at Procura
3 January 2021, 17:17
Is CodeGym not using JDK11? I wanted to use the Files.readString() but I got the message that it can't verify it. Is it something to do with my settings?
Roman
Level 41
4 January 2021, 09:14
CodeGym is using JDK8
Isma
Level 41 , Madrid, Spain
28 December 2020, 19:08
I spent a lot of time figuring out why my program wasn't working. Remember closing the streams:
Switch/Cypher
Level 25 , Bexleyheath, United Kingdom
6 October 2020, 11:57
Something that helped me: It has to work if the last character of the read file IS NOT a SPACE.
Attila
Level 25
21 April 2020, 02:36
for me:
split(" ");
and
Math.round();
helped.
Robert Constantinescu
Level 25 , Bucharest, Romania
2 December 2019, 21:27
Took me a while, tried with DataOutputStream but without succes and after seeing Alek commen with Buffered object, solved succesfully. A buffered Object is the key or at least it was for me. Good luck!
Alek
Level 23 , Warsaw, Poland
16 September 2019, 16:30
Spent a lot of time here, but I really liked this one. So meny things learned here, like writing to files complete strings with BufferedWriter instead of playing with ASCII byte by byte. :)