Java If语句在字符串搜索中不起作用

Java If语句在字符串搜索中不起作用,java,string,if-statement,data-structures,Java,String,If Statement,Data Structures,任务 名为getKey1()的函数应该返回一个字符串中出现次数最多的子字符串 我做了什么? public class Proc { public static String getKey1(String str) { LinkedList link = new LinkedList(); int i = 0; int t = 0; String pattern = ""; l1: while(i < str.length()) {

任务

名为getKey1()的函数应该返回一个字符串中出现次数最多的子字符串

我做了什么?

public class Proc {
public static String getKey1(String str)
{
    LinkedList link = new LinkedList();
    int i = 0;
    int t = 0;
    String pattern = "";
    l1:
    while(i < str.length())
    {
        if(str.charAt(i) != ' ')
        {
            t = i;
            for(t = i; t < str.length(); t++)
            {
                if(str.charAt(t) == ' ')
                    break;
                else if( t >= str.length())
                    break l1;
            }
            pattern = str.substring(i, t);
            link.add(pattern);
            i += pattern.length();
        }
        else
            i++;
    }


    LinkedList link2 = new LinkedList();
    LinkedList link3 = new LinkedList();
    Iterator f = link.iterator();

    while(f.hasNext())
    {
        Object o = f.next();
        String ss = (String)o;
        if(link3.contains(o))
        {
            Iterator m = link2.iterator();
            while(m.hasNext())
            {
                Object ro = m.next();
                struct sto = (struct)ro;
                String so = sto.str;
                if(so.equals(ss))
                {
                    sto.count++;
                }
            }
        }
        else
        {
            link3.add(ss);
            link2.add(new struct(ss, 1));
        }
    }

        Iterator k = link2.iterator();
        struct min = new struct("a", 10);

        while(k.hasNext())
        {
            Object ost = k.next();
            struct spt = (struct)ost;
            System.out.println(spt.str + "  " + spt.count);
            if(spt.count > min.count);
            {
                min.str = spt.str;
                min.count = spt.count;
            }
        }
    return min.str;
    }
}
主要类别:

public class Asdf {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println(Proc.getKey1("This is a string. boom boom kaboom boom"));
}

}
输出是

This  1

is  1

a  1

string  1

boom  3

kaboom  1


kaboom
问题

这实际上是一个测试运行,它显示了子字符串在string str中出现的次数。因此,根据这个输出,“boom”应该返回到main,但“kaboom”是返回的。通过进一步测试,我得出结论,最后一个if语句中存在一个问题: 如果(标准计数>最小计数) 看起来,不管发生什么,程序总是在这个if语句中运行。我把它改成了 如果(4<3) 并且仍然得到相同的输出。

在IF行的末尾有一个“;”

if(spt.count > min.count);
在IF行的末尾有一个“;”

if(spt.count > min.count);
在IF行的末尾有一个“;”

if(spt.count > min.count);
在IF行的末尾有一个“;”

if(spt.count > min.count);
所有
if(标准计数>最小计数)
做的是执行null语句
如果其计算结果为true。始终执行此行后大括号中的位

我确信控制流不是有意的

也要考虑重构远离你的代码>中断标签;<代码>反模式。它使代码变得脆弱:太容易意外地移动标签,忘记某人根据其精确位置有一个

断点。

All
if(spt.count>min.count)
做的是执行null语句
如果其计算结果为true。始终执行此行后大括号中的位

我确信控制流不是有意的

也要考虑重构远离你的代码>中断标签;<代码>反模式。它使代码变得脆弱:太容易意外地移动标签,忘记某人根据其精确位置有一个

断点。

All
if(spt.count>min.count)
做的是执行null语句
如果其计算结果为true。始终执行此行后大括号中的位

我确信控制流不是有意的

也要考虑重构远离你的代码>中断标签;<代码>反模式。它使代码变得脆弱:太容易意外地移动标签,忘记某人根据其精确位置有一个

断点。

All
if(spt.count>min.count)
做的是执行null语句
如果其计算结果为true。始终执行此行后大括号中的位

我确信控制流不是有意的


也要考虑重构远离你的代码>中断标签;<代码>反模式。它使代码变得脆弱:很容易不小心移动标签,而忘记某人根据其精确位置有一个

断点。

Memran已经回答了您的具体问题,“if”下面的块将无条件运行,因为分号结束if,而该块只是一个块

然而,除非我误解了你在这里的意图,否则坦率地说,这是你最小的问题:-)

看起来,当你说“子字符串”时,你实际上是指“分隔空间的令牌”。假设是这样的话,这里有一个很好的简单方法,可以将单词分开,数一数,然后寻找最大的数:

String input = "foo bar baz quux quux bar baz foo bar wibble";
List<String> words = Arrays.asList(input.split(" "));

Map<String, Integer> counts = new HashMap<String, Integer>();

for (String word : words) 
{
    Integer count = counts.get(word);
    count = (count == null? 1 : count+1);
    counts.put(word, count);
}

int max = 0;
String maxWord = null;
for (String word : counts.keySet()) 
{
    Integer count = counts.get(word);
    if(count > max) 
    {
        max = count;
        maxWord = word;
    }
}

System.out.println("Most-used word is "+maxWord+" with "+max+" uses.");
String input=“foo-bar-baz-qux-qux-bar-baz-foo-bar-wibble”;
List words=Arrays.asList(input.split(“”));
映射计数=新的HashMap();
for(字符串字:字)
{
整数计数=计数。获取(字);
计数=(计数==null?1:计数+1);
计数。放(字,计数);
}
int max=0;
字符串maxWord=null;
for(字符串字:counts.keySet())
{
整数计数=计数。获取(字);
如果(计数>最大值)
{
最大值=计数;
maxWord=word;
}
}
System.out.println(“最常用的单词是“+maxWord+”和“+max+”uses”);

我相信,如果性能很重要,也有更聪明的方法来实现这一点。但通常简单明了更好。

Memran已经回答了您的具体问题,“if”下面的块将无条件运行,因为分号结束if,然后该块就是一个块

然而,除非我误解了你在这里的意图,否则坦率地说,这是你最小的问题:-)

