Java 找出字符串中两个特定单词之间的距离
我想编写一个Java程序来确定字符串中两个给定单词之间的单词数(距离) 例如,在字符串“该相机的图像质量非常好。” “质量”和“伟大”之间的距离是一Java 找出字符串中两个特定单词之间的距离,java,Java,我想编写一个Java程序来确定字符串中两个给定单词之间的单词数(距离) 例如,在字符串“该相机的图像质量非常好。” “质量”和“伟大”之间的距离是一 可以从String.split(…)开始获取所有单词的数组 然后可以在数组中搜索这两个单词。一个你知道这两个词的索引,你就可以确定距离 只要一个指针,就可以优化代码: public static void main(String[] args) { String str = "The picture quality is great of
只要一个指针,就可以优化代码:
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);
}
以下是我的解决方案:
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;
}
publicstaticvoidmain(字符串[]args){
String input=“这台相机的图像质量非常好”;
//快乐的流动
System.out.println(findInstance(输入,“质量”,“很棒”));
System.out.println(findDistance(输入,“picture”,“of”));
//倒序词
System.out.println(findDistance(输入,“照相机”,“很棒”));
//不出现的词
试一试{
System.out.println(findDistance(输入,“图片”、“摄像机”);
}
捕获(IllegalArgumentException e){
System.out.println(“捕获到预期的异常,消息为:+e.getMessage());
}
}
私有静态int finddInstance(字符串输入、字符串字1、字符串字2){
//检查输入
如果(输入==null){
抛出新的IllegalArgumentException(“输入不能为null”);
}
if(word1==null | | word2==null){
抛出新的IllegalArgumentException(“应提供两个单词”);
}
//确定边界
int pos1=input.indexOf(word1);
int pos2=input.indexOf(word2);
//检查边界
如果(位置1<0 | |位置2<0){
抛出新的IllegalArgumentException(“两个单词都应该出现在输入中”);
}
//如有必要,交换边界,以允许单词按相反顺序排列
如果(pos1>pos2){
int-tmp=pos1;
pos1=pos2;
pos2=tmp;
}
//获取边界之间的范围,包括第一个单词
字符串范围=输入。子字符串(pos1,pos2);
//在空格上拆分范围
//减1不计算第一个单词
返回范围.split(\\s”).length-1;
}
祝你有一个愉快的一天(这是一个很好的图片质量) 系统输出打印项次(“1”)@欧西里斯哈哈,但没有道理。Op举了一个例子来澄清这个问题。在这种情况下,你有点迂腐(-:关闭,因为它的措辞像一个家庭作业(我想是这样的)它是在乞求代码而不是询问。它也没有显示任何研究成果。该死!就在我回答它之后。向下投票!明智到可以留下评论吗?我没有向下投票给你,但我猜这是因为你没有显示任何参数化,并且使用了一种过于复杂的方法来标记整个字符串。@Noob它工作正常,非常感谢你放松投票,恢复理智。当你只需要两个单词时,为什么要使用数组参数?@Jacob你读到了我的想法。刚刚改变了那个(-: