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 (26)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jaime Padilla
Level 22 , Chandler, United States
14 October 2022, 18:02
New task: The program reads in two file names. Requirements: 1. The program must read file names. Don't look at those sentences as step-by-step directions. They're talking about the program as a whole. Your task is to: 1. Read the source file name 2. If the specified file (to be copied) does not exist, then the program should display "File does not exist." and read a file name one more time and only then read the destination file name.
Level 19 , Kyiv, Ukraine
17 July 2022, 12:17
How I solved this task. Read first file name. Declare fileInputStream. In try block initialize fileInputStream. In catch block print message, read first file name again and initialize fileInputStream again. Then the rest of the code. It took me 25 attempts to solve it ))
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...
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.
Level 12 , 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.
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.
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.
Level 33 , Newark, United States
27 May 2020, 05:52
Can some one help what mistake Im doing here.
Level 41
16 November 2020, 13:13
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!
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.