简体   繁体   English

是否可以多次使用文件扫描程序而不声明多个扫描程序?

[英]Is It Possible to Use a File Scanner Multiple Times Without Declaring Multiple Scanners?

I'm making a program for my improv theater that will help us pick out games we play in shows on a night without overlapping styles of any other games.我正在为我的即兴剧院制作一个程序,该程序将帮助我们挑选晚上在演出中玩的游戏,而不会与任何其他游戏的风格重叠。 I've come into a problem though.不过我遇到了问题。 In the code below, Scanner scWU reads a .txt file that contains names of improv games, and Scanner sc is a normal System.in scanner.在下面的代码中,Scanner scWU 读取一个包含即兴游戏名称的 .txt 文件,而 Scanner sc 是一个普通的 System.in 扫描器。

I've pasted two of my methods below.我在下面粘贴了两个方法。 getWarmUp() returns the string (game) after it's been deemed a doable game of a certain category (in this case, the warmup game category). getWarmUp()在它被认为是某个类别的可行游戏(在本例中为热身游戏类别)后返回字符串(游戏)。 isWarmUp() reads the warmupgames.txt file and sees if the game entered is indeed a warmup game isWarmUp()读取warmupgames.txt文件,查看进入的游戏是否确实是热身游戏

My question is: If the user fails to enter the name of the game (and isWarmUp returns false), how can I re-launch the method OR reset the scWU scanner at the top of the file?我的问题是:如果用户无法输入游戏名称(并且 isWarmUp 返回 false),我该如何重新启动该方法或重置文件顶部的 scWU 扫描器? Would I have to declare multiple scanners?我是否必须申报多个扫描仪? Or could I easily make the same scanner scan the file again after the user fails the first time to enter a game correctly?或者我可以在用户第一次正确进入游戏失败后轻松地让相同的扫描仪再次扫描文件吗? (NOTE: I am aware the while loop on line 25 is an infinite loop. This is where I'm hoping to address this question) (注意:我知道第 25 行的 while 循环是一个无限循环。这是我希望解决这个问题的地方)

I will answer any confusion about my code我会回答关于我的代码的任何困惑

   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;

Here's what I mean.这就是我的意思。 Presumably you're now using getWarmUp something like this:大概你现在正在使用getWarmUp这样的东西:

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

But getWarmUp (or, rather, isWarmUp , which getWarmUp calls) might need to re-read the file from the beginning.但是getWarmUp (或者更确切地说, isWarmUpgetWarmUp调用)可能需要从头开始重新读取文件。 The only way it can do this is by creating a new Scanner .它可以做到这一点的唯一方法是创建一个新的Scanner And you'd need the filename in order to create the new Scanner .并且您需要文件名才能创建新的Scanner So have getWarmUp take the filename as an argument instead of an opened 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;
   }    

Then call it like this:然后像这样调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM