有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用indexof计算单词

我必须数一数正在阅读的博客文章中的前10个单词。。。但我的代码不允许这种情况发生。我不能用。拆分或字符串为空或数组。。。这就给我留下了索引和子字符串。我的代码现在只得到前3个单词。。。对我有什么帮助吗

这是我必须使用的

String getSummary()方法 1.返回条目的前十个单词作为条目摘要。如果条目包含10个字或更少,则该方法返回整个条目。 2.可能的逻辑-字符串类的indexOf方法可以找到空间的位置。将其与循环结构一起使用,以查找前10个单词

public class BlogEntry 
{
    private String username;
    private Date dateOfBlog;
    private String blog;

    public BlogEntry() 
    {
        username = "";
        dateOfBlog = new Date();
        blog = "";
    }

    public BlogEntry(String sName, Date dBlogDate, String sBlog)
    {
        username = sName;
        dateOfBlog = dBlogDate;
        blog = sBlog;
    }

    public String getUsername()
    {
        return username;
    }

    public Date getDateOfBlog()
    {
        return dateOfBlog;
    }

    public String getBlog()
    {
        return blog;
    }

    public void setUsername(String sName)
    {
        username = sName;
    }

    public void setDateOfBlog(Date dBlogDate)
    {
        dateOfBlog.setDate(dBlogDate.getMonth(), dBlogDate.getDay(), dBlogDate.getYear());
    }

    public void setBlog(String sBlog)
    {
        blog = sBlog;
    }

    public String getSummary()
    {
        String summary = "";
        int position;
        int wordCount = 0;
        int start = 0;
        int last;

        position = blog.indexOf(" ");
        while (position != -1 && wordCount < 10)
        {
            summary += blog.substring(start, position) + " ";
            start = position + 1;
            position = blog.indexOf(" ", position + 1);
            wordCount++;
        }

        return summary;
    }

    public String toString()
    {
        return "Author: " + this.getUsername() + "\n\n" + "Date posted: " + this.getDateOfBlog() + "\n\n" + "Text body: " + this.getBlog();
    }
}

