【问题标题】:return array from inside an if,for statement从 if,for 语句中返回数组
【发布时间】:2015-12-14 12:34:32
【问题描述】:

我正在构建一个用于库存目的的标签阅读器。使用 for 循环遍历标签以计算/总计 id。我的返回行出现错误“tagsFound 无法解析为变量”。如何在 for 循环内使用变量,然后在循环外访问它?

public String[] getTags(AlienClass1Reader reader)throws AlienReaderException{
    int coneCount = 0;
    int drumCount = 0;

    // Open a connection to the reader
      reader.open();
    // Ask the reader to read tags and print them
      Tag tagList[] = reader.getTagList();
      if (tagList == null) {
        System.out.println("No Tags Found");
      } else {
        System.out.println("Tag(s) found: " + tagList.length);
        for (int i=0; i<tagList.length; i++) {
          Tag tag = tagList[i];
          System.out.println("ID:" + tag.getTagID() +
                             ", Discovered:" + tag.getDiscoverTime() +
                             ", Last Seen:" + tag.getRenewTime() +
                             ", Antenna:" + tag.getAntenna() +
                             ", Reads:" + tag.getRenewCount()
                             ); 
        //tagFound[i]= "" + tag.getTagID();
          String phrase = tag.getTagID();
          tagFound[i] = phrase;
          String delims = "[ ]+";
          String[] tokens = phrase.split(delims);
          if (tokens[0].equals("0CCE") && tokens[3].equals("1001")){drumCount++;}
          if (tokens[0].equals("0CCE") && tokens[3].equals("1004")){coneCount++;}
          String[] tagsFound;
        tagsFound[i] = tag.getTagID();
      }
        System.out.println("Cones= " + coneCount);
        System.out.println("Drums= " + drumCount);

      // Close the connection
      reader.close();
      return tagsFound;
    }
}

【问题讨论】:

    标签: java for-loop error-handling scope


    【解决方案1】:
    public String[] getTags(AlienClass1Reader reader)throws AlienReaderException{
    int coneCount = 0;
    int drumCount = 0;
    
    // Open a connection to the reader
      reader.open();
    // Ask the reader to read tags and print them
      Tag tagList[] = reader.getTagList();
      if (tagList == null) {
        System.out.println("No Tags Found");
      } else {
        System.out.println("Tag(s) found: " + tagList.length);
        String[] tagsFound = new String[tagList.length];
        for (int i=0; i<tagList.length; i++) {
          tagsFound = "";
          Tag tag = tagList[i];
          System.out.println("ID:" + tag.getTagID() +
                             ", Discovered:" + tag.getDiscoverTime() +
                             ", Last Seen:" + tag.getRenewTime() +
                             ", Antenna:" + tag.getAntenna() +
                             ", Reads:" + tag.getRenewCount()
                             ); 
        //tagFound[i]= "" + tag.getTagID();
          String phrase = tag.getTagID();
          tagFound[i] = phrase;
          String delims = "[ ]+";
          String[] tokens = phrase.split(delims);
          if (tokens[0].equals("0CCE") && tokens[3].equals("1001")){drumCount++;}
          if (tokens[0].equals("0CCE") && tokens[3].equals("1004")){coneCount++;}
        tagsFound[i] = tag.getTagID();
      }
        System.out.println("Cones= " + coneCount);
        System.out.println("Drums= " + drumCount);
    
      // Close the connection
      reader.close();
      return tagsFound;
    }
    }
    

    返回的数组中标签不满足条件的位置会有空字符串。

    【讨论】:

    • 感谢您的快速回复,我现在收到错误“类型不匹配:无法从字符串转换为字符串 []”的行 tagsFound = "";
    • 数组类型必须为getTagID方法返回的标签类型
    猜你喜欢
    • 1970-01-01
    • 2013-07-05
    • 2014-05-28
    • 2017-07-27
    • 2015-01-15
    • 1970-01-01
    • 2015-07-28
    • 2016-09-14
    相关资源
    最近更新 更多