#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Im having trouble using static methods to create and pass an array


    I am working on a program for an intro to Java class and I am supposed to use a static method to pass information from an array. All of the supporting classes written are working and I can get it to print without using static methods. I cannot however rewrite it to use static methods. It compiles and runs but just prints a blank line.

    Code:
    public class StudentTest1
    {
    	public static void main (String args[])
    	{
    		
    		int index = 0;	
    		
    		String dataArray[][] = {{"Undergraduate", "John", "Smith", "615 Bowen", "Arlington", "Texas", "76019", "3079"},
    					{"Graduate" ,"Jane", "Jones", "212 Davis", "Fort Worth", "Texas", "76134", "1095", "MSIS"},
    					{"Undergraduate", "Dean", "Rice", "1001 HWY 2", "Dalls", "Texas", "75010", "1381"},
    					{"Undergraduate", "Rachel", "Cole", "789 Crowley", "Arlington", "Texas", "76015", "8003"},
    					{"Graduate", "Brad", "Shelborne", "942 Baird", "Grand Prairie", "Texas", "75050", "9780", "PHD"}};
    	
    
    		
    		Student studentArray[] = new Student[5];
    
    		int hourArray [] = {12, 3, 9, 18, 6};
    			
    		createInstances(dataArray);
    
    		printInstances(studentArray);
    		
    
    
    	}
    
    		//createInstances
    		public static Student createInstances(String[][] dataArray)
    		{
    
    			int index = 0;	
    		
    			Student studentArray[] = new Student[5];	
    						
    			for (index = 0; index < studentArray.length; index ++)
    			{
    				if(dataArray[index][0].equals("Undergraduate"))
    					studentArray[index] = new Undergraduate(dataArray[index][1], dataArray[index][2], Integer.parseInt(dataArray[index][7]),
    						new Address(dataArray[index][3],dataArray[index][4], dataArray[index][5], Integer.parseInt(dataArray[index][6])), 
    						0, 295.00);
    
    				else if(dataArray[index][0].equals("Graduate"))
    					studentArray[index] = new Graduate(dataArray[index][1], dataArray[index][2], Integer.parseInt(dataArray[index][7]),
    						new Address(dataArray[index][3],dataArray[index][4], dataArray[index][5], Integer.parseInt(dataArray[index][6])), 
    						0, 125.00,  dataArray[index][8]);
    							
    			}	
    				index--;
    				return studentArray[index];
    			
    
    			
    		}
    		//calcTuition
    
    		
    		//PrintInstances		
    
    		public static void printInstances(Student[] studentArray)
    		{
    			String message = "";
    			int index;
    
    
    
    			for (index = 0; index < studentArray.length; index ++)
    			{
    
    				if (studentArray[index] != null)
    				message += studentArray[index].toString() + "\n";
    			}
    
    			System.out.println(message);
    		}
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Notice this line here.

    if (studentArray[index] != null)
    message += studentArray[index].toString() + "\n";
    In your main method, you create an array of Student objects. By default each entry in the array is null until you assign it to refer to an actual Student object.

IMN logo majestic logo threadwatch logo seochat tools logo