找到第一个 space 字符的索引?
Find the index of the first space character?
我已经编写了将数据值读取和写入数组列表的功能代码。尽管它 return 包含所有内容,但如果一个值有多个单词,我如何排除第一个单词?
// 3 points
static ArrayList<String> Q2(String filename) {
// You are given a file (filename) containing a different random phrase on each line. Return an
// ArrayList containing each phrase, but without the first word of each phrase.
//
// Example: If the files contains the 2 phrases "roofed crossover" and "beneficiary charles frederick worth" the
// ArrayList should contain "crossover" and "charles frederick worth"
ArrayList<String> al = new ArrayList<String>();
try {
for(String s : Files.readAllLines(Paths.get(filename))){
al.add(s.substring(9));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return al;
}
评分者的评价如下:
Incorrect on input: data/phrases0.txt
Expected output : [algae, blood platelet, charles frederick worth, convert, crossover, eye movement, ferocity, itch, lake albert, loewi, mountainside, peach, sontag, specialty, supposition, surprised endometriosis, testimonial, trial golden fleece, waterproofing, wrongdoer]
Your output : [ferocity, peach, ed algae, wi, ossover, ry charles frederick worth, ised endometriosis, wrongdoer, lake albert, ng waterproofing, d eye movement, mountainside, g testimonial, c itch, tal sontag, ive blood platelet, golden fleece, ic specialty, convert, s supposition]
我已经return返回了一些没有第一个字符串的值,但是有些单词比子字符串可以达到的要大。
迭代拆分字符串时忽略第一项可能是最简单的方法
data = "";
for(String s : Files.readAllLines(Paths.get(filename))){
line = s.split(",");
for (int i = 1; i < line.length; i++) {
String data = line[i] + System.getProperty("line.separator");
list.add(data);
}
}
根据评论,您还需要在输出中使用逗号。
您只需要删除 space 之前的第一个单词,然后只需使用此代码:从 space 字符的位置获取输入字符串的子字符串。
for(String s : Files.readAllLines(Paths.get(filename))){
al.add(s.substring(s.indexOf(" ")+1)));
}
我已经编写了将数据值读取和写入数组列表的功能代码。尽管它 return 包含所有内容,但如果一个值有多个单词,我如何排除第一个单词?
// 3 points
static ArrayList<String> Q2(String filename) {
// You are given a file (filename) containing a different random phrase on each line. Return an
// ArrayList containing each phrase, but without the first word of each phrase.
//
// Example: If the files contains the 2 phrases "roofed crossover" and "beneficiary charles frederick worth" the
// ArrayList should contain "crossover" and "charles frederick worth"
ArrayList<String> al = new ArrayList<String>();
try {
for(String s : Files.readAllLines(Paths.get(filename))){
al.add(s.substring(9));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return al;
}
评分者的评价如下:
Incorrect on input: data/phrases0.txt
Expected output : [algae, blood platelet, charles frederick worth, convert, crossover, eye movement, ferocity, itch, lake albert, loewi, mountainside, peach, sontag, specialty, supposition, surprised endometriosis, testimonial, trial golden fleece, waterproofing, wrongdoer]
Your output : [ferocity, peach, ed algae, wi, ossover, ry charles frederick worth, ised endometriosis, wrongdoer, lake albert, ng waterproofing, d eye movement, mountainside, g testimonial, c itch, tal sontag, ive blood platelet, golden fleece, ic specialty, convert, s supposition]
我已经return返回了一些没有第一个字符串的值,但是有些单词比子字符串可以达到的要大。
迭代拆分字符串时忽略第一项可能是最简单的方法
data = "";
for(String s : Files.readAllLines(Paths.get(filename))){
line = s.split(",");
for (int i = 1; i < line.length; i++) {
String data = line[i] + System.getProperty("line.separator");
list.add(data);
}
}
根据评论,您还需要在输出中使用逗号。
您只需要删除 space 之前的第一个单词,然后只需使用此代码:从 space 字符的位置获取输入字符串的子字符串。
for(String s : Files.readAllLines(Paths.get(filename))){
al.add(s.substring(s.indexOf(" ")+1)));
}