Let's make the code do something useful!

  • 6
  • Locked
Old task: Enter 2 file names, and copy the first file to the location specified by the second file name. New task: Enter 2 file names, and copy the first file to the location specified by the second file name. If the file to be copied doesn't exist, then the program reports it and reads another source file name and only then reads the destination file name.
You can't complete this task, because you're not signed in.
Comments (24)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Karas Java Developer
12 September 2021, 22:43
I have been trying really hard to be DRY but I am not able to see how that could be with out creating another class...
CityPop56709
Level 13 , Fort Worth, United States
12 September 2021, 05:09
I solved this by using a try-catch nested into another try-catch. It really helps if you make test file in the CodeGymTasks folder and mess around with actually reading and copying it. Otherwise it's REALLY hard to tell if your code is working at all.
Krad
Level 11 , Cherry Log, United States
22 November 2020, 17:13
I'm not trying to complain, but WHAT HAPPENED? I understood task after task, Learned a lot, but all of a sudden I don't have a clue what to do on these tasks. Am I the minority? Or is it designed like this? What am I missing?
Lex Icon
Level 17 , Sofia, Bulgaria
28 December 2020, 20:56
I feel your pain brother, so, my best advice is. Regardless of the outcome, was it an easy task or it was the hardest one you'd ever seen - download the right solution from the Help section anyway, just to see another approach, it's very useful as you may see different solutions over one task.
Switch/Cypher
Level 25 , Bexleyheath, United Kingdom
21 September 2020, 09:52
Lots of people tried to clarify this task, here's what I found useful: You ONLY read the output filename AFTER you've checked whether the INPUT one is valid. 1. Enter Input filename. 2. IF it's not valid, read another Input filename. 3. if that's not valid either, program ends with an exception. 4. If that one is valid or the first is valid THEN read the output filename.
Andrei
Level 41
16 November 2020, 13:12
Noice! good mention of reading the output file AFTER i've checked whether the input is valid.
Lucas Hoage
Level 14 , Savannah, United States
6 June 2020, 15:18
It's not unsolvable in the sense that the task is too complicated. I think it has more with learning how the flow of try catch works and how initializations affect the flow below them. Good learning experience.
Surya
Level 33 , Newark, United States
27 May 2020, 05:52
Can some one help what mistake Im doing here.
Andrei
Level 41
16 November 2020, 13:13
Yes.
Adrian
Level 27 , Spanish Town, Jamaica
19 May 2020, 02:21
There are a lot of overly complicated suggestions here. here's what you need to know: if you simply wrap the code that throws the exceptions: String sourceFileName = reader.readLine(); ---------> IOException InputStream fileInputStream = getInputStream(sourceFileName);---------> FileNotFoundException the fileInputStream instance will not be visible to the code outside the try block. to fix this, set the InputStream fileInputStream to null before making the try-catch wrap. all the best!
null
Level 26 , Orlando, United States
31 March 2020, 03:17
There only 2 cases to keep in mind for this. If the inputFileName is valid in try{} block, keep going to read the next outPutFileName, else go to catch block{} and do the same thing.
Jeff
Level 22 , Spartanburg, United States
14 March 2020, 14:49
Tips: 1. Use a for loop that repeats a max of 2 time 2. Wrap a try/catch block in the for loop 3. Wrap all I/O in the try block 4. Get the file input stream immediately after reading the source file name Avoid: 1. Multiple try/catch blocks 2. Doing anything in a finally block other than clean up resource (don't even use a finally block for this problem) 3. More than 20 lines of code in the main method. I've got it down to 17 lines including closing braces
Aakankasha Sharma
Level 18 , Chandigarh, India
28 June 2020, 09:22
Why two times though?
Kent Hervey Software Engineer/Consult at Zeal IT Consultants
10 March 2020, 16:16
Wow. lowest number I have seen for any task This task has been completed by 865 students.
Agent Smith
Level 38
21 August 2020, 21:11
1205+ in August 2020.
Andrei
Level 41
16 November 2020, 13:14
1311 in November 2020.