共 (5) 个答案

  1. # 1 楼答案

    将此添加到代码中:

    public static void main(String[] args) 
    {
        BlogEntry be = new BlogEntry("" , new Date(), "this program is pissing me off!");
        System.out.println( be.getSummary() );        
    }
    

    生成此输出:

    this program is pissing me
    

    这不是三个字,而是五个字。你应该有6个。这使得你的bug更容易理解。你正在经历一个典型的off-by-one error。您只需要追加和计算空格前的单词。这就省去了最后一个单词,因为它不出现在空格之前,只出现在最后一个空格之后

    下面是一些与您开始使用的代码相近的代码,可以看到所有6个单词:

    public String getSummary()
    {
        if (blog == null) 
        {
            return "<was null>";
        }
    
        String summary = "";
        int position;
        int wordCount = 0;
        int start = 0;
        int last;
    
        position = blog.indexOf(" ");
        while (position != -1 && wordCount < 10)
        {
            summary += blog.substring(start, position) + " ";
            start = position + 1;
            position = blog.indexOf(" ", position + 1);
            wordCount++;
        }
        if (wordCount < 10) 
        {
            summary += blog.substring(start, blog.length());
        }
    
        return summary;
    }
    

    当使用此测试时:

    public static void main(String[] args) 
    {
        String[] testStrings = {
              null //0
            , ""
            , " "
            , "  "
            , " hi"
            , "hi "//5
            , " hi "
            , "this program is pissing me off!"
            , "1 2 3 4 5 6 7 8 9"
            , "1 2 3 4 5 6 7 8 9 "
            , "1 2 3 4 5 6 7 8 9 10"//10
            , "1 2 3 4 5 6 7 8 9 10 "
            , "1 2 3 4 5 6 7 8 9 10 11"
            , "1 2 3 4 5 6 7 8 9 10 11 "
            , "1 2 3 4 5 6 7 8 9 10 11 12"
            , "1 2 3 4 5 6 7 8 9 10 11 12 "//15
        };
    
        ArrayList<BlogEntry> albe = new ArrayList<>();
    
        for (String test : testStrings) {
            albe.add(new BlogEntry("" , new Date(), test));
        }
    
        testStrings[0] = "<was null>";
    
        for (int i = 0; i < albe.size(); i++ ) {
            assert(albe.get(i).getSummary().equals(testStrings[Math.min(i,11)]));
        }
    
        for (BlogEntry be : albe)
        {
            System.out.println( be.getSummary() );        
        }
    }
    

    将产生以下结果:

    <was null>
    
    
    
     hi
    hi 
     hi 
    this program is pissing me off!
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9 
    1 2 3 4 5 6 7 8 9 10
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    

    另外,我不知道从何处导入Date,但是import java.util.Date;import java.sql.Date;都不会使代码无错误。我不得不注释掉你的代码

    如果你的老师允许,你当然可以尝试其他答案中的想法,但我想你想知道发生了什么

  2. # 2 楼答案

    试试这个逻辑

    public static void main(String[] args) throws Exception {
            public static void main(String[] args) throws Exception {
        String data = "This one sentence has exactly 10 words in it ok";
    
        int wordIndex = 0;
        int spaceIndex = 0;
        int wordCount = 0;
        while (wordCount < 1 && spaceIndex != -1) {
            spaceIndex = data.indexOf(" ", wordIndex);
            System.out.println(spaceIndex > -1 
                    ? data.substring(wordIndex, spaceIndex)
                    : data.substring(wordIndex));
    
            // The next word "should" be right after the space
            wordIndex = spaceIndex + 1;
            wordCount++;
        }
    }
    

    结果:

    This
    one
    sentence
    has
    exactly
    10
    words
    in
    it
    ok
    

    更新

    {}不是一个选项吗?使用regex可以尝试以下操作:

    public static void main(String[] args) throws Exception {
        String data = "The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog";
        Matcher matcher = Pattern.compile("\\w+").matcher(data);
    
        int wordCount = 0;
        while (matcher.find() && wordCount < 10) {
            System.out.println(matcher.group());
            wordCount++;
        }
    }
    

    结果:

    The
    quick
    brown
    fox
    jumps
    over
    the
    lazy
    dog
    The
    

    正则表达式返回具有以下字符的单词[a-zA-Z_0-9]

  3. # 3 楼答案

    String.indexOf还提供了一个重载,允许从特定点进行搜索(链接到API)。使用此方法非常简单:

    public int countWort(String in , String word){
        int count = 0;
    
        int index = in.indexOf(word);
    
        while(index != -1){
            ++count;
    
            index = in.indexOf(word , index + 1);
        }
    
        return count;
    }
    
  4. # 4 楼答案

    我不知道它的效率有多高,但是每次你对它进行索引时,你能把字符串剪下来吗?例如:

    tempBlog的内容:
    这是一个测试
    这是一个测试
    测试
    测试

    摘要内容:


    a
    测试

    public String getSummary()
    {
        String summary = "";
        int wordCount = 0;
        int last;
        //Create a copy so you don't overwrite original blog
        String tempBlog = blog;
    
        while (wordCount < 10)
        {
            //May want to check if there is actually a space to read. 
            summary += tempBlog.substring(0, tempBlog.indexOf(" ")) + " ";
            tempBlog = tempBlog.substring(tempBlog.indexOf(" ")+1);
            wordCount++;
        }
    
        return summary;
    }
    
  5. # 5 楼答案

    我认为我们可以通过检查字符是否为空格字符来找到前10个单词的索引。以下是一个例子:

    public class FirstTenWords
    {
        public static void main( String[] args )
        {
            String sentence = "There are ten words in this sentence, I want them to be extracted";
            String summary = firstOf( sentence, 10 );
            System.out.println( summary );
        }
    
        public static String firstOf( String line, int limit )
        {
            boolean isWordMode = false;
            int count = 0;
            int i;
            for( i = 0; i < line.length(); i++ )
            {
                char character = line.charAt( i );
                if( Character.isSpaceChar( character ) )
                {
                    if( isWordMode )
                    {
                        isWordMode = false;
                    }
                }
                else
                {
                    if( !isWordMode )
                    {
                        isWordMode = true;
                        count++;
                    }
                }
                if( count >= limit )
                {
                    break;
                }
            }
            return line.substring( 0, i );
        }
    }
    

    我的笔记本电脑上的输出:

    There are ten words in this sentence, I want