【问题标题】:JAVA :How to determine the index of the next character in a string NOT equal to a set of delimitersJAVA:如何确定字符串中下一个字符的索引不等于一组分隔符
【发布时间】:2017-02-24 04:17:18
【问题描述】:

我认为这个问题对于有经验的程序员来说并不复杂,但我对它很陌生,所以我很挣扎。 我有一个在 java 类之前声明的分隔符列表:

public static final String DELIMITERS = ",<.>/?;:'\"[{]}\\|=+-_)(*&^%$#@!`~ \t\n";

我想创建一个带有两个参数(一个起始索引和一个字符串)的方法。目标是通读字符串并返回与不在上面给出的分隔符​​列表中的字符相对应的下一个索引。如果起始索引是负数或大于文本长度,则该方法应简单地返回 -1。否则它只返回不在分隔符列表中的下一个字符的索引。 这是我目前所拥有的:

public static boolean isDelimiter(char c) {
    String letter = "" + c;
    if(DELIMITERS.contains(letter)){
        return true;
    }
    else{
        return false;
    }
}

public static int posNextWord(int startPosition, String text) {
    boolean isWord = false;
    int nextWordPosition = 0;
    if(startPosition < 0 || startPosition > (text.length()-1)){
        return -1;
    }
    else{
        while(isWord = false) {
            for (int i = startPosition; i < text.length(); i++) {
                if(!isDelimiter(text.charAt(i))){
                    nextWordPosition = nextWordPosition + i + startPosition;
                    isWord = true;
                }
                else{
                    isWord = false;
                }
            }
        }
        return nextWordPosition;
    }

}

}

但是,当我使用示例文本和索引运行该程序时,该方法只返回数字 0。任何帮助都将不胜感激。另外,在 posNextWord() 方法中使用 isDelimiter 方法是必需的。

【问题讨论】:

  • 显示你调用posNextWord的主要方法
  • 我认为你需要在isWord = true;之后break
  • 我真的不能;这是一项更大任务的一部分。此方法只是与类似方法 (posNextDelimiter()) 一起使用,以便创建第三种方法来确定用户输入的一组行的字数
  • 注意while (isWord = false);那应该是==,但在这种情况下,您根本不需要while 循环。如果确实需要 while 循环,while (!isWord) 会更好。

标签: java string count delimiter word


【解决方案1】:

整个代码块就是问题所在:

while(isWord = false) {
    for (int i = startPosition; i < text.length(); i++) {
        if(!isDelimiter(text.charAt(i))){
            nextWordPosition = nextWordPosition + i + startPosition;
            isWord = true;
        }
        else{
            isWord = false;
        }
    }
}
return nextWordPosition;

首先,您只需要一个循环来检查循环中的每个字符。您不需要whilefor。其次,如果你想找到第一个不匹配的char,你找到了就直接返回即可。

像这样:

for (int i = startPosition; i < text.length(); i++) {
    if(!isDelimiter(text.charAt(i))){
        return i + startPosition;
    }
}
return -1; 

【讨论】:

  • @JBarron 没问题,乐于助人!如果这解决了问题,请记得点赞并接受 =]
【解决方案2】:

查看记录的 cmets。如果不清楚,请不要犹豫:

public static boolean isDelimiter(char c) {

    String letter = "" + c;
    if(DELIMITERS.contains(letter)){
        return true;
    }
    //else{ this else is not needed
        System.out.println(letter +" is not a delimiter");
        return false;
    //}
}

public static int posNextWord(int startPosition, String text) {

    //boolean isWord = false; not used 
    int nextWordPosition = 0;
    if((startPosition < 0) || (startPosition > (text.length()-1))){
        return -1;
    }
    //else{   this is not needed
    //while(isWord = false) {

        for (int i = startPosition; i < text.length(); i++) {

            if(!isDelimiter(text.charAt(i))){
                nextWordPosition = nextWordPosition + i + startPosition;
                //isWord = true;
                return nextWordPosition; //as Scary Wombat commented 
            }
            //else{
            //  isWord = false;
            //}

         }
        //isWord = false;
    //}
    return nextWordPosition;
    // }

}

顺便说一句:我认为将 0 作为“未找到”结果返回不是一个好主意。 0 也可以作为有效的“找到”位置返回。

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 2018-07-20
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多