看起来,当你说“子字符串”时,你实际上是指“分隔空间的令牌”。假设是这样的话,这里有一个很好的简单方法,可以将单词分开,数一数,然后寻找最大的数:

String input = "foo bar baz quux quux bar baz foo bar wibble";
List<String> words = Arrays.asList(input.split(" "));

Map<String, Integer> counts = new HashMap<String, Integer>();

for (String word : words) 
{
    Integer count = counts.get(word);
    count = (count == null? 1 : count+1);
    counts.put(word, count);
}

int max = 0;
String maxWord = null;
for (String word : counts.keySet()) 
{
    Integer count = counts.get(word);
    if(count > max) 
    {
        max = count;
        maxWord = word;
    }
}

System.out.println("Most-used word is "+maxWord+" with "+max+" uses.");
String input=“foo-bar-baz-qux-qux-bar-baz-foo-bar-wibble”;
List words=Arrays.asList(input.split(“”));
映射计数=新的HashMap();
for(字符串字:字)
{
整数计数=计数。获取(字);
计数=(计数==null?1:计数+1);
计数。放(字,计数);
}
int max=0;
字符串maxWord=null;
for(字符串字:counts.keySet())
{
整数计数=计数。获取(字);
如果(计数>最大值)
{
最大值=计数;
maxWord=word;
}
}
System.out.println(“最常用的单词是“+maxWord+”和“+max+”uses”);

我相信,如果性能很重要,也有更聪明的方法来实现这一点。但通常简单明了更好。

Memran已经回答了您的具体问题,“if”下面的块将无条件运行,因为分号结束if,然后该块就是一个块

然而,除非我误解了你在这里的意图,否则坦率地说,这是你最小的问题:-)

看起来,当你说“子字符串”时,你实际上是指“分隔空间的令牌”。假设是这样的话,这里有一个很好的简单方法,可以将单词分开,数一数,然后寻找最大的数:

String input = "foo bar baz quux quux bar baz foo bar wibble";
List<String> words = Arrays.asList(input.split(" "));

Map<String, Integer> counts = new HashMap<String, Integer>();

for (String word : words) 
{
    Integer count = counts.get(word);
    count = (count == null? 1 : count+1);
    counts.put(word, count);
}

int max = 0;
String maxWord = null;
for (String word : counts.keySet()) 
{
    Integer count = counts.get(word);
    if(count > max) 
    {
        max = count;
        maxWord = word;
    }
}

System.out.println("Most-used word is "+maxWord+" with "+max+" uses.");
String input=“foo-bar-baz-qux-qux-bar-baz-foo-bar-wibble”;
List words=Arrays.asList(input.split(“”));
映射计数=新的HashMap();
for(字符串字:字)
{
整数计数=计数。获取(字);
计数=(计数==