Java Programming Problems [Help please!]

Kyr

Evil Dictator Fluffkins
Adventurer
Joined
Mar 8, 2005
Messages
114
Linkshell
Holiday
Well, school starts up again, been a few years since I did anything serious with Java, and I'm afraid my memory/coding abilities are a bit rusty. Been running into a few problems, was hoping anyone who is familiar with coding in Java would be able to help answer some questions as I come up with them.

Anyway, I'll start off with my initial problem, and work from there if/when my questions can be answered.


1.

My first problem, concerns objects, creating an array of objects, and then actaully using methods on those. Allow me to be a bit more specific with coding and my actual problem.


The idea is to create an object student, and then an array we'll call listOfStudents. I need to be able to create a new student and add it to listOfStudents. I think I may have got the general idea of how to go about that, but my main problem comes from when you utilize different classes to perform specific methods in the main.

Apologies for the rather weak, crumby, or massively faulty code. Like I said, it's been a few years since I did any coding, and I didn't do much back then anyway, so I hope someone can understand what I'm trying to do with it, give me pointers, and correct my numerous errors I'm sure I have.

student class
Code:
package practice;

public class student {

    /** Creates a new instance of student */
    public student(String studentName, int studentID, String email, String phoneNumber) {}
}

listOfStudents class
Code:
package practice;

public class listOfStudents {
    
    private int size = 100;
    private int count = 0;
    public student[] studentList;
    
    /** 
     * Creates a new instance of listOfStudents
     */
    
    public listOfStudents() {
            
        studentList = new student[size];
    }
    public void addStudent(student newStudent){
            if( count != size){
                 studentList[count] = newStudent;
                 count = count+1;
                 }
            else{
                 System.out.println("The Array is Full!");
                 }
     }
}

main class
Code:
package practice;

public class Main {
   
    /** Creates a new instance of Main */
    public Main() {
    } 
    
     public static void main(String[] args){
            student s1 = new student("John", 1234, "[email protected]", "800-9000");
            studentList.addStudent(s1);
            }
}


Thoughts on where to start?

The only part that won't compile is this line
Code:
studentList.addStudent(s1);
which leaves me with my problem at hand. How exactly do I go about doing this? At this point in time, I'm at a loss. Perhaps my student isn't compatable with my array for students, perhaps it's because of how I set up my classes, etc. I just don't know. I have a hunch that it's something to do with me trying to call an object/method incorrectly, or some such. Perhaps I'm trying to do it in the wrong place, or perhaps I'm doing it wrong altogether.

Anyways, please post any thoughts you can, and ask me questions, and I'll better explain what I'm trying to do if it's not clear.

It's so basic I know, and yet I just can't remember how to handle it. I'm afraid I've forgotten what belongs where.

Thanks in advance.
 
The immediate problem is, you haven't initialized the variable studentList in main ever.

If you wan't to avoid working with global variables (I would probably have the student list as a global variable for this, but I'll humor you) the code would look something like this:

package practice;

public class Main {

/** Creates a new instance of Main */
public Main() {
}

public static void main(String[] args){
listOfStudents studentList = new listOfStudents()
student s1 = new student("John", 1234, "[email protected]", "800-9000");
studentList.addStudent(s1);
}
}

This will create an empty studentList object, which you can then do whatever you want to to. If you want to use it as a global variable, the code would look like this:


package practice;

public class Main {

/** Creates a new instance of Main */
public Main() {
} /*<<< You don't need this, it's kind of pointless unless you plan on creating an object. The "public static void Main the system" calls without creating an instance of your Main class. To make things simpler for you, I would change the name of the class from Main to something more user-friendly.*/

public/private listOfStudents studentList = new listOfStudents()/*here you would use public or private to determine whether or not other classes could directly see the studentList.*/
public static void main(String[] args){

student s1 = new student("John", 1234, "[email protected]", "800-9000");
studentList.addStudent(s1);
}
}
 
  • Like
Reactions: Kyr
Thank you very much! I figured it was something simple like this, totally forgot the obvious.

That solved my problem perfectly. We'll see what other simple issues I run into as I go along further...oh how fun it is to be programming again.
 
Back
Top