Results 1 to 7 of 7

Thread: Array Help

  1. #1
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default Array Help

    Hi guys,

    i am having trouble trying to figure out how to do my assignment.

    i need to read in a file of login details (done)

    they can be either Instructors or Admins, the problem im having is creating objects of the admin class or instructor class which both extend an Abstract user class.

    i was thinking of checking each key in the array, if it contained instructor then create an instructor object.

    if it contained admin, create an admin object.

    the problem with this i'm having is when i do this the array im storing the objects in muist be of a type, so Instructor and Admin are 2 different types.

    could anyone give me some pointers of how to go about accomplishing this.

    i dont just want the answer because i'll never learn that way.

    Thanks


    Java Code:
    import java.io.*;
    
    public class RequestApp {
    
       private static String[] users;
       private static String[] requests;
       private static Admin[] adminArray;
       private static Instructor[] instructorArray;
      
    
    public static void main(String[] args) throws IOException {
          // read login text file and create user objects
    
          try {
             ReadFile file = new ReadFile();
            instructorArray= new Instructor[file.readLines("login.txt")];
            adminArray= new Instructor[file.readLines("login.txt")];
             
             users = file.openFile("login.txt");
    
             for (int i = 0; i < users.length; i++) {
    
                if (users[i].contains("instructor")) {
                   instructorArray[i] = new Instructor(users[i]);
                }
             }
             for (int i = 0; i < users.length; i++) {
                if (users[i].contains("admin")) {
                   adminArray[i] = new Admin(users[i]);
                }
             }
    
          }
    
          catch (IOException e) {
             System.out.println(e.getMessage());
          }
    
    
          
    
       }
    Last edited by maknib; 05-08-2011 at 01:17 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First, since they both come from the same super class you can hold them in an array of the super class type
    Java Code:
    SuperClass[] users = new SuperClass[some size]
    it may be easier to use a list of some sort. You should also Corinne reading and parsing the data into objects at the same time. What is the format of the file you are reading?

  3. #3
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    First, since they both come from the same super class you can hold them in an array of the super class type
    Java Code:
    SuperClass[] users = new SuperClass[some size]
    it may be easier to use a list of some sort. You should also Corinne reading and parsing the data into objects at the same time. What is the format of the file you are reading?

    The file is logins.txt
    and instructors are..
    username | password | role | course
    or admin is just
    username | password | role

    some instructors don't have course.


    so i could do.. User userArray = new User[length]; ??

    this still confuses me as Admin class has different methods than instructor class.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A good thing about polymorphism is the ability to have an array(or list) of an interface or super class. If you declare an array of some super class(abstract or not) you can fill it with any class that extends the super class. For now just worry about filling the array before doing anything useful with it.

    It would be really nice to read in one line and create a correct object type. Since each entry is on a new line this is very possible. You can read in one line, split it up(with split()) and then create the correct object. Finally you can add the newly created object to the array(or a list). The list will be easier since it can be easily resized as entries are adding. Arrays are fixed size.

    Let me know if anything is unclear.

  5. #5
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    First, since they both come from the same super class you can hold them in an array of the super class type
    Java Code:
    SuperClass[] users = new SuperClass[some size]
    it may be easier to use a list of some sort. You should also Corinne reading and parsing the data into objects at the same time. What is the format of the file you are reading?
    Quote Originally Posted by sunde887 View Post
    A good thing about polymorphism is the ability to have an array(or list) of an interface or super class. If you declare an array of some super class(abstract or not) you can fill it with any class that extends the super class. For now just worry about filling the array before doing anything useful with it.

    It would be really nice to read in one line and create a correct object type. Since each entry is on a new line this is very possible. You can read in one line, split it up(with split()) and then create the correct object. Finally you can add the newly created object to the array(or a list). The list will be easier since it can be easily resized as entries are adding. Arrays are fixed size.

    Let me know if anything is unclear.
    and then create the correct object. Finally you can add the newly created object to the array(or a list).

    i think that just helped me. back to the drawing board to try again :)
    i was thinking in a whole incorrect manner

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped. Come back if you need more help.

    Another amazing thing about using an abstract class and polymorphism is that you can call a method of the abstract class on the objects of the super class array without knowing which object type it is. If this is unclear, I wrote a fairly small snippet which shows what I mean, compile and run it, it may be helpful.

    Java Code:
    abstract class X{
    	abstract void doSomething();
    }
    class Y extends X{
    	public void doSomething(){
    		System.out.println("Do something - " + getClass().getName());
    	}
    }
    class Z extends X{
    	public void doSomething(){
    		System.out.println("Do something - " + getClass().getName());
    	}
    }
    
    public class PolyTest{
    	public static void main(String[] args){
    		X[] ys = new X[10];
    		for(int i = 0; i < 5; ++i){
    			ys[i] = new Y();
    		}
    		for(int i = 5; i < 10; ++i){
    			ys[i] = new Z();
    		}
    		for(int i = 0; i < ys.length; ++i){
    			ys[i].doSomething();
    		}
    	}
    }

  7. #7
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Glad to have helped. Come back if you need more help.
    thanks for your help sunde :)

    i think i have it working at the moment :)
    had an error then realised the error came from Main and not the class

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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