【问题标题】:Scanner through a line with whitespace and comma用空格和逗号扫描一行
【发布时间】:2012-07-22 12:07:44
【问题描述】:

我是 Java 新手,正在寻找有关 Java 的 Scanner 类的帮助。下面是问题。 我有一个包含多行的文本文件,每行有多对数字。这样每对数字都表示为(数字,数字)。例如 3,3 6,4 7,9。所有这些多对数字都由空格隔开。以下是文本文件中的示例。

1 2,3 3,2 4,5

2 1,3 4,2 6,13

3 1,2 4,2 5,5

我想要的是我可以分别检索每个数字。这样我就可以创建一个链表数组了。以下是我到目前为止所取得的成就。

Scanner sc = new Scanner(new File("a.txt"));
    Scanner lineSc;
    String line;
    Integer vertix = 0;
    Integer length = 0;
    sc.useDelimiter("\\n"); // For line feeds

    while (sc.hasNextLine()) {
        line = sc.nextLine();
        lineSc = new Scanner(line);

        lineSc.useDelimiter("\\s"); // For Whitespace
        // What should i do here. How should i scan through considering the whitespace and comma
        }

谢谢

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    考虑使用正则表达式,不符合您期望的数据将很容易被识别和处理。

    CharSequence inputStr = "2 1,3 4,2 6,13";    
    String patternStr = "(\\d)\\s+(\\d),";    
    // Compile and use regular expression
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    
    while (matcher.find()) {
        // Get all groups for this match
        for (int i=0; i<=matcher.groupCount(); i++) {
            String groupStr = matcher.group(i);
        }
    }
    

    第一组和第二组将分别对应每对中的第一个和第二个数字。

    【讨论】:

    • 谢谢。但是上述解决方案以 2 1 然后 3 4 然后 2 6 并忽略 13 的形式给我输出。我想要的是我应该有第一个数字 2。然后是数字对 1,3 然后 4,2 和然后是 6,13。我也想以单独的整数形式而不是作为一个组来使用它。因为我想将这些线转换为一个数组,这样 Array[2][0] = {1,3} , Array[2][1] = {4,2} 等等..
    【解决方案2】:

    1.使用Scanner的nextLine()方法从文件中获取每一整行文本

    2.然后使用BreakIterator类及其静态方法getCharacterInstance(),获取单个字符,会自动处理逗号、空格等

    3.BreakIterator还提供了很多灵活的方法来分离句子、单词等。

    更多详情请看:

    http://docs.oracle.com/javase/6/docs/api/java/text/BreakIterator.html

    【讨论】:

      【解决方案3】:

      使用 StringTokenizer 类。 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

      //this is in the while loop
      //read each line
      String line=sc.nextLine();
      
      //create StringTokenizer, parsing with space and comma
      StringTokenizer st1 = new StringTokenizer(line," ,");
      

      然后,当您像这样调用 nextToken() 时,如果您想要该行中的所有数字,每个数字都会被读取为字符串

      while(st1.hasMoreTokens())
      {
          String temp=st1.nextToken();
      
          //now if you want it as an integer
          int digit=Integer.parseInt(temp);
      
          //now you have the digit! insert it into the linkedlist or wherever you want
      }
      

      希望这会有所帮助!

      【讨论】:

        【解决方案4】:

        使用 split(regex),更简单:

         while (sc.hasNextLine()) {
              final String[] line = sc.nextLine().split(" |,");
              // What should i do here. How should i scan through considering the whitespace and comma
              for(int num : line) { 
                    // Do your job
              }        
         }
        

        【讨论】:

          猜你喜欢
          • 2017-02-22
          • 1970-01-01
          • 2021-04-04
          • 2012-09-15
          • 2011-05-09
          • 2019-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多