Respuesta :
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.

Answer:
1)
if (fullName.indexOf(", ") != -1){
lastName = fullName.substring(0, fullName.indexOf(", "));
firstName = fullName.substring(fullName.indexOf(", ") + 2, fullName.length());}
else{
firstName = fullName.substring(0, fullName.indexOf(" "));
lastName = fullName.substring(fullName.indexOf(" ") + 1, fullName.length());}
Explanation:
fullName is a string that contains the full name.
This string is to be split into two sub strings containing last and first name. indexOf() function is used which returns the position of the specified character in this function like here the specified character is comma followed by a space (, ) or single space( ).
+2 means that it will skip the comma/single space from the fullName string to store the string in the lastName.
length() function here is used to return the number of characters in the string fullName.
!= -1 here checks if the comma or space are present in the fullName string.
Another way to extract first and last name from full name is:
int index = fullName.indexOf(",");
if (index >= 0){
lastName = fullName.substring(0, index).trim();
firstName = fullName.substring(index+1).trim();}
else
{index = fullName.indexOf(" ");
firstName = fullName.substring(0, index).trim();
lastName = fullName.substring(index+1).trim();}
trim() function here removes the spaces.
The fullName can be assigned a name like this:
String fullName="lastname, firstname";
To display sub strings:
System.out.println(lastName);
System.out.print(firstName);
2)
String userstring= ""; // store the string
int land = 0;
int air = 0;
int water = 0;
while(!(userstring.equals("xxxxx"))) { // loop terminates when "xxxxx" is read
userstring = stdin.next(); // reads the string of characters
if(userstring.equals("land")) {
/* equals functions compares the entered string with land specified in this function */
land= land + 1;
}else if(userstring.equals("air")) {
air= air + 1;
}else if(userstring.equals("water")) {
water = water + 1;
} }
//displays land air and water on a separate line
System.out.println("land:" + land);
System.out.println("air:" + air);
System.out.println("water:" + water);