无法将字符串从一个类传递到另一个类

时间:2016-02-24 19:59:44

标签: java string class methods

您好我正在尝试编写一个程序,该程序从逗号分隔的文本文件中获取第三行,然后将其传递给WeatherDataPoint类中的getData方法,在该类中,它将字符串拆分为逗号上的单独字符串。然后我希望将字符串变量日期设置为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]也可以。

3 个答案:

答案 0 :(得分:0)

尝试替换它:

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

使用:

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

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

答案 1 :(得分:0)

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

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

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

修改

如果您需要跳过文件的前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 []并按你的意愿处理它。以下是示例代码:

private ArrayList date = new 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" 

随意扩展和修改。

答案 2 :(得分:0)

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