Several errors for identifier expected and illegal start of type. Confused

泄露秘密 提交于 2019-12-31 05:14:08

问题


The whole program is supposed to enter a combination lock and accept the combo. This is the code I'm having issues with.

import java.util.*;  // needed for Scanner

public class CombinationLock extends Lock
{
   // Instance Variables
   private String combination;

   Scanner keyboard = new Scanner(System.in);

   System.out.println("Enter Combination --> ");
   String combo = keyboard.nextLine();

   if(combination = combo)
   {
    super.open();
   }

public String toString()
   {
    String str = super.toString() + "\n" +
                 "Combination = " + combination + "\n";  
    return str;
   }

public void setCombination()
{

}

public boolean getCombination()
{

}

public CombinationLock()
{
   super();    // call the default constructor of the Lock class
   combination = "";
}

public CombinationLock(String combo)
{
    super();
    combination = combo;
}


}

These are the errors I'm getting

--------------------Configuration: <Default>--------------------
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:10: error: <identifier> expected
   System.out.println("Enter Combination  ");
                     ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:10: error: illegal start of type
   System.out.println("Enter Combination  ");
                      ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: illegal start of type
   if(combination = combo)
   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: <identifier> expected
   if(combination = combo)
                 ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: ';' expected
   if(combination = combo)
                   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: illegal start of type
   if(combination = combo)
                         ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:13: error: <identifier> expected
   if(combination = combo)
                          ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:14: error: ';' expected
   {
    ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:15: error: illegal start of type
    super.open();
         ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:18: error: class, interface, or enum expected
public String toString()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:22: error: class, interface, or enum expected
    return str;
    ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:23: error: class, interface, or enum expected
   }
   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:25: error: class, interface, or enum expected
public void setCombination()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:30: error: class, interface, or enum expected
public boolean getCombination()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:35: error: class, interface, or enum expected
public CombinationLock()
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:38: error: class, interface, or enum expected
   combination = "";
   ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:39: error: class, interface, or enum expected
}
^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:41: error: class, interface, or enum expected
public CombinationLock(String combo)
       ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:44: error: class, interface, or enum expected
    combination = combo;
    ^
C:\Users\waki_\OneDrive\Documents\CombinationLock.java:45: error: class, interface, or enum expected
}
^
20 errors

Process completed.

I'm not sure what's causing the problem. I've tried to find other answers but none of them were relevant to my problem.


回答1:


Java statements have to appear in a block of code. So in this case, the code you have in this class outside of methods would need to be surrounded by {}.

But it looks like you are trying to read input etc. Ideally what you shoud do is create a main method in this class where you create an instance of CombinationLock and read input in the main method.




回答2:


You need to correct a couple of things:

  1. The code to get user's input should be inside the method. Java doesn't allow writing such logic outside method or blocks.
  2. If condition is not properly implemented. Java expects the expression inside if condition to return a boolean. However, the = operator used inside if results in a String. For String comparisons, we should use string1.equals(string2) inside if.



回答3:


You are writing statements outside of a function. Make sure all statements are inside functions, and that all instance variables have scope identifiers.

Specifically, make sure the following statements are inside a function:

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter Combination --> ");
String combo = keyboard.nextLine();

if(combination.equals(combo))
{
    super.open();
}


来源:https://stackoverflow.com/questions/36253878/several-errors-for-identifier-expected-and-illegal-start-of-type-confused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!