Note that there are some explanatory texts on larger screens.

plurals
  1. POFirst, last and sometime middle name detection Java
    primarykey
    data
    text
    <p>I am trying to write a code to give off the user's name in different formats after they enter it. However, if a user does not have a middle name, the system should print that there was an error. I have it so it works perfectly if the user enters three names but does not work if the user enters two names. Here is my code:</p> <pre><code>import java.util.Scanner; public class Assignment3 { public static void main(String[] args) { String fullName; Scanner in = new Scanner(System.in); System.out.print ("What are your first, middle, and last names? "); fullName = in.nextLine(); System.out.println(fullName); if (fullName.contains(" ")) { String[] nameParts = fullName.split(" "); String firstInitial = nameParts[0].substring(0,1).toUpperCase(); String secondInitial = nameParts[1].substring(0,1).toUpperCase(); String thirdInitial = nameParts[2].substring(0,1).toUpperCase(); if (nameParts[2].isEmpty()) { System.out.println("No Middle Name Detected"); } else { System.out.println ("Your initials are: " + firstInitial + secondInitial + thirdInitial); String lastVariationOne = nameParts[2].substring(0, nameParts[2].length()); lastVariationOne = lastVariationOne.toUpperCase(); String firstVariationOne = nameParts[0].substring(0, nameParts[0].length()); firstVariationOne = firstVariationOne.substring(0,1).toUpperCase() + firstVariationOne.substring(1, nameParts[0].length()); System.out.println("Variation One: " + lastVariationOne + ", " + firstVariationOne + " " + secondInitial + "."); String lastVariationTwo = nameParts[2].substring(0, nameParts[2].length()); lastVariationTwo = lastVariationTwo.substring(0,1).toUpperCase() + lastVariationTwo.substring(1, nameParts[2].length()); System.out.println("Variation Two: " + lastVariationTwo + ", " + firstVariationOne); } } else { System.out.println("Wrong. Please enter your name properly."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload