Part 1: User input through command-line

Write a Java program that asks the user to enter the following information, piece by piece, through command-line interface:

  • First Name
    • Store it as a String
  • Middle Name
    • First, ask the user whether they have a middle name
    • If they do, ask them to enter it and store it as a String
  • Last Name
    • Store it as a String
  • Age
    • Store it as an int (number of years)
  • Height
    • Ask for feet and inches separately
    • Store both of those in int variables
  • Weight
    • Store it as a double (pounds)
  • Eye Color
    • Store it as a String
  • Does the user have siblings?
    • Ask a "yes" or "no" question
    • Store it as a boolean variable (true if yes, false if not)

The program should check each of the values and ensure they are correct (it should ask the user to re-enter them if an invalid value is entered). For example, if the user enters a negative value as their age or weight, we know for sure that it is wrong. Same applies if the user enters something else than "yes" or "no" for the last question.

You can assume that the input will not contain incorrect type of data (i.e. for numbers, the input will be only numbers; for strings, the input will only be strings etc.)

The output of the program should follow this format:

Full Name: _______________
Age: _____
Height: _____ (meters)
Weight: _____ (kilograms)
Eye Color: _______
Has Siblings: _____ (yes/no)

Part 2: User input through JOptionPane

Re-write the same program, but ask the user for input using JOptionPane pop-up dialogs instead of doing it through command-line interface. The program should output the information the same way as before.

Hints and resources

  • Before writing the code, think about the design of your program. Give yourself at least 10-15 minutes to consider what functionality of the language you will use. Write down which classes, functions, and control structures you are going to utilize. If you don't know how they work, take a look at the Java documentation or search for examples of usage online.
  • Start by writing and testing small individual portions of the program. This way you will have much less trouble putting the whole thing together later. In general, design your proof-of-concept snippets first, and then start putting them together to form a larger portion of code.
  • Make sure you utilize appropriate functionality from the Scanner class when retrieving the user input through command-line interface. When reading the documentation, pay attention to what data type Scanner's various next methods return and utilize them appropriately. For example:
    • next() reads a token (one word) and returns it as a String
    • nextInt() reads an integer and returns it as an int
    • nextLine() reads whole line (could be multiple words), returns String
    • etc.
  • JOptionPane does not have a function for retrieving numerical input. So for the information like age, height, and weight, you will need to retrieve it as String and convert it to appropriate data type. These two functions will help you:
    • Integer.parseInt(String s)
    • Double.parseDouble(String s) If you are unsure how to use them, you can always look it up online (search "how to use Integer.parseInt" etc.).
  • For "yes" or "no" questions, figure out a way to ensure that uppercase or lowercase letters in "yes"/"no" don't make a difference. The user should be able to enter "YES", "Yes", "no", "No" without an error. In this case, the relevant functionality you may be interested in is String's method called toLowerCase().
  • Remember that any object comparison in Java should be done using the equals() method (as opposed to the == operator)
  • Don't forget to use import statements at the beginning of your program to be able to use the Scanner and JOptionPanefunctionality.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.