Search code examples
javaprintingarrayobject

cant print the whole array


i need to print an array but it only prints the last info i entered

that's the printing code

   for(int i=0; i<undergrad.length;i++){

        if(undergrad[i]!=null){
        System.out.println("Student Name : "+undergrad[i].getName()+"\n"
                + "Full Address : "+undergrad[i].getAddress()+"\n"
                + "Mobile No :"+undergrad[i].getPhone()+"\n"
                + "Number of Tests : "+undergrad[i].getTests()+"\n"
                + "GBA : "+undergrad[i].GBA()+"\n"
                + "Status : "+undergrad[i].computeGrade()+"\n"
                + "Internship : "+undergrad[i].getInternship()+"\n"
                + "Project Title : "+undergrad[i].getProject_Title()+"\n"
                + "Project Area : "+undergrad[i].getProject_Area()+"\n"
                + "Project Score : "+undergrad[i].computeProjectGrade()+"\n"
                + "****************************************\n");

        } 
    }

the array of objects is of size 10 but i might put info in less than 10 (for example 4) and when i print i need to show all and only the 4 i entered

thats the adding part

for(int i=0; i<undergrad.length; i++){

    undergrad[i]=new Undergraduate();


    System.out.println("Enter Student Name: ");
    undergrad[i].setName(in.nextLine());

    System.out.println("Full Address  : ");
    undergrad[i].setAddress(in.nextLine());

    System.out.println("Mobile No : ");
    undergrad[i].setPhone(in.nextLong());

    System.out.println("Number of test : ");
     undergrad[i].setTests(in.nextInt());
    tests=undergrad[i].getTests();

    for(int j=0; j<tests; j++){
        System.out.println("Test "+(j+1)+":");
        undergrad[i].setScores(in.nextInt(), j);
    }

    System.out.println("Internship: ");
    undergrad[i].setInternship(in.nextBoolean());

    in.nextLine();
    System.out.println("Project title: ");
    undergrad[i].setProject_Title(in.nextLine());

    System.out.println("Project Area: ");
    undergrad[i].setProject_Area(in.nextLine());

    System.out.println("Supervisor Grade: ");
    undergrad[i].setGrade(in.nextInt(), 0);

    System.out.println("Committee Grade: ");
    undergrad[i].setGrade(in.nextInt(), 1);

    System.out.println("\n");

  break;  
}

i need the break because the user should be asked about the action he want to do after entering details about one student (actions like\ writing details for another student or printing the details about the students written )


Solution

  • If you say that the break; at the end of your code is needed for the overall code to work, then clearly you are writing to the same array index (0) each time.

    The way to fix your program is to remove the for loop you currently have and instead keep track of the index i in the outside loop, the one which offers the user a menu of choices. Then, each time you go to the code which fills a new student, increment your i variable.

    Keep in mind that, in real code, you would never hit this issue because you would be using a List<Student> which automatically takes care about these concerns.