String 从字符串中提取单词
所以我做了这个算法,从一串单词中提取单词,而不需要使用标点符号和空格。它一直工作到最后一个字的第二个字,然后给出一个越界错误。如果您需要查看其他方法,请告诉我String 从字符串中提取单词,string,String,所以我做了这个算法,从一串单词中提取单词,而不需要使用标点符号和空格。它一直工作到最后一个字的第二个字,然后给出一个越界错误。如果您需要查看其他方法,请告诉我 public static double totalSentiment(String fileName) { String list = textToString(fileName); double total = 0; int start = 0; for(int
public static double totalSentiment(String fileName)
{
String list = textToString(fileName);
double total = 0;
int start = 0;
for(int i = 0; i < sentiment.size(); i++)
{
int stop = list.indexOf(' ', start+1);
String word = list.substring(start, stop);
int puncC = word.indexOf(',');
int puncP = word.indexOf('.');
int puncE = word.indexOf('!');
int puncQ = word.indexOf('?');
String polishedWord;
if(puncC != -1)
{
polishedWord = word.substring(0, puncC);
total += sentimentVal(polishedWord);
System.out.println(i + ") " + polishedWord);
}
else if(puncP != -1)
{
polishedWord = word.substring(0, puncP);
total += sentimentVal(polishedWord);
System.out.println(i + ") " + polishedWord);
}
else if(puncE != -1)
{
polishedWord = word.substring(0, puncE);
total += sentimentVal(polishedWord);
System.out.println(i + ") " + polishedWord);
}
else if(puncQ != -1)
{
polishedWord = word.substring(0, puncQ);
total += sentimentVal(polishedWord);
System.out.println(i + ") " + polishedWord);
}
else
{
total += sentimentVal(word);
System.out.println(i + ") " + word);
}
start = stop;
}
return total;
}
publicstaticdouble(字符串文件名)
{
字符串列表=textToString(文件名);
双倍合计=0;
int start=0;
for(int i=0;i
在for循环内部的某个地方,它试图访问一个超出末尾的索引,当您不仅在for循环索引“i”上操作,而且展望未来时,这是很常见的。例如:list.indexOf(“”,start+1);开始总是比列表长度至少少1+1吗?嗯,是的。