Java Can';t将字符串从一个类传递到另一个类
您好,我正在尝试编写一个程序,从逗号分隔的文本文件中获取第三行,然后将其传递到WeatherDataPoint类中的getData方法,在该方法中,它将字符串拆分为逗号上的单独字符串。然后我希望字符串变量date设置为dataLine2[7]的值。然后通过从驱动程序类调用getDate方法打印出日期字符串Java Can';t将字符串从一个类传递到另一个类,java,string,class,methods,Java,String,Class,Methods,您好,我正在尝试编写一个程序,从逗号分隔的文本文件中获取第三行,然后将其传递到WeatherDataPoint类中的getData方法,在该方法中,它将字符串拆分为逗号上的单独字符串。然后我希望字符串变量date设置为dataLine2[7]的值。然后通过从驱动程序类调用getDate方法打印出日期字符串 public class driver { } 当我运行程序时,我得到了错误 Exception in thread "main" java.lang.ArrayIndexOutOfBou
public class driver
{
}
当我运行程序时,我得到了错误
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",,,,,,,,,,,,
我在描述程序时犯了一个错误,我实际上想处理文件中的所有数据,但跳过了前三行,很抱歉混淆了。将数据线[7]更改为数据线[0]同样有效。尝试替换此选项:
String [] dataLine2 = dataLine1.split(",");
date = dataLine2[7];
与:
因为在数组
dataLine2
中,第7个索引中没有元素。您试图从数组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()));
"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"
如果需要处理整行,最好保存整个字符串[],并根据需要进行处理。下面是一个示例代码:
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;
}
对于文件中的示例数据,如下所示:
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()));
"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"
输出如下:
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()));
"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"
您可以根据需要随意展开和修改。确保在csv文件的第8列有日期数据。错误消息不是不言自明的吗?该行上的记录少于8条,因此是例外。使用调试器。或者将值打印到控制台。您问题中的代码获取文件中的所有行,而不是如您所述的第三行。如果这段代码有效,它将只打印文件最后一行的数据。如果你的档案只有三行,我想你会没事的,但我怀疑这是你想要的…从技术上说,是第八单元。数组是零IndexedHanks,在我进一步讨论字符串的8个部分时输入error。编辑。谢谢你的回复,我添加了一个数据文件中的一行是什么样子的示例。这很奇怪,我测试了你的代码,输出结果应该是“49.8”。检查此文件是否在项目文件夹中。您可能有另一个同名但数据不同的文件。为了在指向文件路径时读取该文件,需要将其放置在项目文件夹中。