我有一个看起来像这样的文本文件:
Aaaaa 0.55 2
bbb 2.1 0.25
ccccc 71 21
..... ... ...
ZzZz 0.725 42.92
Total: 746.54
我正在尝试获取每一列并将其放在每列的String []中。
readprod[]
将是第一列,readcant[]
是第二列,readpret[]
是第三列。
String[] readprod;
String[] readcant;
String[] readpret;
int nrz;
int lineCount;
String[] columns;
nrz = 0;
File root = android.os.Environment
.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/.InterSRL");
dir.mkdirs();
final File file = new File(dir, str);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
lineCount = 0;
while ((line = br.readLine()) != null) {
lineCount++;
}
} catch (IOException e) {
// You'll need to add proper error handling here
}
readprod = new String[lineCount];
readpret = new String[lineCount];
readcant = new String[lineCount];
// Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
columns = line.split("\\s+");
readprod[nrz] = columns[0];
readcant[nrz] = columns[1];
readpret[nrz] = columns[2];
nrz++;
}
问题是我在java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
获得了readcant[nrz] = columns[1];
。
有什么问题?有没有更好的方法从文本文件中读取列并将它们放在每列的不同字符串或arraylists中?
另外:当它们之间有3个以上的空格时,有没有办法拆分它们?我刚刚意识到第一列,名称的列可以有一个值,例如Fish Sticks 200g
..该死的......
columns = line.split("\\s+");
readprod[nrz] = columns[0];
Log.d("Listener", "Columns[0]= "+columns[0]);
readcant[nrz] = columns[1];
Log.d("Listener", "Columns[1]= "+columns[1]);
readpret[nrz] = columns[2];
Log.d("Listener", "Columns[2]= "+columns[2]);
nrz++;
这就是我得到的:
07-13 01:55:43.570: D/Listener(15805): Columns[0]= Bulion
07-13 01:55:43.570: D/Listener(15805): Columns[1]= 1
07-13 01:55:43.570: D/Listener(15805): Columns[2]= 2.25
07-13 01:55:43.570: D/Listener(15805): Columns[0]=
07-13 01:55:43.570: D/AndroidRuntime(15805): Shutting down VM
07-13 01:55:43.570: W/dalvikvm(15805): threadid=1: thread exiting with uncaught exception (group=0x40d42930)
07-13 01:55:43.575: E/AndroidRuntime(15805): FATAL EXCEPTION: main
07-13 01:55:43.575: E/AndroidRuntime(15805): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
07-13 01:55:43.575: E/AndroidRuntime(15805): at com.example.testlayout.Administrare$1.onItemClick(Administrare.java:161)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.widget.AbsListView$1.run(AbsListView.java:3423)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.os.Handler.handleCallback(Handler.java:725)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.os.Looper.loop(Looper.java:137)
07-13 01:55:43.575: E/AndroidRuntime(15805): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-13 01:55:43.575: E/AndroidRuntime(15805): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 01:55:43.575: E/AndroidRuntime(15805): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 01:55:43.575: E/AndroidRuntime(15805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-13 01:55:43.575: E/AndroidRuntime(15805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-13 01:55:43.575: E/AndroidRuntime(15805): at dalvik.system.NativeStart.main(Native Method)
所以问题是他到了第二排......
答案 0 :(得分:2)
当它们之间有超过3个空格时,有没有办法拆分它们?
是使用此正则表达式"\\s{3,}"
这意味着3个或更多空格
See more on Regular Expressions here
也是你的最后一行
总计:746.54
除非你处理问题否则会导致问题
读取该行后,您在文件中得到了什么?
你可以把一个if in来检查列的长度是3
if(columns.length == 3)
// Assign your values
else
// Log error in reading