Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do i make it Not Equal to?

    THANKX FOR the replies!!
    BUt i need more favor to ask.
    now, I want it to output "Wrong ID" If only the password matches "open" and output "Wrong Password" If only the user ID matches "admin."
    How do i do it?

    =========================================================

    import javax.swing.JOptionPane;
    import java.lang.*;

    public class Logpass
    {
    public static void main(String [ ] args)
    {
    char ID;
    char password;

    String input1 = JOptionPane.showInputDialog(null, "Please enter your ID.");
    String input2 = JOptionPane.showInputDialog(null, "Please enter youe password.");

    if( input1.equals( "admin" ) && input2.equals( "open" ))
    {
    JOptionPane.showMessageDialog(null, "Welcome");
    }
    else if( )
    {
    JOptionPane.showMessageDialog(null, "Wrong ID");
    }
    else if( )
    {
    JOptionPane.showMessageDialog(null, "Wrong password");
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Wrong ID and Password");
    }
    }
    }

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if(input1 != "admin")
    {
    JOptionPane.showMessageDialog(null, "Incorrect ID");
    }
    else if(input2 != "open")
    {
    JOptionPane.showMessageDialog(null, "Wrong password");
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Welcome");
    }
    It's not very scaleable, but it does the job

  • #3
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    You can use the following character "!" which will reverse a value from true to false and vice versa. for example the following statement

    if( !input1.equals( "admin" ) && !input2.equals( "open" )) {

    }

    This means if input1 not equal to admin and input2 not equal to open

    Cheers,
    Ess

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Quote Originally Posted by KeZZeR View Post
    It's not very scaleable, but it does the job
    Quote Originally Posted by ess View Post
    You can use the following character "!" which will reverse a value from true to false and vice versa. for example the following statement

    if( !input1.equals( "admin" ) && !input2.equals( "open" )) {

    }

    This means if input1 not equal to admin and input2 not equal to open

    Cheers,
    Ess
    I just wanted to quickly point out the difference between Kezzer's code and ess' code (or is it ess's?)

    In Kezzer's code we saw the != used with Strings and while this works in occasion it can provide some unpredictable behavior.

    Since Java is essentially a fully pointers language, the == != sometimes does things unexpected to the Java beginner. Let me give an example.

    With Kezzer's code,

    Code:
    if(input1 != "admin")
    It looks like input1 is checking if it's the same as the String "admin" but this is actually incorrect. What's really going on is it's checking to see if the Pointer input1 is pointing to the same location as the temporary pointer "admin".

    Which sometimes is true, and sometimes is false, depending on where it is allocated in memory.

    So for a much safer and always true method you want to check the actual String inside of the pointer, use ess' code. It calls upon a helper method (of equals to which you will learn more about in your continuation of Java) to check that the Strings are the same, not that the pointers correspond.

    Anyways, I wanted to clarify this just for knowledge reference.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah yes, I remember. It typically works most of the time though from what I can remember although my memory is pretty terrible (I was in a coma recently after-all ).

    Yeh, it's better to use the members for that particular object when it comes to operations like these.

    Just use !myString.equals("whatever"); instead as pointed out.


  •  

    Posting Permissions

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