【发布时间】:2012-11-21 19:14:15
【问题描述】:
对不起,如果我的代码看起来很糟糕,我在编程方面没有那么经验丰富。我需要以以下格式从 .txt 传输文本:Date-Name-Address-etc..
我正在读取文件,然后使用 String.split("-") 拆分字符串。我遇到了循环问题。
try{
File file = new File("testwrite.txt");
Scanner scan = new Scanner(file);
String[] test = scan.nextLine().split("-");
while(r<100){
while(c<6){
data[r][c] = test[c];
test = scan.nextLine().split("-");
c++;
}
r++;
c = 0 ;
}
System.out.println(data[1][5]);
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
【问题讨论】:
-
System.out.println(data[1][5]) - 用于测试目的。
-
看起来
for-loop 更适合这个。 -
首先是第8行,test = scan.nextLine().split("-"); test是一个字符串数组,需要指定索引。
-
如果您提供更多详细信息,我很乐意提供帮助。请编辑您的问题。
标签: java arrays file multidimensional-array