String.split()在另一个数组中的String上调用,未能正常工作。

huangapple go评论78阅读模式
英文:

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 Strings (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 Strings 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&lt;Combo&gt; combos=new ArrayList&lt;&gt;();
String[] dummy;
String read=readFromFile();
String[] readarray=read.split(&quot;#&quot;);
for (String ss:readarray) {
    dummy=ss.split(&quot;-&quot;);
    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.

huangapple
  • 本文由 发表于 2020年9月30日 22:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定