我收到错误:
错误:(65,3)java:非法启动表达式
参考这一行:
public boolean equals(WordList wordList)
我认为这是由字符串数组WordList[]
的范围引起的。但是,似乎这应该是可以接受的,因为我在构造函数中调用变量的实例。
我尝试将WordList[]
构造更改为public equals(WordList wordList)
,boolean equals(WordList wordList)
和其他组合,但这些组合都没有更改错误消息。
代码:
public class WordList
{
String[] words;
public int count;
//constructor
public WordList()
{
//create a size two array of strings and assign it to words instance variable
words = new String[2];
count = 0;
}
public int addWord(String word)
{
if(findWord(word) == -1) //word not in list
{
return count;
}
if(words.length == count)
{
String[] temp = new String[words.length * 2];
for(int n = 0; n < words.length; n++)
{
temp[i] = words[i];
}
words = temp;
}
words[count] = word;
count++;
return count;
}
public void removeWord(String word) //void bc returns nothing
{
int index = findWord(word); // to minimize how many times we call method
if(index == -1)
{
return;
}
for(int n = index; n < count -1; n++)
{
words[n] = words[n + 1];
}
words[count - 1] = "";
count --;
return;
}
public int findWord(String word) {
//iterate over each word in current list
//return index of word if found
for (int i = 0; i < count; i++)
{
if (words[i].equals(word))
{
return i;
}
return -1;
}
public boolean equals(WordList wordList)
{
boolean boolEquals;
//override equals method in Object class
//first checks if number of words in each WordList is equal
//if true -> iterate through all words in 1 of lists +
if(count == wordlist.count)
{
for(int i = 0; i < count; i++)
{
if(findWord(words[i]) == -1)
{
boolEquals = false;
}
boolEquals = true;
}
}
boolEquals = false;
return boolEquals;
}
public String toString()
{
//provide number of words in a string and then list each word on a new line
String result = "";
result += "There are " + count + " words in the word list: \n";
for(int i = 0; i < count; i++)
{
result += words[i] + "\n";
}
return result;
}
public static void main(String[] args)
{
}
导致此错误消息的原因是什么?我宁愿保留构造函数,尽可能少地对代码进行更改,以使其成为一种学习体验,而不是简单地从其他人那里获取代码。
答案 0 :(得分:5)
方法中的语法错误 - 缺失}
public int findWord(String word){
答案 1 :(得分:2)
您的代码在第62行缺少'}'
你可能应该在第26行将'i'替换为'n'......