回答
只是一个指针,可以优化代码:
public static void main(String[] args) {
String str = "The picture quality is great of this camera";
StringTokenizer st = new StringTokenizer(str);
int numberOfWords = 0;
boolean start = false;
while(st.hasMoreTokens()){
String token = st.nextToken();
if(token.equals("quality")){
start = true;
continue;
}
if(start) {
if(token.equals("great")){
start = false;
}
else {
numberOfWords++;
}
}
}
System.out.println(numberOfWords);
}
Downvote!明智足以留下评论? – NINCOMPOOP 2013-04-25 05:33:08
我没有downvote你,但我想这是因为你没有显示任何参数化,并通过标记整个字符串使用过于复杂的方法。 – 2013-04-25 07:50:40
@Noob它的工作原理和感谢你这么多 – 2013-04-26 07:21:31
- 也许从String.split(...)开始获取所有单词的数组。
- 然后你可以搜索数组中的两个单词。你知道这两个词的索引,你可以确定距离。
这里是我的解决方案:
public static void main(String[] args) {
String input = "The picture quality is great of this camera";
// happy flows
System.out.println(findDistance(input, "quality", "great"));
System.out.println(findDistance(input, "picture", "of"));
// words in reversed order
System.out.println(findDistance(input, "camera", "great"));
// non occurring words
try {
System.out.println(findDistance(input, "picture", "camcorder"));
}
catch(IllegalArgumentException e) {
System.out.println("Expected exception caught, message was: " + e.getMessage());
}
}
private static int findDistance(String input, String word1, String word2) {
// check input
if (input == null) {
throw new IllegalArgumentException("Input cannot be null");
}
if (word1 == null || word2 == null) {
throw new IllegalArgumentException("Two words should be provided");
}
// determine boundaries
int pos1 = input.indexOf(word1);
int pos2 = input.indexOf(word2);
// check boundaries
if (pos1 < 0 || pos2 < 0) {
throw new IllegalArgumentException("Both words should occur in the input");
}
// swap boundaries if necessary to allow words in reversed order
if (pos1 > pos2) {
int tmp = pos1;
pos1 = pos2;
pos2 = tmp;
}
// obtain the range between the boundaries, including the first word
String range = input.substring(pos1, pos2);
// split the range on whitespace
// minus one to not count the first word
return range.split("\\s").length - 1;
}
有一个愉快的一天(其卓越的画质)!
为什么你使用一个数组参数,当你期望正好两个单词? – 2013-04-25 08:11:46
@Jacob你读我的思想,只是改变( - : – 2013-04-25 08:16:24
- 1. 两个词之间的语义距离
- 2. 提取字符向量中两个特定单词之间的所有单词
- 3. 如何找到两个特定字符串之间的数字?
- 4. 计算两个字符串之间的levenshtein距离
- 5. 确定数组中两个特定元素之间的距离?
- 6. 距离之间的两个
- 7. 两个单词之间的最小距离(python)
- 8. 获取unix中两个特定字符之间的字符串
- 9. 几何:找到两点之间的特定距离
- 10. 单词之间的详细距离
- 11. 在c中的两个单词之间提取字符串#
- 12. 删除unix中两个单词之间的字符串
- 13. C从两个单词之间的字符串中提取单词
- 14. 在两个特定字符串之间输出n行文本
- 15. 从java中的一个字符串中分出特定单词
- 16. REGEX捕获两个字符串之间的所有单词
- 17. Python - 剪切两个字符串之间的所有单词
- 18. 查找字符串中的2个预定字词之间的所有字符
- 19. 获取所有两个特定单词之间的词在python
- 20. Python - 如何查找并保存两个特定字符串之间的所有单词
- 21. 如何找到两个字符串之间的子字符串?
- 22. 查找两个字符串之间的所有子字符串
- 23. vb.net查找两个字符串之间的字符串
- 24. 如何找到android中的两个区域之间的距离
- 25. 2个子字符串之间的距离
- 26. VBA - 2个字符串之间的距离
- 27. 正则表达式匹配两个特定单词之间的连字符
- 28. 如何找到openlayers中两个标记之间的距离?
- 29. 在Python中找到两个gps点之间的距离
- 30. 两个GEO位置之间的距离
System.out.println(“1”); – Osiris 2013-04-25 05:19:43
@Osiris哈哈,但没有道理。欧普举了一个例子来澄清问题。在这种情况下,你有点迂腐( - : – 2013-04-25 07:43:41
因为它的功能就像一个家庭作业(我假设它是这样的),它是求代码而不是问,它也没有显示研究成果 – 2013-04-25 08:12:33