1. Create a method named getNames. This method should declare an array of five f
ID: 3696386 • Letter: 1
Question
1. Create a method named getNames. This method should declare an array of five first names as strings. In the method, prompt the user for each name as a string. Each string should be a first name of at least five characters (e.g. "Charles") and the first character must be received as an uppercase letter. Keep in mind a first name may be more than one word (e.g. "Mary Lou"). If a data validation isn't passed, throw an error of the Exception class. Handle the exception in a method named handleError. This method should accept a string containing a suitable message about the type of error that occurred (e.g. "The name wasn't at least five characters."). The handleError method simply prints the string it received as an argument. Keep prompting the user, if necessary, until all data validations are passed.
2. Now that data validations have been enforced, pass the array of five first names to a method named searchNames. This method does not return a value. It should prompt the user for which first name they wish to display in the array as an integer (e.g. "Enter the position number of the name you wish to see: "). Use a try…catch block to prompt the user and obtain the integer. Display the matching name at that array index position as a string. Handle three potential errors via the catch block. The first should take care of a non-integer index entry. The second should take care of an index entry that is out of bounds for the array size. The third should be of an Exception type. Each raised exception should include a suitable custom message for that exception type along with the result of its .getMessage() string.
5. Call each of the methods as necessary above from the main method. The pseudocode for the main
method will be as given below.
For exercises 1 and 2:
Get the names and ensure data validations are passed. Return an array of strings.
Search for a name within the array based on an integer position.
Explanation / Answer
import java.util.Scanner;
public class WordGuess {
static void handleError(String s){
System.out.println(s);
}
static String[] getNames(){
String names[] = new String[5];
Scanner in = new Scanner(System.in);
for(int i = 0; i < 5; i++){
System.out.print("Enter name " + (i + 1) + ": ");
names[i] = in.nextLine();
in.next();
if(names[i].length() < 5 || (names[i].charAt(0) <= 'A' || names[i].charAt(i) > 'Z')){
try {
throw new Exception();
} catch (Exception e){
i--;
handleError("Error");
}
}
}
return names;
}
static void searchNames(String[] names){
Scanner in = new Scanner(System.in);
System.out.print("Enter the position number of the name you wish to see: ");
try{
int index = Integer.parseInt(in.next());
System.out.println("Name: " + names[index]);
}
catch(NumberFormatException e){
System.out.println("Input not an integer");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Index invalid");
}
catch(Exception e){
System.out.println("Invalid input");
}
}
public static void main(String args[]){
String[] arrString = getNames();
searchNames(arrString);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.