Hello fellow seniors, i can display all the data from array after i input all the data, but do not know how to display it on displayDonor() Any help is highly appreciate.

import javax.swing.JOptionPane;

public class DonorMenu{

	public static void main(String args[])
	{ 
		int choice = -1;
		do{
			choice = getChoice();
			if (choice != 0){
				getSelected(choice);
			}
		}
		while ( choice !=  0);
			System.exit(0);
	}

	public static int getChoice()
	{
		String choice;
		int ch;
		choice = JOptionPane.showInputDialog(null,
			"1. Enter 5 Blood Donor details\n"+
			"2. Find a lucky Donor\n"+
			"3. Display Donors\n"+
			"0. Exit\n\n"+
			"Enter your choice");
		ch = Integer.parseInt(choice);
		return ch;

	}
	
public static void getSelected(int choice){
		if(choice==1){
			insertDonor();
		}
		if(choice==2){
			findDonor();
		}
		if(choice==3){
			displayDonor();
		}
	}

public static void insertDonor()
	{
	 
   // create an array (of size 5) to store Donor objects
        Donor [] list = new Donor[5];
  
        
        for (int i=0; i<list.length; i++) {
           // input a Donor details
           String lastname=JOptionPane.showInputDialog(null, "Enter last name: ");           
           String firstname=JOptionPane.showInputDialog(null, "Enter first name: ");           
           String iCard=JOptionPane.showInputDialog(null, "Enter identity card: ");           
           String hPhone=JOptionPane.showInputDialog(null, "Enter hand phone: ");           
           String inpStr=JOptionPane.showInputDialog(null, "Enter gender(m/f): ");    
           char gender = inpStr.charAt(0);       
  
           // create a Donor object using the input values
           Donor d = new Donor(lastname,firstname,iCard,hPhone,gender);
           add(list, i, d);
        } display(list);
  
}

public static void findDonor()
	{
	String output = "The Lucky Donor is ";
	for(int i=0; i<1; i++){
	output += (int)(Math.random()*5);
	JOptionPane.showMessageDialog(null, output);
	}
  }

public static void displayDonor()
	{
	
  }
  
public static void add(Donor [] donors, int i, Donor d) { 
        // insert Donor object into array at position i
        donors[i]=d;
    }  
  
public static void display(Donor [] donors) {
        // display details of all Donor objects in array
       for(int i=0 ; i<donors.length ; i++){
       
       	String output = donors[i].getLastName();
       	output += donors[i].getFirstName();
       	output += donors[i].getIcard(); 
       	output += donors[i].getHphone(); 
       	output += donors[i].getgender();
       	JOptionPane.showMessageDialog(null, output);
       }
    }//End display
}//End Class DonorMenu

Instead of creating your Donor[] inside of the insertDonors() method, create the array at the top of your class and label it as static. This will make it a class variable which will mean any of the methods in your class can use it.

Instead of creating your Donor[] inside of the insertDonors() method, create the array at the top of your class and label it as static. This will make it a class variable which will mean any of the methods in your class can use it.

! Thanks a lot! Working on it now. ^.^

This article has been dead for over six months. Start a new discussion instead.