Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with If the user hits Cancel on either first or last name, show the error

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with If the user hits Cancel on either first or last name, show the error

    i know its a if and loop but not sure what so far i have this

    //CIS 226 Assignment 2
    //The GUI - Assign2
    // - 02/02/2011

    import javax.swing.JOptionPane;


    public class Assign2 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String name = JOptionPane.showInputDialog("Please enter your first name:");
    String lastname = JOptionPane.showInputDialog("Please enter your last name");
    JOptionPane.showMessageDialog(null, "Hello, " + name +(" ") +lastname+ ", \n Welcome to CIS 226." );

    //For extra credit

    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);



    }

    }


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Need help with If the user hits Cancel on either first or last name, show the err

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String name = JOptionPane.showInputDialog("Please enter your first name:");
    String lastname = JOptionPane.showInputDialog("Please enter your last name");
    JOptionPane.showMessageDialog(null, "Hello, " + name +(" ") +lastname+ ", \n Welcome to CIS 226." );

    //For extra credit

    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);

    }
    when user will click on cancel button null is assigned into the variable.name or lastname


    if(name.equals(null) || lastname.equals(null))
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);



    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String name = JOptionPane.showInputDialog("Please enter your first name:");
    String lastname = JOptionPane.showInputDialog("Please enter your last name");

    if(name.equals(null) || lastname.equals(null))
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);

    else
    JOptionPane.showMessageDialog(null, "Hello, " + name +(" ") +lastname+ ", \n Welcome to CIS 226." );

    //For extra credit
    }
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with If the user hits Cancel on either first or last name, show the err

    thanks i saw the name = null in my book but i think i was missing the null in my script. but thanks again

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with If the user hits Cancel on either first or last name, show the err

    i have it like this but when i hit cancel on both name and last name i still get the null message " hello welcome, null null, to cis226. whats wrong?

    //CIS 226 Assignment 2
    //The GUI - Assign2
    // - 02/02/2011

    import javax.swing.JOptionPane;


    public class Assign2 {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String name = JOptionPane.showInputDialog("Please enter your first name:");
    String lastname = JOptionPane.showInputDialog("Please enter your last name");
    JOptionPane.showMessageDialog(null, "Hello, " + name +(" ") +lastname+ ", \n Welcome to CIS 226." );

    //For extra credit

    if(name.equals(null) || lastname.equals(null))
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);
    else
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);

    }

    }

  5. #5
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Need help with If the user hits Cancel on either first or last name, show the err

    //For extra credit

    if(name.equals(null) || lastname.equals(null))
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);
    else
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);

    }

    you haven't seen the code i had given earlier carefully, welcome message should be in else part.

    if(name.equals(null) || lastname.equals(null))
    JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226.", "Error ", JOptionPane.ERROR_MESSAGE);

    else
    JOptionPane.showMessageDialog(null, "Hello, " + name +(" ") +lastname+ ", \n Welcome to CIS 226." );

    And please mark this thread solved , if your problem is solved
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Show files name in JFrame
    By altisw5 in forum AWT / Java Swing
    Replies: 15
    Last Post: November 30th, 2011, 08:30 AM
  2. Run Or Cancel Java Thread !
    By byrodi in forum The Cafe
    Replies: 11
    Last Post: November 3rd, 2010, 09:04 AM
  3. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  4. Show Text With cursor
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2010, 10:02 AM
  5. How do i show all the values in one window(JOptionPane)??
    By Antonioj1015 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2009, 09:24 PM