Search code examples
javaidentifier

Why am I getting a Identifier expected error


import java.util.*;

public class Students
{
   public static void main(String[] args){
    Scanner scan=new Scanner(System.in);
    Student s1=new Student();//creates object of class aircraft
    Student s2=new Student();
    //or
    //Student s1,s2
    //s1=new Student();
    //s2=new Student();
    String str;
    int i;
    //str=s1.getname();
   } 
}
class Student{ //extends Students{
   String name; 
   int 1;  ?<Identifier> expected? 
   int 2;  ?<Identifier> expected?
   int 3;  
   }
 }

in the last three lines i get identifier expected. why? The student class is supposed to store the name, and three test scores.


Solution

  • In java, variables are not allowed to start with numbers - 1 is an invalid variable name (though num1 is valid). See the naming requirements.

    To fix your code, I would rename them int var1, int var2, int var3. (Though better names would be better - try something more descriptive)