Results 1 to 12 of 12
  1. #1
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Unhappy Data not inserted into an object

    I have an object student
    Java Code:
    public class Student {
    	public String id = "";
    	public String name = "";
    	public ManualCar manCar = null;
    	public AutoCar autoCar = null;
    	public MotorBike bike = null;
    	
    	public Student(String id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    	
    	public void display() {
    		System.out.println("========= S T U D E N T =========");
    		System.out.println("Student ID   : " + id);
    		System.out.println("Student Name : " + name);
    		if (manCar != null) {
    			manCar.display();
    		}
    		if (autoCar != null) {
    			autoCar.display();
    		}
    		if (bike != null) {
    			bike.display();
    		}
    	}
    	
    
    }
    I have an object array of 46 which contains a student object from 1-45. 0 is not used

    Java Code:
    public Student[] stud = new Student[46];
    I also created a method to register a student and place the value inside the object array

    Java Code:
    	private void registerStudent() { //Register Student
    		String id = Keyboard.readString("Please enter student ID: ");
    		String name = Keyboard.readString("Please enter student Name: ");
    		for (int i = 1; i < stud.length; i++) {
    			if (stud[i] == null && i < stud.length) {
    				stud[i] = new Student(id, name);
    				break;
    			}
    		}
    		System.out.println("Registration completed successfully");
    	}
    I have another method which generates all the students objects.

    Java Code:
    private void generateStudents() {
    		for (int i = 1; i < stud.length; i++) {
    			stud[i] = new Student("", "");
    		}
    }
    I have a method to print out all entries
    Java Code:
    	private void displayAllStudents() { //Display all students.
    		int count = 0;
    		for (Student i : stud) {
    			if (i != null && i.id != "" && i.name != "") {
    				i.display();
    				count++;
    			}
    		}
    		if (count == 0) {
    			System.out.println("No Students are registered.");
    		}
    	}
    I ran the generateStudents method first which should instantiate all the objects. After that the register student method, I type in an id then name. Although my method says it was successfully registered, when I print out all students, there's no entries at all.
    Last edited by LoViNgHeArTy; 06-27-2011 at 05:58 PM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok so what you're saying that you get no compile errors, no run time errors but you're not getting the correct output. What is printing out?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    Completely nothing, after executing my method to print out all entries after I keyed in entry, I made it to display "No students are registered" if there are no entries at all. It did show there are no entries. I even tried stud[1].display(). It just returns whatever that is in my display method inside student class but there's no value for id and name at all.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    If you first run generateStudents none of the entries in your array are null anymore; you test for that in your other method (to be able to insert a student in that slot).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Did it print "No students are registered"?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    yeah it did print no students are registered. I think I somehow know what's the error. my condition in my register student is a null but I instantiate it already therefore it is not a null anymore. Therefore no entries was entered into the object. Am I correct?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    Try adding some debug code.
    Add a toString method to the Student class that will return "Student: name=" +name + ", id=" + id;
    In the registerStudent() method print out stud[i] for each student looked at.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LoViNgHeArTy View Post
    yeah it did print no students are registered. I think I somehow know what's the error. my condition in my register student is a null but I instantiate it already therefore it is not a null anymore. Therefore no entries was entered into the object. Am I correct?
    Change your test in your display method to this:

    Java Code:
    if (i != null) {
       i.display();
       count++;
    }
    ... and see that your array is filled with the (empty) students that were put in by your first method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Well JosAH pointed that out, but yes. If you are checking for null, but making them not null you will never fill the slot. What you should do is find an alternative to checking.

    I don't see your generate students method, but maybe you could do something in there to see if the slot is used. Maybe an array of booleans that match your student slots.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks guys, I removed the generate students, which should keep everything at null. I made it in a way whereby when register student method was called, then it creates a new object at the very first null index the method finds, it solved my problem, I've included "stud[i] != null" in other methods conditions I have to avoid an exception.

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Glad to see you made it work.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Looping over the array to find the next "empty" slot is expensive and O(n). If you had a variable to store the next available slot then it will be O(1). Replace the for loop with
    Java Code:
    stud[index++] = new Student(id, name);

Similar Threads

  1. How to find latest inserted rows
    By g.raaja in forum JDBC
    Replies: 5
    Last Post: 06-21-2011, 09:26 AM
  2. count inserted line in database
    By tedy2808 in forum New To Java
    Replies: 3
    Last Post: 08-16-2010, 10:57 AM
  3. Replies: 2
    Last Post: 03-16-2010, 08:24 PM
  4. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  5. Replies: 2
    Last Post: 08-10-2007, 12:36 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
  •