用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();
}
}
# 1 楼答案
将此添加到代码中:
生成此输出:
这不是三个字,而是五个字。你应该有6个。这使得你的bug更容易理解。你正在经历一个典型的off-by-one error。您只需要追加和计算空格前的单词。这就省去了最后一个单词,因为它不出现在空格之前,只出现在最后一个空格之后
下面是一些与您开始使用的代码相近的代码,可以看到所有6个单词:
当使用此测试时:
将产生以下结果:
另外,我不知道从何处导入
Date
,但是import java.util.Date;
和import java.sql.Date;
都不会使代码无错误。我不得不注释掉你的代码如果你的老师允许,你当然可以尝试其他答案中的想法,但我想你想知道发生了什么
# 2 楼答案
试试这个逻辑
结果:
更新
{}不是一个选项吗?使用
regex
可以尝试以下操作:结果:
正则表达式返回具有以下字符的单词[a-zA-Z_0-9]
# 3 楼答案
String.indexOf
还提供了一个重载,允许从特定点进行搜索(链接到API)。使用此方法非常简单:# 4 楼答案
我不知道它的效率有多高,但是每次你对它进行索引时,你能把字符串剪下来吗?例如:
tempBlog的内容:
这是一个测试
这是一个测试
测试
测试
摘要内容:
这
是
a
测试
# 5 楼答案
我认为我们可以通过检查字符是否为空格字符来找到前10个单词的索引。以下是一个例子:
我的笔记本电脑上的输出: