本来在另外一个帖子里提到了这个问题,但是貌似大家没有理解我的意思,可能是我解释的不清楚,于是我自己写了程序,这样大家就能把错误直接挑出来了,特此从新发帖
我想把存有三行字符串的txt文档的数据用split分割,然后分割好的数值依次赋值放到ss[]数组里,以下是txt文档里的数据(每行最后的数字是设备价格,价格前是设备名称):
nb 910 conf max 10
nb PTN conf max 10
PTN 910 247,8206684
System Control - 16×E1(120ohm) 1359,8
16×E1 Board 545,63
2×Board 181,67
4×Board 362,68
PTN 912 171,5
System 2×FE(electric) and 2×FE(optical) 561,72
ADSL(capa 1) 65
SHDSL capa 1) 26
PTN Installation 455,0
PTN Commissioning 630,7
PTN SITE Survey 367,9
ADSL I&C+NMS 20
SHDSL I&C+NMS 40
License PTN910 NE 209,6
License PTN912 NE 212,74
以下是我写的程序,老是报错的,因为太菜也不知道怎么改才好。
package MW;
import java.io.*;
public class Test { /**
* @param args
*/
public void Price()throws IOException{
BufferedReader buf=new BufferedReader(new FileReader("W:\\modeleCoutMW1.txt"));
int i=0;
String[] ss=new String[36];
String buffer="a";
while(buffer!=null){
buffer=buf.readLine();
String tem[]=buffer.split(" ");//这里是两个空格(因为txt文档里名称与价格之间是两个空格)
for(int j=0;j<tem.length;j++){ss[i]=tem[j];i++;};
}
for(int k=0;k<ss.length;k++)System.out.println(ss[k]);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test p=new Test();
p.Price();
}}
报错信息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 36
at MW.Test.Price(Test.java:16)
at MW.Test.main(Test.java:23)谢谢各位了。
我想把存有三行字符串的txt文档的数据用split分割,然后分割好的数值依次赋值放到ss[]数组里,以下是txt文档里的数据(每行最后的数字是设备价格,价格前是设备名称):
nb 910 conf max 10
nb PTN conf max 10
PTN 910 247,8206684
System Control - 16×E1(120ohm) 1359,8
16×E1 Board 545,63
2×Board 181,67
4×Board 362,68
PTN 912 171,5
System 2×FE(electric) and 2×FE(optical) 561,72
ADSL(capa 1) 65
SHDSL capa 1) 26
PTN Installation 455,0
PTN Commissioning 630,7
PTN SITE Survey 367,9
ADSL I&C+NMS 20
SHDSL I&C+NMS 40
License PTN910 NE 209,6
License PTN912 NE 212,74
以下是我写的程序,老是报错的,因为太菜也不知道怎么改才好。
package MW;
import java.io.*;
public class Test { /**
* @param args
*/
public void Price()throws IOException{
BufferedReader buf=new BufferedReader(new FileReader("W:\\modeleCoutMW1.txt"));
int i=0;
String[] ss=new String[36];
String buffer="a";
while(buffer!=null){
buffer=buf.readLine();
String tem[]=buffer.split(" ");//这里是两个空格(因为txt文档里名称与价格之间是两个空格)
for(int j=0;j<tem.length;j++){ss[i]=tem[j];i++;};
}
for(int k=0;k<ss.length;k++)System.out.println(ss[k]);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test p=new Test();
p.Price();
}}
报错信息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 36
at MW.Test.Price(Test.java:16)
at MW.Test.main(Test.java:23)谢谢各位了。
for(int j=0;j <tem.length;j++){ss[i]=tem[j];i++;}在for前面要把i重置为0,因为while一次之后i已不是0
试了一下,没有什么问题.可能是你的文件的格式.import java.io.*;
public class Test { /**
* @param args
*/
public void Price()throws IOException{
BufferedReader buf=new BufferedReader(new FileReader("c:\\a.txt"));
int i=0;
String[] ss=new String[36];
String buffer=buf.readLine();
while(buffer!=null){
String tem[]=buffer.split(" ");//这里是两个空格(因为txt文档里名称与价格之间是两个空格)
for(int j=0;j <tem.length;j++){ss[i]=tem[j];i++;};
buffer=buf.readLine();
}
for(int k=0;k <ss.length;k++)System.out.println(ss[k]);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test p=new Test();
p.Price();
}
}
while(buffer!=null){
buffer=buf.readLine();
想想最后一行的情况,之前buffer!=null,buffer再读一行就成了null,
之后再split,再做所以就报错。
看来标准的东西还是不要随便想当然的修改了,按照标准的做法做吧。
我将test改成了test3
import java.io.*;public class test3 { /**
* @param args
*/
public void Price() throws IOException {
BufferedReader buf = new BufferedReader(new FileReader(
"C:\\modeleCoutMW1.txt"));
int i = 0;
String[] ss = new String[36];
String buffer ;
while ((buffer=buf.readLine()) != null) {
String tem[] = buffer.split(" ");// 这里是两个空格(因为txt文档里名称与价格之间是两个空格)
for (int j = 0; j < tem.length; j++) {
ss[i] = tem[j];
i++;
}
}
for (int k = 0; k < ss.length; k++)
System.out.println(ss[k]);
} public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
test3 p = new test3();
p.Price();
}}