【问题标题】:Java Scanner Class HelpJava 扫描程序类帮助
【发布时间】:2010-10-16 05:17:35
【问题描述】:

我想了解如何使用扫描仪从输入文件中打印特定字符串。对于要打印的字符串,该行必须以 *star 开头,字符串必须用引号括起来,并且是下一个标记,并且与 *star 在同一行,当然忽略空格。

示例输入文本文件:“test.txt”

这是一个测试

*星号“变量X”

更多测试

*star "变量Y

更多

测试

*star next"variableZ"

根据这个示例输入文本,输出应该是唯一的。

“变量X”

这是我的代码的一部分:

    Scanner scanner = new Scanner (new File ("test.txt"));

    while (scanner.hasNextLine()){

        if(scanner.hasNext("*star")) {
            scanner.next();

            if(scanner.hasNext()){
                String test = scanner.next();
                System.out.println(test);
            }

但它缺少一些关键的东西。非常感谢您的帮助!

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:
    package so3947761;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
      private static final Pattern RELEVANT_LINE_PATTERN =
          Pattern.compile("^\\*star\\s+\"(.*)\"$");
    
      public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("test.txt"));
    
        while (scanner.hasNextLine()) {
          String line = scanner.nextLine();
    
          Matcher m = RELEVANT_LINE_PATTERN.matcher(line);
          if (m.matches()) {
            String data = m.group(1);
    
            System.out.println(data);
          }
        }
      }
    }
    

    一些备注:

    • 我仅使用扫描仪将输入拆分为行。
    • 真正的工作是由正则表达式模式完成的,其中包含您所说的所有要求。
    • 如果您想进一步解析引号中的文本(例如用换行符替换\n),正则表达式无法做到这一点,因此您必须明确定义要替换的序列。
    • 如果您希望在这些位置允许空格,您可能需要在^ 的右侧(行首)和$ 的左侧(行尾)添加\\s*

    【讨论】:

    • 那个正则表达式似乎很好,但对于问题中给出的输入对我不起作用
    • 这很奇怪,因为我完全按照您提供的输入对其进行了测试。好吧,几乎,我删除了领先的空白。所以你可能应该遵循上面第四点中的建议。
    【解决方案2】:
        while (scanner.hasNextLine()){
    
            String line = scanner.nextLine();
    
            if(line.startsWith("*star")){
    
            String[] split= line.split("\\s+"); //Split the string where there is whitespace
    
            //No whitespace after *star 
            if ((split.length<2)){
            System.exit(0); 
            }
    
            String file= split[1];
            String star=split[0];
    
            String file1=file.trim(); //Removes whitespace 
    
            if(file1.startsWith("\"")&&(file1.endsWith("\""))){
    
            System.out.println(file1);
            }
    
       }
    

    【讨论】:

      【解决方案3】:

      这就是诀窍

      public static void main(String[] args) throws FileNotFoundException
          {
              Scanner scanner = new Scanner (new File ("c:\\test.txt"));
      
              while (scanner.hasNextLine()){
                  if(scanner.hasNext(Pattern.compile("\\*star"))) {
                      scanner.next();
                      System.out.println(scanner.nextLine());
                      return;
                  }
                  else
                      scanner.nextLine();
              }
          }
      

      编辑: 答案中缺少引用部分,不记得放它,因为即使没有它,我也得到了输出,但这是一个更强大的解决方案:

      Scanner scanner = new Scanner (new File ("c:\\new.txt"));
      
              while (scanner.hasNextLine()){
                  if(scanner.hasNext(Pattern.compile("\\*star"))) {
                      scanner.next();
                      String s = scanner.nextLine();
                      Matcher m = Pattern.compile("\".+\"").matcher(s);
                      if(m.find())
                      {
                          System.out.println(m.group());
                          return;
                      }
                  }
                  else
                      scanner.nextLine();
              }
      

      【讨论】:

      • 这个没有去掉前导 *star 和 "" 引号,如示例输出中...
      • 您真的尝试在输入上运行它吗?即使没有我刚刚添加的报价代码,它也确实为我得到了正确的答案;但我同意它应该在那里。前面的代码肯定去掉了前导 *star(当一行以它开头时,如前所述),并且不需要去掉引号,如输出所示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2013-10-28
      • 2017-04-01
      • 2010-12-31
      相关资源
      最近更新 更多