Write a method called getNameArray that takes an integer parameter (numNames), r
ID: 3824538 • Letter: W
Question
Write a method called getNameArray that takes an integer parameter (numNames), returns an array of Strings, and is declared to throw an Exception. If the parameter passed is a negative value throw an Exception. otherwise, loop numNames times prompting the user to enter anothe name each pass through the loop. Return this array of names. Write a method called writeNames that takes two parameters an array of Strings (nameArray) and a String (fileName), and returns nothing. Write the array of Strings (names) to a text file named using the fileName parameter. Make sure to catch any Exception that might be thrown. Write a method called readNames that takes a String parameter (fileName) and returns an ArrayList of Strings (nameArrayList). The method reads the text file (filename) and populates the ArrayList with an element for each line in the file. The method returns the populated ArrayList. Make sure to catch any Exception that might be thrown. Write a method called printNames that takes an ArrayList of Strings and prints the contents of the ArrayList to the command line, each on a separate line.Explanation / Answer
1)
String[] getNameArray(int numNames){
String input_string[] = new String[numNames];
Scanner scanner = new Scanner( System.in );
for(int i=0;i < numNames;i++){
System.out.print( "Enter a string: " );
input_string[i] = scanner.nextLine();
}
return input_string;
}
4)
void printNames(String array[]){
int len=array.length;
for(int i=0;i < len;i++){
System.out.println(array[i]);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.