Results 1 to 6 of 6
  1. #1
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default New to Java with a lot of problems in my code- please help

    I am coding in blueJ. My objectives are this: 1)Write a User class

    A User:

    has a username e.g 'fj3'
    has a userType which can be: 'user', 'editor' or 'admin'
    has a name e.g 'Francis'
    has a constructor which takes the username, userType and name as parameters
    has a getUsername() method
    has a getUserType() method
    has a getName() method
    has a setUserType() method which takes one of the user types as a parameter

    2)Write a UserGroup class

    -The UserGroup class must have an ArrayList of Users.

    Write a constructor for the UserGroup class. It should instantiate the ArrayList.

    In UserGroup write a method called .addSampleData() which creates 10 Users and using the ArrayList's add() method put the 10 new User objects into the ArrayList.

    In UserGroup write a getUser method which takes an int as a parameter and returns the User in that slot of the ArrayList.

    In UserGroup write a printUsernames() method in UserGroup:

    Using an enhanced for loop (see above), loop through the ArrayList and print the username and userType of each user in the ArrayList.

    What I have so far is:
    Java Code:
    public class User{
         public enum UserType{                           
            ADMIN, EDITOR, USER;
         }
    
         public String id;                             
         public UserType userPermissions;               
         public String actualName;                      
    
         public User(String username, UserType userType, String name){
             id = username;
             userPermissions = userType;
             actualName= name;
         } 
    
        public String getUsername(){
            return id;
        }
    
        public UserType getUserType(){
            return userPermissions;
        }       
    
        public String getName(){
            return actualName;
        }
    
        public void setUserType(UserType input){
            userPermissions = input;
        }
    }
    And my UserGroup class:

    Java Code:
    import java.util.*;
    import User.UserType; 
    
    public class UserGroup{
    
        private static ArrayList<User> People;
    
        public UserGroup(){
            People = new Arraylist<User>();
        }
    
        public static void addSampleData(String username, UserType userType, String name){
            People.add(new User(username, userType,name));
        }
    
        public String getUser(int list){
            return User; 
        }
    
        for (User user: groupArray){
            System.out.printf("%s %s\n", user.getUsername(), user.getuserType);
        }
    }
    This is obviously far from being complete but I am completely stuck. My first problem is that "for (User user : groupArray)" is giving me the error illegal start of type. Please help me with this!! I think my User class is fine but my UserGroup class is nowhere near completing all the objectives and I don't know how to do them!!
    Last edited by tsing520; 11-10-2013 at 01:56 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: New to Java with a lot of problems in my code- please help

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Note: The import statement should not include the keyword: package.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: New to Java with a lot of problems in my code- please help

    I am using blueJ so the full error message is just "illegal start of type" and the 20th line of UserGroup is highlighted when i try to compile. I tried 'for (User user: (User[])UserGroup.toArray()){' as well for this part but I get the same error message!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: New to Java with a lot of problems in my code- please help

    Without the full text of the error messages it will be hard to say what the problems are.

    Please copy full text of the compiler's error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: New to Java with a lot of problems in my code- please help

    How would I go about getting this? Should I copy my code into notepad and compile using javac on hte command line or is there some way I can get the full text of the error message in blueJ?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: New to Java with a lot of problems in my code- please help

    Sorry, I have no idea about how to use bluej. The javac command gives nice error messages.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. My first Java Code [PROBLEMS]
    By JoshuaBritton in forum New To Java
    Replies: 8
    Last Post: 04-12-2012, 10:57 PM
  2. A few problems with my code..
    By Kerrai in forum New To Java
    Replies: 1
    Last Post: 10-25-2010, 07:49 PM
  3. Problems with my source code
    By bilak09 in forum New To Java
    Replies: 10
    Last Post: 04-26-2010, 06:23 AM
  4. Problems with code
    By jforce93 in forum New To Java
    Replies: 8
    Last Post: 08-14-2009, 02:49 AM
  5. problems with java code! (very new - need help asap!)
    By sumkindafreek in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 05:00 AM

Tags for this Thread

Posting Permissions

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