Results 1 to 5 of 5
  1. #1
    LeeB96 is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Illegal start of expression compile error?

    I'm trying to write a simple program that prints out the properties of a user in an Array List. When I compile, there is only 1 error for the constructor within the main class. It says "Illegal start of expression" and refers to the word public. Maybe it's because I'm inexperienced as I don't see what it means by this error and once this has been resolved, would the program then print out the properties of the user? Any help would be greatly appreciated!

    Here is the code:

    Java Code:
    import java.util.ArrayList;
    
    public class UserGroup {
        
        ArrayList<User> userGroup = new ArrayList<>();
        User userOne;
        
        public void addSampleData(String username, String userType, String name) {
            
        userGroup.add(new User("LeeB", "Staff", "Lee"));
        
    }
        
        public User getUser(int index)  {
            
        return userGroup.get(0);
        
    }
        
        public void printusername(){
            
           for (User x : userGroup)
               System.out.println(x);
           
        }
    }
    class User {
        
      String username;
      String userType;
      String name;
    
    
        User(String username, String userType, String name) {
            
        this.username = username;
        this.userType = userType;
        this.name = name;
        }
    
      public String getUsername() {
          return username;
      }
    
      public String getUserType() {
          return userType;
      }
    
       public String getName() {
          return name;
      }
    
      public String setUserType(String admin) {
          return userType = admin;
      }
        
    }
    
    public class Main {
     public static void main(String[] args) {
    
        public Main(); {
         
         UserGroup userGroupObject = new UserGroup();
     
     System.out.println(userGroupObject.getUser(0)); 
     }
    
     }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Illegal start of expression compile error?

    Please copy the full text of the error message and paste it here. It has important info about the error.
    For example what source line the error is on. I see lots of places where there is the keyword: public.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Illegal start of expression compile error?

    You should have pointed out which line was causing the issue.

    I expect it was this one?
    Java Code:
    public Main(); {
    You seem to have stuck your constructor inside the main() method, which is the wrong place.

    There's also a spurious semi-colon there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    LeeB96 is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: Illegal start of expression compile error?

    Sorry, it was indeed that line and the error message reads "Main.java:62: error: illegal start of expression"

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Illegal start of expression compile error?

    Your indentation of your UserGroup class is also very confusing. The {} don't align properly.
    And you should always use braces, even for single statements in loops.

    Don't do this:
    Java Code:
    for (User x : userGroup) 
       System.out.println(x);
    Do this:
    Java Code:
    for (User x : userGroup) {
        System.out.println(x);
    }
    It will save you lots of debugging time in the future.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Getting an illegal start of expression error
    By wigwom in forum New To Java
    Replies: 17
    Last Post: 10-06-2013, 09:47 PM
  2. Replies: 12
    Last Post: 10-20-2012, 06:55 PM
  3. Error: illegal start of expression
    By iswan in forum JDBC
    Replies: 2
    Last Post: 09-28-2011, 09:29 AM
  4. Illegal start of expression error pls help me!
    By Gayethiri_86 in forum New To Java
    Replies: 12
    Last Post: 05-12-2010, 03:06 PM
  5. Illegal start of expression error
    By lukermsdn111 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 01:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •