Difference between revisions of "FillArray Method"

From CompSciWiki
Jump to: navigation, search
Line 49: Line 49:
 
}
 
}
 
</pre>
 
</pre>
}}
 

Revision as of 19:55, 5 April 2010

{{1010PrAD|ProblemName=fillArray Method

|Problem= Description of problem


|SideSection=


|Solution=Description of solution

|SolutionCode=
import java.io.*;

class Students{
	public static void main (String [] args){

		String [] comp1010 = fillArray(10);
		printArray(comp1010);


		System.out.println("--End of Processing--");
	}

	public static void printArray(String [] courseList){	//note that the method is void, and the input variable is an array of Strings
								//We called the array "courseList", since this can refer to any of the arrays
								//we send it

		for (int i=0; i<courseList.length;i++){		//for loop to go through each element in the array
			System.out.println(courseList[i]);	//print the current element
		}
		System.out.println();				//add a blank line after the list of students
	}

	public static String[] fillArray(int size){
	        String[] names = new String[size];                                         //an Array to hold the entered names
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  //a BufferedReader to accept user input of names
		
                for (int i=0;i<names.length;i++){
			System.out.print("Please enter a name: ");                         //prompt the user for a name
			try{
				names[i] = br.readLine();
			}
			catch (IOException ioe){
				System.out.println("ERROR!!!");
				System.exit(1);
			}
		}
		return names;
	}
}