我应该如何解决这个问题;“表达式开头非法”;Java中的错误?
我得到一个错误: 错误:(65,3)java:表达式的开头非法 关于这一行:我应该如何解决这个问题;“表达式开头非法”;Java中的错误?,java,arrays,boolean,boolean-logic,Java,Arrays,Boolean,Boolean Logic,我得到一个错误: 错误:(65,3)java:表达式的开头非法 关于这一行: public boolean equals(WordList wordList) 我认为这是由字符串数组WordList[]范围内的某些内容引起的。然而,这似乎是可以接受的,因为我正在调用构造函数中变量的实例 我已尝试将WordList[]结构更改为public equals(WordList WordList),boolean equals(WordList WordList),以及其他组合,尽管这些组合都没有更改
public boolean equals(WordList wordList)
我认为这是由字符串数组WordList[]
范围内的某些内容引起的。然而,这似乎是可以接受的,因为我正在调用构造函数中变量的实例
我已尝试将WordList[]
结构更改为public equals(WordList WordList)
,boolean equals(WordList WordList)
,以及其他组合,尽管这些组合都没有更改错误消息
代码:
公共类单词列表
{
字符串[]个单词;
公共整数计数;
//建造师
公共词表()
{
//创建一个大小为2的字符串数组,并将其分配给Word实例变量
单词=新字符串[2];
计数=0;
}
公共int addWord(字符串字)
{
if(findWord(word)==-1)//单词不在列表中
{
返回计数;
}
if(words.length==计数)
{
字符串[]临时=新字符串[words.length*2];
for(int n=0;n遍历列表中1个+中的所有单词
if(count==wordlist.count)
{
for(int i=0;i
导致此错误消息的原因是什么?我希望保留构造函数并对代码进行尽可能少的更改,以使其成为一种学习体验,而不是简单地从其他人那里获取代码。您的代码在第62行缺少一个“}”
您可能也应该在第26行将“i”替换为“n”…语法错误-缺少} 公共整数findWord(字符串字){
使用适当的制表符可以更容易地解决此问题。如果您的代码进行了合理的缩进,则很容易看出问题所在。@Compass我应该如何使用制表符?谢谢,这些建议现在已经解决了谢谢,现在已经解决了
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)
{
}