【问题标题】:Can't pass string from one class to another不能将字符串从一个类传递到另一个类
【发布时间】:2016-02-24 19:59:44
【问题描述】:

您好,我正在尝试编写一个程序,该程序从逗号分隔的文本文件中获取第三行,然后将其传递给 WeatherDataPoint 类中的 getData 方法,在该方法中,它将字符串拆分为逗号上的单独字符串。然后我希望将字符串变量 date 设置为 dataLine2[7] 的值。然后通过调用驱动程序类的 getDate 方法打印出日期字符串。

public class driver 

{

public static void main(String[] args) 
{
    WeatherDataPoint weather = new WeatherDataPoint();
    String dataLine1 = "";
    String inputFile = "weatherdata.csv";
    Scanner readFile = null;
    try
    {
        readFile = new Scanner (new File(inputFile));
    }
    catch (FileNotFoundException ex)
    {
        System.out.println("Error file not found");
        System.exit(1);
    }

    while (readFile.hasNextLine())
    {

        dataLine1 = readFile.nextLine();

        weather.getData(dataLine1);



    }

    System.out.println(String.format("%s",weather.getDate()));
}

}

public class WeatherDataPoint {
private String date = "";
private String temperature;


public void getData(String dataLine1)
{
    String [] dataLine2 = dataLine1.split(",");
    date = dataLine2[7];
    //long epoc = Long.parseLong(fields[DATE_POS]);
    //Date d = new Date(epoc * 1000);

}


public String getDate()
{
    //return String.format("%s", (date));
    return date;
}

当我运行程序时出现错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7

编辑这里是文本文件中的一行

"1452386100","1","5","29.002591800284698","77.91272888363851","78.21244075516647","86.27","49.8",,"99","0",,"0",,"0","0","49.53002056234984","49.8","49.8",,,,,,,,,,,,,,,,,,,,,"100",,,,,,,,,,,,

我在描述程序时犯了一个错误,我实际上想处理文件中的所有数据,但跳过前三行,抱歉造成混淆。将 dataLine[7] 更改为 dataLine[0] 也可以。

【问题讨论】:

  • 错误信息不是不言自明吗?您在该行上的记录少于 8 条,因此是例外。使用您的调试器。或者将值打印到控制台。
  • 如您所说,您问题中的代码从文件中获取所有行,而不是第三行。如果此代码有效,它只会打印文件最后一行的数据。如果你的文件只有三行,我想你会没事的,但我怀疑这就是你想要的......

标签: java string class methods


【解决方案1】:

尝试替换这个:

 String [] dataLine2 = dataLine1.split(",");
date = dataLine2[7];

与:

  String [] dataLine2 = dataLine1.split(",");
  date = dataLine2[dataLine2.length-1];

因为在数组 dataLine2 中,您在第 7 个索引中没有元素。

【讨论】:

    【解决方案2】:

    确保您在 csv 文件的第 8 列有日期数据。

    【讨论】:

      【解决方案3】:

      您正在尝试从数组dataLine2 的第 8 个单元格中获取值。异常指出此索引超出范围,这意味着数组 dataLine2 没有 8 个单元格。

      这意味着您的字符串 dataLine1 在文件的单行中似乎没有 8 个部分。

      尝试打印dataLine2 变量的大小,这样您就可以看到一行有多少个字符串部分。然后通过遍历数组中的单元格来获取每个单元格。

      编辑:

      如果您需要跳过文件的前 3 行,您需要执行以下操作:

      WeatherDataPoint weather = new WeatherDataPoint();
      String dataLine1 = "";
      String inputFile = "weatherdata.csv";
      Scanner readFile = null;
      try {
          readFile = new Scanner (new File(inputFile));
      }
      catch (FileNotFoundException ex) {
          System.out.println("Error file not found");
          System.exit(1);
      }
      
          int linesPassed = 0;
          while (readFile.hasNextLine()) {
              String row = readFile.nextLine();
              linesPassed++;
              if (linesPassed > 3) {
                  dataLine1 = row;
                  weather.getData(dataLine1);
              }
          }
      System.out.println(String.format("%s",weather.getDate()));
      

      如果您需要处理整行,最好保存整个 String[] 并根据需要进行处理。这是一个示例代码:

      私有 ArrayList 日期 = 新 ArrayList(); 私有字符串温度;

      public void getData(String dataLine1){
          String[] rowParts =dataLine1.split(",");
          for(String part : rowParts)
              date.add(part); 
      }
      
      public String getDate() {
          //do some processing here, I`ve added a dummy print code
          String allValues = "";
          for(String val : date)
              allValues += val + " ";
          return allValues;
      }
      

      对于文件中的示例数据,如下所示:

      "1452386100","1","5","29.002591800284698","77.91272888363851","78.21244075516647","86.27","49.8",,"99","0",,"0",,"0","0","49.53002056234984","49.8","49.8",,,,,,,,,,,,,,,,,,,,,"100",,,,,,,,,,,,
      "1452386100","1","5","29.002591800284698","77.91272888363851","78.21244075516647","86.27","49.8",,"99","0",,"0",,"0","0","49.53002056234984","49.8","49.8",,,,,,,,,,,,,,,,,,,,,"100",,,,,,,,,,,,
      "1452386100","1","5","29.002591800284698","77.91272888363851","78.21244075516647","86.27","49.8",,"99","0",,"0",,"0","0","49.53002056234984","49.8","49.8",,,,,,,,,,,,,,,,,,,,,"100",,,,,,,,,,,,
      "0","1","2","3","4","5","6","7",
      "0","1","2","3","4","5","6","7",
      

      输出是这样的:

      "0" "1" "2" "3" "4" "5" "6" "7" "0" "1" "2" "3" "4" "5" "6" "7" 
      

      根据需要随意扩展和修改。

      【讨论】:

      • 技术上,第 8 个单元格。数组是零索引的
      • 谢谢,在我进一步讨论字符串的 8 个部分时键入错误。已编辑。问候。
      • 感谢您的回复我添加了数据文件中的一行的示例。
      • 这很奇怪,我测试了你的代码,输出应该是“49.8”。检查此文件是否在您的项目文件夹中。您可能有另一个名称相同但数据不同的文件。为了按照您指定的路径读取文件,您需要将其放在项目文件夹中。
      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多