Answer:
// Scanner class is defined to allow the program receive user input
import java.util.Scanner;
// The class name Solution is defined
public class Solution {
// object stdin of Scanner is declared static and private
// it is static
// so that it can be called from questionTwo method
private static Scanner stdin = new Scanner(System.in);
// main method to begin program execution
public static void main(String args[]) {
// prompt the user to enter full name
System.out.println("Enter the fullname: ");
// the received name is saved to userInput
String userInput = stdin.nextLine();
// empty string for firstName
String firstName = "";
// empty string for lastName
String lastName = "";
// if the userInput contain comma
// then it is splitted with comma
// else if it doesn't contain comma
// it is splitted with space
if(userInput.contains(",")){
String[] array = userInput.split(",");
// lastName is the name before the comma
lastName = array[0];
// firstName is the name after the comma
firstName = array[1];
} else{
String[] array = userInput.split(" ");
// firstName is the name before the space
firstName = array[0];
// lastName is the name after the space
lastName = array[1];
}
// firstName and last is printed
System.out.println("FirstName: " + firstName + "\nLastName: " + lastName);
// beginning of second part of question
// method for questionTwo is called
questionTwo();
}
// solution to questionTwo
public static void questionTwo(){
// the userword is declared as empty string
String userword = "";
// landCounter is set to 0
int landCounter = 0;
// airCounter is set to 0
int airCounter = 0;
// waterCounter is set to 0
int waterCounter = 0;
// while to continue receiving user input
// as long as it is not "xxxxx"
while(true) {
// The user is prompt to enter a word
System.out.println("Enter your word again: ");
// the received word is saved to userword
userword = stdin.nextLine();
/* Block of if-statement to increment
counter based on the input value of userword
if userword is "xxxxx", the loop terminate
*/
if(userword.equals("land")){
landCounter += 1;
}
if(userword.equals("air")){
airCounter += 1;
}
if(userword.equals("water")){
waterCounter += 1;
}
if (userword.equals("xxxxx")){
break;
}
}
// Display the number of land, air and water in three lines
System.out.println("land: " + landCounter + "\nair: " + airCounter + "\nwater: " + waterCounter);
}
}
Explanation:
The code is well commented. There is an attach image of when the code is executed.