示例代码:
<div ng-repeat="playlist in playlists" >
<a class="view-detail" ng-href="#/playlist/{{playlist.id}}">{{playlist.playlistName}}</a>
<a ng-href="#/playlist/{{playlist.id}}">check out this playlist >></a>
<span ng-repeat="genre in genres | filter:{name:'playlist.playlistGenre'}">
<img ng-src="{{genre.image}}">
</span>
</div>
输出:
输入几个字:为什么没有工作
线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:
在test.Split.main(Split.java:12)
My String没有像我期望的那样分裂成数组。关于这是为什么的任何想法?
第12行:
import java.util.Scanner;
public class Split {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a few words: ");
String wordsWhole = scan.next();
String[] wordsSplit = new String[4];
wordsSplit = wordsWhole.split("//s+");
System.out.println("Second word: " + wordsSplit[1]);
}
}
答案 0 :(得分:1)
有几个问题:
Scanner.next()
只返回输入中的第一个字(以空格分隔),使用Scanner.nextLine()
获取整行。"\\s+"
)。String[] wordsSplit = wordsWhole.split("\\s+");
答案 1 :(得分:-4)
import java.util.Scanner;
public class Split {
public static void main(String... args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a few words: ");
String wordsWhole = "This is a new sentence."//scan.next();
String[] wordsSplit = wordsWhole.split(" ");
if(wordsSplit != null){
for(String str: wordsSplit){
System.out.println("A word: " + str);
}
}
}
}