/**
 * @(#)password.java
 *
 *
 * @author 
 * @version 1.00 2012/7/14
 */
import javax.swing.JOptionPane;
import javax.swing.*;
import java.util.*;
public class password {


    public static void main (String[] args) 
        {


 String user;
 user=JOptionPane.showInputDialog("enter password to continue(letter only)");


 if(user.compareTo("adnan"))
    {   
        JOptionPane.showMessageDialog(null,"ACCES GRANTED","welcome",
                                        JOptionPane.INFORMATION_MESSAGE);
    }
 else
 {
 JOptionPane.showMessageDialog(null,"ACCES DENIED","error",
                                        JOptionPane.ERROR_MESSAGE);

 }




   }
}

Recommended Answers

All 2 Replies

first of all, you don't actually ask a question, but, having taken a glimpse at your code, I can figure out what your problem is:

if(user.compareTo("adnan"))

something tells me this is not what you want to do. compareTo is actually used to sort
elements. the above gives you an error, because in the if structure, you'll need a boolean expression, something returning either true or false.

the compareTo method, however, doesn't. it returns a primitive int.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

for instance, the compareTo method in a class User that has one String as instance variable, the name:

String name; 
....
@Override
public int compareTo(User test){
  return this.name.compareTo(test.getName());  
}

above you have three options: either the two names are equal, then the result of the method will be 0.
the name in test can be (alphabetically) smaller then the one in the current instance, the method will return a result that is -1 or less (so, this would be the case for: this.name = "a"; and test.name = "b";
third option: the reverse, the value in test is alphabetically smaller than the one in the current instance, that way, the result will be 1 or higher.

so, what you need to do there, is:

if ( user.compareTo("adnan") == 0 ){
...
}

but, if you just want to check for equality, not whether it might be bigger or smaller, compareTo is not what you want to do. There is a method for that purpose, it doesn't check the order of the elements, it just verifies equality of the value of two objects. this is the equals method. For the String class, of which you are comparing two instances, there is even a special one: equalsIgnoreCase

This is easiest explained with a simple example:

String one = "adnan";
String two = "adnan";
String three = "Adnan";

if ( one.equals(two) ){
  System.out.println("first is true");
}
else{
  System.out.println("first is false");
}

if ( one.equals(three) ){
  System.out.println("second is true");
}
else{
  System.out.println("second is false");
}

if ( one.equalsIgnoreCase(three) ){
  System.out.println("third is true");
}
else{
  System.out.println("third is false");
}

the above will print:

first is true
second is false
third is true

thank you very much stultuske you are the best

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.