8 Replies - 1324 Views - Last Post: 14 October 2012 - 07:51 AM Rate Topic: -----

#1 andreajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 12

Question about using loops and if statements to solve

Posted 11 October 2012 - 06:04 PM

I need help to understand how to finish this code. It is a password checker. However it doesn't even verify that the length is correct. I could use some help with where I'm going or what I'm doing wrong. It doesn't give me any errors it simply just doesn't perform the task.
      
      public static void main(String[] args) {  
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a safe password: ");
      String s = input.next();

private static boolean safe(String s) {    
        
        int counter = 0;

        boolean digitFound = false;
        boolean lowerCaseFound = false;
        boolean upperCaseFound = false; 
        boolean spaceChar = false;
        
        if (s.length() < 8) {
            return false;
            } 
        while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isDigit(c)) {
                digitFound = true;
            }
            counter++;
        }
    
            counter = 0;
         while(counter < s.length()) {
            
            char c = s.charAt(counter);
            
            if (Character.isLowerCase(c)) {
                lowerCaseFound = true;
            }
            counter++;
         }          
         
         counter = 0; 
         while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isUpperCase(c)) {
                upperCaseFound = true;
            }
            counter++;
        }  
          
         counter = 0;
         while(counter < s.length()) {
            
            char c = s.charAt(counter);          
            if (Character.isSpaceChar(c)) {
                spaceChar = true;
            }
            counter++;
            return true;
         }       
         return false;
      }
    }


This post has been edited by baavgai: 12 October 2012 - 06:03 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: Question about using loops and if statements to solve

#2 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 06:05 PM

First thing is first please put your code in code tags.
Was This Post Helpful? 0
  • +
  • -

#3 andreajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 12

Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 06:16 PM

I'm really not sure what that means because I don't do forums. I apologize if this is wrong.


public static void main(String[] args) { 
Scanner input = new Scanner(System.in);
System.out.print("Enter a safe password: ");
String s = input.next();
/**
 * Utility method that tells if a given string is a safe password.
 * @param s the string to be tested
 * @return true if s is safe and false if not
 */
      
private static boolean safe(String s) { 

int counter = 0;
       /**
        * This is declaring digitFound. It is a boolean data type
        * and I am assigning it to false because  it has to assume its false 
        * for it to check it to be true.
        */
boolean digitFound = false;
boolean lowerCaseFound = false;
boolean upperCaseFound = false; 
boolean spaceChar = false;

if (s.length() < 8) {
return false;
} 
while(counter < s.length()) {

char c = s.charAt(counter); 
if (Character.isDigit©) {
digitFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);

if (Character.isLowerCase©) {
lowerCaseFound = true;
}
counter++;
} 

counter = 0; 
while(counter < s.length()) {

char c = s.charAt(counter); 
if (Character.isUpperCase©) {
upperCaseFound = true;
}
counter++;
} 

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter); 
if (Character.isSpaceChar©) {
spaceChar = true;
}
counter++;
return true;
} 
return false;
}
}

Was This Post Helpful? 0
  • +
  • -

#4 andreajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 12

Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 06:37 PM

Instead of return true I think it has to return as safe and this has to be in the main method? I got this far but now I'm just lost. I could type in "hi" and it tells me it works. I need it to tell me this password isn't acceptable. Which I thought where I had, length < 8 return false, would do this but it hadn't.
Was This Post Helpful? 0
  • +
  • -

#5 Xente  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 31-January 12

Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 07:16 PM

Indent your code...it helps a lot, especially in times like these where you're trying to find a bug in the program. The problem is with the logic, if a string is less than eight characters long it returns false, but the last return statement returns false as well. The return true statement is only in the while loop. At least that's what I can see.

Also, a better return statement would be

return (digitFound && lowerCaseFound && upperCaseFound && isSpaceChar);


If all the booleans are true, it will return true.
Was This Post Helpful? 0
  • +
  • -

