Results 1 to 7 of 7
Thread: Array Help
- 05-08-2011, 01:14 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
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.
- 05-08-2011, 01:20 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
First, since they both come from the same super class you can hold them in an array of the super class type
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?Java Code:SuperClass[] users = new SuperClass[some size]
- 05-08-2011, 01:25 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
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.
- 05-08-2011, 01:50 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-08-2011, 02:01 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
- 05-08-2011, 02:38 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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(); } } }
- 05-09-2011, 04:44 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
create a 2d char array from a 1D string array
By jschmall12 in forum New To JavaReplies: 1Last Post: 04-27-2010, 09:01 PM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks