1

I'm having an issue with my code, where the loop works fine if a file exists, but if it doesn't then it only repeats once. I'd like it to work so that if a user enters a filename, and that file exists, it outputs that files content, and then asks the name of a new file. It should keep doing this until the user enters an escape word. If the file doesn't exist, it should tell them the file doesn't exist, and then prompt them to enter a new file name to see if it exists. This should loop continuously until the escape word is entered. Once the escape word is entered, it should output the end game message.

I need to accomplish this without using methods, since it's for a college beginning java class. I have only a few weeks of coding experience and I'm completely stuck on getting this to function properly. My teacher suggested to "use an if else block to make the 2 paths mutually exclusive," although I'm not quite sure what this means or how to accomplish it.

Can anyone help me fix my code to accomplish the above?

Relevant code:

              while (!filename.equals(escapeWord))
              {
                 System.out.print("\nWhere would you like to go?\n");
                 filename = keyboard.nextLine();

                 File file = new File(filename);
                 Scanner inputFile = new Scanner(file);

                 if (!file.exists())
                    {
                    System.out.println("You can't go that way!");
                    return;
                    }

                 while (inputFile.hasNext())
                 {
                    String fileRead = inputFile.nextLine();
                    System.out.println(fileRead);
                    dayCount += 1;
                 }

                 inputFile.close();
              }

              //End Game Message

              System.out.println("Congratulations, your adventure is over. " +
              "You spent " +dayCount + " days adventuring!");
user3708356
  • 39
  • 1
  • 6
  • 1
    Don't post code in external links. Post your **relevant** code in your question. – Sotirios Delimanolis Jun 04 '14 at 18:29
  • See this question's answer:http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-java-on-windows – Ozair Kafray Jun 04 '14 at 18:32
  • Are this posted code section called from another method? – Rahul Jun 04 '14 at 18:38
  • I see that and that's basically what's in my code, but I need to know how to continuously loop that until an escape word is entered. It loops the output if the file exists fine, but when the file doesn't exist it doesn't loop properly. I'm not sure where I'm going wrong. – user3708356 Jun 04 '14 at 18:40

2 Answers2

2

First of all I would move the check for the escape word into the loop.

Secondly the check for the file would have thrown you out of your loop.

          while (true)
          {
             System.out.print("\nWhere would you like to go?\n");
             filename = keyboard.nextLine();

             if(filename.equals(escapeWord))
             {
                break;
             }

             File file = new File(filename);

             if (!file.exists())
             {
                System.out.println("You can't go that way!");
                continue;
             }

             Scanner inputFile = new Scanner(file);

             while (inputFile.hasNext())
             {
                String fileRead = inputFile.nextLine();
                System.out.println(fileRead);
                dayCount += 1;
             }

             inputFile.close();
          }
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • This seems to be a step in the right direction, except if I enter a file name that doesn't exist it throws an exception now. `Exception in thread "main" java.io.FileNotFoundException: up (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:131) at java.util.Scanner.(Scanner.java:611)` – user3708356 Jun 04 '14 at 18:57
  • That's from the `Scanner` trying to access the file. I updated the post by moving it behind the exists check. – Dawnkeeper Jun 04 '14 at 19:01
  • This makes sense. Looking over the code I see why this works now and where I was going wrong. You're awesome, thanks! – user3708356 Jun 04 '14 at 19:04
0

Not much sure but I think the return statement causing the issue; returning back to the caller if file don't exist. Remove that return and just say continue; so if file don't exist then it will go for the next iteration.

            if (!file.exists())
                {
                System.out.println("You can't go that way!");
                return; <-- Here
                }

Should look like

            if (!file.exists())
                {
                System.out.println("You can't go that way!");
                continue;
                }
Rahul
  • 76,197
  • 13
  • 71
  • 125