3 Replies - 3775 Views - Last Post: 15 October 2012 - 03:39 PM Rate Topic: -----

#1 squaregate   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 15-October 12

Trouble understanding method implementation/use

Posted 15 October 2012 - 02:38 PM

I'm writing an application that will take a user-inputted string and check that it is eight characters long and has at least one digit, lower case character, upper case character, and special character. The part that checks for those things is supposed to be implemented as a method that the main method uses. I haven't been able to successfully use the "safe" method and am feeling kind of lost when it comes to classes and methods. I'm confused as to whether using
while (safe2(JOptionPane.showInputDialog = false) {etc}
is on the right track for solving this or if I'm way off. Any advice is appreciated and I would especially appreciate if anyone could point me to whatever they find to be the best explanation of classes and methods for someone new.

/**
 *
 * @author -------------
 */
public class PWChecker {

public static void main(String args[]) {
    safe safe2 = new safe();
}
    
private static boolean safe(String s) {
        
        int length = s.length();
        int count = 0;
        
        
        boolean hasEightChar = false;
        boolean hasDigit = false;
        boolean hasLowerCase = false;
        boolean hasUpperCase = false;
        boolean hasSpecial = false;
        
        // checks each character of the inputted password
        while (count <= length - 1) {
            char c = s.charAt(count);
            
            
            if (length >= 8) {
                hasEightChar = true;
            }
            
            
            if (Character.isDigit(s.charAt(count))) {
                hasDigit = true;
            }
            
            
            if (Character.isLowerCase(s.charAt(count))) {
                hasLowerCase = true;
            }
            
            
            if (Character.isUpperCase(s.charAt(count))) {
                hasUpperCase = true;
            }
            
            
            if (!Character.isLetterOrDigit(s.charAt(count))) {
                hasSpecial = true;
            }
                
            count++;
        }
        
        if (hasEightChar && hasDigit && hasLowerCase && hasUpperCase &&
                hasSpecial) {
            return true;
        }
        else {
            return false;
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble understanding method implementation/use

#2 NathanMullenax   User is offline

  • D.I.C Head
  • member icon

Reputation: 103
  • View blog
  • Posts: 218
  • Joined: 23-September 12

Re: Trouble understanding method implementation/use

Posted 15 October 2012 - 03:00 PM

Your password checking function is a static method. You don't need an instance of PWChecker to use it. You can just call it like this:
boolean passwordIsGood = safe("abcdE4$__");



If you had omitted the static keyword, you would first have to create an instance of the PWChecker object, and then invoke the method.
PWChecker pwc = new PWChecker();
boolean passwordIsGood = pwc.safe("abcdE4$__");



As far as a good reference on classes and methods in Java, you could probably find something in the tutorial section of this site.

This one talks about what static means:
http://www.dreaminco...ing-classes-ii/
Was This Post Helpful? 2
  • +
  • -

#3 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Trouble understanding method implementation/use

Posted 15 October 2012 - 03:07 PM

In regards to learning classes and methods, I really advise you go through the Oracle tutorials as they provide quite a nice explanation Here Make sure you go through all the pages though to get a good understanding. If you check out the java tutorials section on here aswell, there should be plenty of beginner tutorials on this topic which should help you.

In regards to your code, you are getting slightly confused, and doing a lot more work than you need too. To start of with, you need to get the input from the user, so I would use a scanner for this as it provides you with some useful methods from the api. In your main method at the moment, you have that line of code, but that line of code is what you would use to instantiate a class. As you only need to call the method, you would only need
boolean isPasswordGood = safe(myString);



I would really advise going through the tutorials first on classes and methods, as you really need to understand these before you proceed. You need to try and write this yourself in order to better your learning, and use us as a guide. As I say though, try to understand classes and methods first before you proceed, it will make things a lot easier to understand.

On a side note, one of the experts here will probably provide you with a good explanation aswell, so try to learn from this if they do.

This post has been edited by nick2price: 15 October 2012 - 03:08 PM

Was This Post Helpful? 2
  • +
  • -

#4 squaregate   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 15-October 12

Re: Trouble understanding method implementation/use

Posted 15 October 2012 - 03:39 PM

Got it working. I'll check out the Oracle guide more, I was skimming over it earlier today and it seems like clearest explanation out of all of the ones I've seen.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1