Hi all,
I wrote this program for constructing an ID out of someones first name, middle name, last name, and address. I got it to work and run perfectly except for one small thing. There is suppose to be the possibility for the person to not enter a middle name, but when i tried not entering a middle name when i ran it the last message dialog box didnt pop-up as it does if the middle name is entered. Is there something i can do so that if a middle name is not entered that it just ignores that it is not there? I was thinking of adding validation to make sure a first and last name is entered. Heres my code:

import javax.swing.*;
public class ConstructID
{
    public static void main(String [] args)
    {
      String firstName = "";
      String middleName = "";
      String lastName = "";
      String address = "";
        
        firstName = JOptionPane.showInputDialog(null,
            "Please enter your first name");
        middleName = JOptionPane.showInputDialog(null,
            "Please enter your middle name");
        lastName = JOptionPane.showInputDialog(null,
            "Please enter your last name");
        address = JOptionPane.showInputDialog(null,
            "Please enter your address");
        firstName = firstName.toUpperCase();
        middleName = middleName.toUpperCase();
        lastName = lastName.toUpperCase();
        
    JOptionPane.showMessageDialog(null, "Your ID is " +
        firstName.substring(0,1) + middleName.substring(0,1) +
        lastName.substring(0,1) + address.substring(0,4));
    }
}

Thanks much,
Sam

if a middle name is not entered that it just ignores that it is not there

Do you know how to use the if statement?
Build your output String (displayed on lines 23-25) in steps using concatenation controlled by if statements.

I do know how to use if statements. I tried using an if to say if middleName is null, or if (middleName.equals("")) then middle name is null. My assumption is that the program has issues because its trying to use substring on a string that doesnt exist. Im not really sure how i would go about building an output string using if statements. Can I add if statements in the lines 23-25? something like firstName.substring(0,1) + if (middleName.equals("")) middleName.substring(0,0) else middleName.substring(0,1) and so on.

Before going on, you need to learn how to use if statements. They are ONE of the primary statements you will use in programming.

There is a ternary operator (?) that you could use in place of an if. The syntax is:
conditional expression ? true results expression : false results expression

it returns the same as:

if(conditional expression)
  true results expression
else
 false results expression

I know how to use the if statement, i am aware in my last reply that my if example is not formatted as an if statement should, and am aware that if statements are used alot in programming. Im not sure how you would throw an if statement in the JOption code stuff lines as it has its own format.

Did you read this part of my previous post?
Build your output String in steps using concatenation controlled by if statements.
String output = "";
if(condition)
output += "more stuff"
if(another cond)
output += "more stuff"
if(another cond)
output += "more stuff"

Nope, dont see that in the previous post, i seen that you had to build my output string but i have never done that before or ran across that in my book yet. I will see what i can do with that. Thanks Norm

Sam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.