英文:
String.split() called on a String contained in another array not working correctly
问题
ArrayList<Combo> combos = new ArrayList<>();
String[] dummy;
String read = readFromFile();
String[] readarray = read.split("#");
for (String ss : readarray) {
dummy = ss.split("-");
Combo combo = new Combo(dummy[0], Integer.parseInt(dummy[1]), Integer.parseInt(dummy[2]), Integer.parseInt(dummy[3]));
combos.add(combo);
}
这是文件包含的字符串,以上代码从中读取:
#22/09/2020-0900-2300-30#22/09/2020-0930-2330-30#22/09/2020-0900-2300-15
非常感谢,希望能帮到你。
英文:
I am trying to obtain an array of String
s (dummy
) which is to be converted to a custom object Combo, by splitting every String
contained in a second array (readarray
). This second array is obtained by splitting the contents of a file.
Even though the array obtained by splitting the file's contents seems to be working fine, I can't seem to successfully split the String
s it contains, I get one empty return (dummy[0]
) and the ones after are null
. Also, readFromFile()
simply uses a FileInputStream
to return the full String
the file contains.
ArrayList<Combo> combos=new ArrayList<>();
String[] dummy;
String read=readFromFile();
String[] readarray=read.split("#");
for (String ss:readarray) {
dummy=ss.split("-");
Combo combo=new Combo(dummy[0],Integer.parseInt(dummy[1]),Integer.parseInt(dummy[2]),Integer.parseInt(dummy[3]));
combos.add(combo);
}
This is the String the file contains from which the above reads:
> #22/09/2020-0900-2300-30#22/09/2020-0930-2330-30#22/09/2020-0900-2300-15
Help would be very much appreciated, thanks a lot.
答案1
得分: 1
删除字符串开头或结尾的 #,然后就可以了。因为这会在你的数组中创建第一个空对象。
英文:
remove the # from the beginning or the end of your string and you are fine.
because it will create the first empty object in your array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论