#6 andreajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 12

Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 08:01 PM

Okay here is what I have I fixed it up some. Though it's still not working. I'm truly a beginner so understanding some of the logic behind java is difficult.

package passwordchecker;

import java.util.Scanner;


/**
 * 
 * @author Andrea 
 */
public class PasswordChecker {

    public static void main(String[] args) {  
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a safe password: ");
      String s = input.next();
      
    }             
/**
 * Utility method that tells if a given string is a safe password.
 * @param s the string to be tested
 * @return true if s is safe and false if not
 */
      
    private static boolean safe(String s) {    
        
        int counter = 0;
        
        /**
        * This is declaring digitFound. It is a boolean data type
        * and I am assigning it to false because  it has to assume its false 
        * for it to check it to be true.
        */
        boolean digitFound = false;
        boolean lowerCaseFound = false;
        boolean upperCaseFound = false; 
        boolean spaceCharFound = false;
        
        if (s.length() < 8) {
            return false;
            
            } 
        while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isDigit(c)) {
                digitFound = true;
            }
            counter++;
        }
    
            counter = 0;
         while(counter < s.length()) {
            
            char c = s.charAt(counter);
            
            if (Character.isLowerCase(c)) {
                lowerCaseFound = true;
            }
            counter++;
         }          
         
         counter = 0; 
         while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isUpperCase(c)) {
                upperCaseFound = true;
            }
            counter++;
        }  
          
         counter = 0;
         while(counter < s.length()) {
            
            char c = s.charAt(counter);          
            if (Character.isSpaceChar(c)) {
                spaceCharFound = true;
            }
            counter++;
            System.out.print("Password accepted.");
            return (digitFound && lowerCaseFound 
                    && upperCaseFound && spaceCharFound);
            
         }       
           System.out.print("Password invaild. Try again:" + input);
    }   
}  

Was This Post Helpful? 0
  • +
  • -

#7 Xente  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 31-January 12

Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 10:01 PM

Did you try compiling the code? When I compile it I get errors...
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Question about using loops and if statements to solve

Posted 12 October 2012 - 05:56 AM

You never invoked your safe() method from your main()
Was This Post Helpful? 1
  • +
  • -

#9 andreajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-October 12

Re: Question about using loops and if statements to solve

Posted 14 October 2012 - 07:51 AM

I've been working on this and still can't get it to work. Any suggestions or ideas about something I should look to add or edit? Thank you

package passwordchecker;

import java.util.Scanner;


/**
 * 
 * @author Andrea Dietrich
 * @version Oct 12, 2012
 */
public class PasswordChecker {

    public static void main(String[] args) {  
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a safe password: ");
      String s = input.next();
      
    }             
/**
 * Utility method that tells if a given string is a safe password.
 * @param s the string to be tested
 * @return true if s is safe and false if not
 */
      

    private static boolean safe(String s) {           
        int counter = 0;
        
        if (s.length() < 8) {
                return false;               
        }
        while (!safe(s)) {
            return false;
        }
     
        boolean digitFound = false;
        boolean lowerCaseFound = false;
        boolean upperCaseFound = false; 
        boolean spaceCharFound = false;
       
           
        while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isDigit(c)) {
                digitFound = true;
            }
            counter++;
        }
    
            counter = 0;
         while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isLowerCase(c)) {
                lowerCaseFound = true;
            }
            counter++;
         }          
         
         counter = 0; 
         while(counter < s.length()) {
            
            char c = s.charAt(counter);           
            if (Character.isUpperCase(c)) {
                upperCaseFound = true;
            }
            counter++;
        }  
          
         counter = 0;
         while(counter < s.length()) {
            
            char c = s.charAt(counter);          
            if (Character.isSpaceChar(c)) {
                spaceCharFound = true;
            }
            counter++;
            System.out.print("Password accepted.");
            
         }       
          
       return false;
    }   
}   

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1