【问题标题】:Is It Possible to Use a File Scanner Multiple Times Without Declaring Multiple Scanners?是否可以在不声明多个扫描仪的情况下多次使用文件扫描仪?
【发布时间】:2019-04-06 00:38:32
【问题描述】:

我正在为我的即兴剧院制作一个程序,该程序将帮助我们挑选出我们在夜间演出中玩的游戏,而不会与任何其他游戏的风格重叠。我遇到了一个问题。在下面的代码中,Scanner scWU 读取一个包含即兴游戏名称的 .txt 文件,而 Scanner sc 是一个普通的 System.in 扫描器。

我在下面粘贴了我的两个方法。 getWarmUp() 返回字符串(游戏),它被认为是某个类别(在本例中为热身游戏类别)的可行游戏。 isWarmUp() 读取warmupgames.txt 文件,查看进入的游戏是否确实是热身游戏

我的问题是:如果用户输入游戏名称失败(并且 isWarmUp 返回 false),我该如何重新启动该方法或重置文件顶部的 scWU 扫描仪?我必须声明多个扫描仪吗?或者在用户第一次正确进入游戏失败后,我可以轻松地让相同的扫描仪再次扫描文件吗? (注意:我知道第 25 行的 while 循环是一个无限循环。这是我希望解决这个问题的地方)

我会回答有关我的代码的任何困惑

   public static String getWarmUp(Scanner sc, Scanner scWU)
   {
      String prompt = "What warm-up game will you choose? \n" +
                      "(NOTE: Type game as it's written on the board. Caps and symbols don't matter.)\n" +
                      "> ";

      System.out.print(prompt);
      String game = sc.nextLine();

      //This while loop is infinite. This is where I'm hoping to somehow allow the scanner to reset and
      //read again on a failed input
      while(!warmUp)
      {
         warmUp = isWarmUp(scWU, game);
         if(!warmUp)
            System.out.println("That's not a warm-up game, try again.");
      }

      return game;
   }

   public static boolean isWarmUp(Scanner scWU1, String game)
   {
      int lineNum = 0;

         while(scWU1.hasNextLine())
         {
            String line = scWU1.nextLine();
            lineNum++;
            if(line.equalsIgnoreCase(game))
               return true;
         }

      return false;

【问题讨论】:

  • 如果您需要能够再次开始读取文件,则不能将已在该文件上打开的 Scanner 传递给您的 getWarmUp 方法:您需要将文件名传递为一个String 以便getWarmUp 可以为自己打开(必要时关闭并重新打开)该文件。
  • @KevinAnderson 我可以在 getWarmUp 中使用关闭和重新打开命令吗?或者我需要将文件名作为字符串传递。如果是后者,你能告诉我一个如何通过字符串打开文件的小例子吗?

标签: java file java.util.scanner


【解决方案1】:

这就是我的意思。大概你现在正在使用getWarmUp 这样的东西:

String gamesFileName = "theGamesFile.txt");
Scanner in = new Scanner(System.in);
Scanner games = new Scanner(gamesFileName);
getWarmUp(in, games);

但是getWarmUp(或者,更确切地说,isWarmUpgetWarmUp 调用)可能需要从头重新读取文件。它可以做到这一点的唯一方法是创建一个新的Scanner。您需要文件名才能创建新的Scanner。所以让getWarmUp 将文件名作为参数而不是打开的Scanner

public static String getWarmUp(Scanner sc, String gamesFn)
   {
      boolean warmUp = false;    
      while(!warmUp)
      {
          String prompt = "What warm-up game will you choose? \n" +
                          "(NOTE: Type game as it's written on the board. Caps and symbols don't matter.)\n" +
                      "> ";
          System.out.print(prompt);

          String game = sc.nextLine();

          Scanner scWU = new Scanner(gamesFn);
          warmUp = isWarmUp(scWU, game);
          scWU.close();
          if(!warmUp)
              System.out.println("That's not a warm-up game, try again.");
      }    
      return game;
   }    

然后这样称呼它:

String gamesFileName = "theGamesFile.txt");
Scanner in = new Scanner(System.in);
getWarmUp(in, gamesFileName);

【讨论】:

  • 谢谢!超级有帮助。我还应该提到我打算为不同的游戏类别使用多个 .txt 文件。那么我会为每个 .txt 文件打开一个扫描仪吗?并取消关联名称(如您的示例)?例如:Scanner scWu = new Scanner (warmupGamesTxtFile); Scanner scR = new Scanner(replayGamesTxtFile);(假设 Scanner 参数是带有 txt 文件名的字符串)
  • 如果可以的话,我会说尝试一次将其保留为一个Scanner。您是否需要同时读取热身游戏文件和重播游戏文件?我们不要忘记您已经想到或将来可能(记住,“可能”通常变成“肯定会”)发明的任何其他类别的游戏文件?如果你不小心的话,你可能会在Scanners 中引起你的注意 (;->)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2013-11-15
  • 1970-01-01
  • 2017-04-27
  • 2014-07-26
  • 1970-01-01
相关资源
最近更新 更多