Results 1 to 4 of 4
  1. #1
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Problem With A Program That Checks For User's Name

    This program should react to the programmer's name (i.e. my name, Marco). If the user enters Marco it asks for a secret, depending on the secret length it reacts differently. This all works perfectly.

    However, if I enter a different name, Dario for example, it just ends whereas it should say Hi Dario, pleased to meet you. What have I done wrong?

    Java Code:
    // Program to read a user's name using JOptionPane, and react differently depending on if itsthe programmer's name or not
    //If its the programmer's name it then asks for a secret and reacts depending on the length of the secret All using JOptionPane
    
    import javax.swing.JOptionPane; //import JOptionPane
    public class MySecret //create class name
    { //open class
    	public static void main (String[] args) //create args
    		{ //open args
    			String programmerName="Marco"; //String with value Andrew
    			String userName=JOptionPane.showInputDialog(null, "Hello, what's your first name?"); //ask for user's first name
    				if (userName.equalsIgnoreCase(programmerName)) //if userName equals programmerName
    				{ //open if
    				String secret=JOptionPane.showInputDialog(null, "Hi " + userName + " tell me a secret."); //ask for user's first name
    				int length = secret.length();
    					if (length < 20) //If length of secret is lower than 20
    					{ //open if
    					JOptionPane.showMessageDialog(null, "That's not much of a secret."); //Print message
    					} //close if
    					else if (length >= 20) //If length of secret is equal to or more than 20
    					{ //open else
    					JOptionPane.showMessageDialog(null, "That's a good secret, I'll never tell."); //Print message
    					} //close else
    				else //else
    					{ //open else
    					JOptionPane.showMessageDialog(null, "Hello, " + userName + ", nice to meet you."); //Print message
    					} //close else
    				} //close if
    			System.exit(0); //program terminates safely
    		} //close args
    } //close class
    Sorry for the overwhelming amount of comments, this is how we're asked to submit.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Problem With A Program That Checks For User's Name

    What else would you expect to happen? If you take out all the extra stuff, your if statement is basically:

    Java Code:
    if (userName.equalsIgnoreCase("Marco")) 
    {
       if (length < 20)  {
          //do some stuff
       } 
       else if (length >= 20){
          //so some stuff
       } 
       else {
          JOptionPane.showMessageDialog(null, "Hello, " + userName + ", nice to meet you."); //Print message
       } 
    }
    //exit
    So you see, that else is never going to happen. I think you meant to put it outside the first if statement?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Problem With A Program That Checks For User's Name

    Thanks! In one comment you solved two problems in two different programs, resolving in one movement my whole assignment!

    If my questions seem simplistic or ridiculous, its because I'm at a very low level at this stage and have only been studying a few weeks.
    Last edited by Interista; 11-17-2011 at 04:00 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Problem With A Program That Checks For User's Name

    Quote Originally Posted by Interista View Post
    If my questions seem simplistic or ridiculous, its because I'm at a very low level at this stage and have only been studying a few weeks.
    No worries. You should see the kinds of questions I ask about tomcat and jsp, where I'm still a beginner.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. user input program
    By myalani in forum New To Java
    Replies: 4
    Last Post: 10-28-2011, 04:07 AM
  2. A String method that checks to see the data is numeric?
    By eLancaster in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 06:32 PM
  3. Replies: 8
    Last Post: 01-15-2011, 09:39 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:55 PM
  5. Replies: 5
    Last Post: 09-09-2008, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •