Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Problem with my simple pw verifier

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with my simple pw verifier

    Hello,

    I am working on a password verifier that checks the characters to meet a certain criteria. The user enters in the number of passwords they need to check and then prompts them to enter each one. My for loop counter for each password is off. I am also having trouble with the word/# "null" and "0" appearing in the println when I run the program.

    I need the user to be able to put in a password, then be prompted with a black space for the next password they need to enter. My code for the loop is as follows:
    public void inputPassword () {
    for (int i = 0; i < (passwordCount); i++) {
    System.out.println(" Please enter each password: " + password);
    password = keyboard.nextLine;

    }
    }
    I know that the result of this is just adding the previous password onto the next line like "cat" then the next password will be "cat" + the next password entered and so on, im not sure how to correct this and get the loop to just take in a new password each time.


    Then, like I said above when typing out a simple println like:

    System.out.println( "Please enter your name:" + name);

    the output is placing the word "null" right after the colon.

    I apologize if this wasn't written in the correct format, but thank you for any assistance, my professor is very indirect with her "help".


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    result of this is just adding the previous password onto the next line
    Can you copy the contents of the console window from when you execute the program to show what you are talking about? I don't see where the code does what you are talking about.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    Please follow along with the program to verify your passwords.
    I am using a Scanner for the program.

    Please enter your name: nulls ("null" appears for some reason)
    Please enther the number of passwords you need to check: 013 (0 appears for no reason)
    Please enter each password: nulldogI12 ("null appears for the first pw entry)
    Please enter each password: dogI12cat123
    Please enter each password: cat123cowT687
    Please enter each password: cowT687

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    ("null" appears for some reason)
    That would be the value of the variable if it has not been assigned a value.

    Why do you print out the value of password at the end of the message?


    There is other code that is setting and changing the variables. You will have to post all of the code to show all the places where the variables and data are used.

    Please be sure to wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    import java.util.Scanner;
       import java.io.*;
     
       public class PasswordChecker {
     
          private String password; 
          private String testPlanAuthor; 
          private String reportString;
          private int passwordCount, totalPasswords;
          private boolean lengthOK, lowercaseOK, uppercaseOK, digitOK;
          private final int MINIMUM_LENGTH = 6;
          PrintWriter outputFile;
          Scanner keyboard = new Scanner(System.in);
     
     
          public static void main (String[] args) throws IOException{
     
          // call methods
             PasswordChecker newInput = new PasswordChecker();
             newInput.startPasswordChecker();
             newInput.inputPassword();
             newInput.checkLength();
             newInput.checkLowerCase();
             newInput.checkUpperCase();
             newInput.reportValidity();
             newInput.printReport();
     
          } // End Main
     
       // start pwchecker
     
     
     
          public void startPasswordChecker() throws IOException {
     
     
             System.out.println("Welcome! " + " This is the Password Verification System." +
                							"\n Please follow along with the program to verify your passwords. ");
     
             System.out.print("Please enter your name: "+ (testPlanAuthor));
             testPlanAuthor = keyboard.nextLine();
     
     
             System.out.print("Please enther the number of passwords you need to check: "+ (passwordCount));
             passwordCount = keyboard.nextInt();
             keyboard.nextLine(); 
     
             outputFile = new PrintWriter("Passwordlist.txt");
          }
     
           // inputpassword
     
          public void inputPassword () {
     
     
             for (int i = 0; i < (passwordCount); i++) {
     
                System.out.print(" Please enter each password: " + (password));
                password = keyboard.nextLine();        
     
     
             }
     
          }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    The posted code does not compile. It has many errors that need to be fixed before the code can be tested.

    Did you post all of the code?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    I did not post all of the code, i thought the problem might exist within that first half of the program since that was where I declared and initialized my variables.

    --- Update ---

    import java.util.Scanner;
       import java.io.*;
     
       public class PasswordChecker {
     
          private String password; 
          private String testPlanAuthor; 
          private String reportString;
          private int passwordCount, totalPasswords;
          private boolean lengthOK, lowercaseOK, uppercaseOK, digitOK;
          private final int MINIMUM_LENGTH = 6;
          PrintWriter outputFile;
          Scanner keyboard = new Scanner(System.in);
     
     
          public static void main (String[] args) throws IOException{
     
          // call methods
             PasswordChecker newInput = new PasswordChecker();
             newInput.startPasswordChecker();
             newInput.inputPassword();
             newInput.checkLength();
             newInput.checkLowerCase();
             newInput.checkUpperCase();
             newInput.reportValidity();
             newInput.printReport();
     
          } // End Main
     
       // start pwchecker
     
     
     
          public void startPasswordChecker() throws IOException {
     
     
             System.out.println("Welcome! " + " This is the Password Verification System." +
                							"\n Please follow along with the program to verify your passwords. ");
     
             System.out.print("Please enter your name: "+ (testPlanAuthor));
             testPlanAuthor = keyboard.nextLine();
     
     
             System.out.print("Please enther the number of passwords you need to check: "+ (passwordCount));
             passwordCount = keyboard.nextInt();
             keyboard.nextLine(); 
     
             outputFile = new PrintWriter("Passwordlist.txt");
          }
     
           // inputpassword
     
          public void inputPassword () {
     
     
             for (int i = 0; i < (passwordCount); i++) {
     
                System.out.print(" Please enter each password: " + (password));
                password += keyboard.nextLine();        
     
     
             }
     
          }
     
       // checklength
          public void checkLength () {
     
             lengthOK = false;
     
             if (password.length() >= MINIMUM_LENGTH) {	
     
                lengthOK = false;
     
             }
          }
       // checkLowerCase
          public void checkLowerCase() {
     
             lowercaseOK = false;
     
             int x = 0;
     
             while (x < password.length()){
     
                if (Character.isLowerCase(password.charAt(x))) {
     
                   lowercaseOK= true;
     
                   x++;         
                }
             }
          }
     
       // CheckUpperCase
          public void checkUpperCase() {
             int x = 0;	
             uppercaseOK = false;
     
             while ( x < password.length()) {
     
                if (Character.isUpperCase(password.charAt(x))) {
     
                   uppercaseOK = true;
                   x++;         
                }
             }
          }  
       // checkDigit
          public void checkDigit () {
     
             int x = 0;
             digitOK = false;
     
             while ( x < password.length()) {
     
                if (Character.isDigit(password.charAt(x))) {
     
                   digitOK = true;
     
                   x++;         
                }
             }
          }
       // reportValidity
          public void reportValidity () throws IOException {
     
     
             if (digitOK && lowercaseOK && uppercaseOK && lengthOK) {
     
                System.out.print(" The password is valid."); }
     
             if (lengthOK != true) {
                System.out.print(" The password is invalid. Its length must be at least 6 characters."); }
             if ( lowercaseOK != true) {
                System.out.print(" The password is invalid. There must be at least 1 lower case letter."); } 
             if (uppercaseOK != true) {
                System.out.print(" The password is invalid. There must be at least 1 upper case letter."); }
             if (digitOK != true) {
                System.out.print(" The password is invalid. There must be at least 1 digit."); }
     
          }
     
       // print Report
     
          public void printReport () throws IOException {
     
             if (digitOK && lowercaseOK && uppercaseOK && lengthOK) {
     
                outputFile.print("The password is valid."); }
     
             if (lengthOK != true) {
                outputFile.print(" The password is invalid. Its length must be at least 6 characters."); }	
             if ( lowercaseOK != true) {
                outputFile.print(" The password is invalid. There must be at least 1 lower case letter."); } 
             if (uppercaseOK != true) {
                outputFile.print(" The password is invalid. There must be at least 1 upper case letter."); }
             if (digitOK != true) {
                outputFile.print(" The password is invalid. There must be at least 1 digit."); }
     
             outputFile.close();
          } // end printreport  
     
       } // End PWChecker

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    Ok got it.

    The places where null is printed is because the String variable does not have a value.
    Assign them an empty String using =""

    Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

    For example if you enter: A word to the wise <PRESS ENTER>
    and use next() only "A" is read, and "word to the wise" is left in the buffer.
    Your next attempt to get something from Scanner will be to get "word".
    nextInt() will fail here.
    To clear the buffer, use the nextLine() method.
    To test if the next token is an int, use the hasNextInt() method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    Still fighting with it.
    Im not sure I know where to use the nextLine() method to clear the buffer. Right after the nextInt? I fixed the strings with "" Im not sure I understand what I need to add in order to get the loop to stop printing the previous password.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    where to use the nextLine() method to clear the buffer. Right after the nextInt?
    Yes.
    The code in post#7 has that.

    stop printing the previous password.
    Did you write this code:
                password += keyboard.nextLine();
    It concatenates the new input onto the old contents????
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    no I just did password = keyboard.nextLine(); and it still concatenates

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    Post the new code. Somewhere it is being concatenated.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    I dont want it to just add the previous pw im still just struggling with getting it to print out a new blank space in the scanner for the next password inside the for loop.

    this portion below

    public void inputPassword () {
     
     
             for (int i = 0; i < (passwordCount); i++) {
     
                System.out.print("Please enter each password: " + (password));
                password = keyboard.nextLine();        
     
     
             }
     
          }

    this loop is causing the string to just append the previous one, it is replacing the password before it.
    so pw #1 = cow
    then pw2= cow+my next password lets say "dog"
    then pw 3 = dog + the next password

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    Please copy the full contents of the console window from when the program executes and shows the problem. When OPs makeup what they think is important they often leave out important data. A full copy shows everything clearer and correctly.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

     
       import java.util.Scanner;
       import java.io.*;
     
       public class PasswordChecker {
     
          private String password = " "; 
          private String testPlanAuthor = " "; 
          private String reportString;
          private int passwordCount; 
          private int totalPasswords;
          private boolean lengthOK, lowercaseOK, uppercaseOK, digitOK;
          private final int MINIMUM_LENGTH = 6;
          PrintWriter outputFile;
          Scanner keyboard = new Scanner(System.in);
     
     
          public static void main (String[] args) throws IOException{
     
          // call methods
             PasswordChecker newInput = new PasswordChecker();
             newInput.startPasswordChecker();
             newInput.inputPassword();
             newInput.checkLength();
             newInput.checkLowerCase();
             newInput.checkUpperCase();
             newInput.reportValidity();
             newInput.printReport();
     
          } // End Main
     
       // start pwchecker
     
     
     
          public void startPasswordChecker() throws IOException {
     
     
             System.out.println("Welcome! " + "This is the Password Verification System." +
                							     "\nPlease follow along with the program to verify your passwords. ");
     
             System.out.print("Please enter your name: "+ (testPlanAuthor));
             testPlanAuthor = keyboard.nextLine();
     
     
             System.out.print("Please enther the number of passwords you need to check: "+ (passwordCount));
             passwordCount = keyboard.nextInt();
             keyboard.nextLine();
     
     
          }
     
           // inputpassword
     
          public void inputPassword () {
     
     
             for (int i = 0; i < (passwordCount); i++) {
     
                System.out.print("Please enter each password: " + (password));
                password = keyboard.nextLine();        
     
     
             }
     
          }
     
       // checklength
          public void checkLength () {
     
             lengthOK = false;
     
             if (password.length() >= MINIMUM_LENGTH) {	
     
                lengthOK = false;
     
             }
          }
       // checkLowerCase
          public void checkLowerCase() {
     
             lowercaseOK = false;
     
             int x = 0;
     
             while (x < password.length()){
     
                if (Character.isLowerCase(password.charAt(x))) {
     
                   lowercaseOK= true;
     
                   x++;         
                }
             }
          }
     
       // CheckUpperCase
          public void checkUpperCase() {
             int x = 0;	
             uppercaseOK = false;
     
             while ( x < password.length()) {
     
                if (Character.isUpperCase(password.charAt(x))) {
     
                   uppercaseOK = true;
                   x++;         
                }
             }
          }  
       // checkDigit
          public void checkDigit () {
     
             int x = 0;
             digitOK = false;
     
             while ( x < password.length()) {
     
                if (Character.isDigit(password.charAt(x))) {
     
                   digitOK = true;
     
                   x++;         
                }
             }
          }
       // reportValidity
          public void reportValidity () throws IOException {
     
     
             if (digitOK && lowercaseOK && uppercaseOK && lengthOK) {
     
                System.out.print(" The password is valid."); }
     
             if (lengthOK != true) {
                System.out.print(" The password is invalid. Its length must be at least 6 characters."); }
             if ( lowercaseOK != true) {
                System.out.print(" The password is invalid. There must be at least 1 lower case letter."); } 
             if (uppercaseOK != true) {
                System.out.print(" The password is invalid. There must be at least 1 upper case letter."); }
             if (digitOK != true) {
                System.out.print(" The password is invalid. There must be at least 1 digit."); }
     
          }
     
       // print Report
     
          public void printReport () throws IOException {
     
             totalPasswords = passwordCount;
             outputFile = new PrintWriter("Passwordlist.txt");
     
             if (digitOK && lowercaseOK && uppercaseOK && lengthOK) {
     
                outputFile.print("The password is valid."); }
     
             if (lengthOK != true) {
                outputFile.print(" The password is invalid. Its length must be at least 6 characters."); }	
             if ( lowercaseOK != true) {
                outputFile.print(" The password is invalid. There must be at least 1 lower case letter."); } 
             if (uppercaseOK != true) {
                outputFile.print(" The password is invalid. There must be at least 1 upper case letter."); }
             if (digitOK != true) {
                outputFile.print(" The password is invalid. There must be at least 1 digit."); }
     
             outputFile.close();
          } // end printreport  
     
       } // End PWChecker

    The output data looks like this:
    Please follow along with the program to verify your passwords.
    Please enter your name: Scot
    Please enther the number of passwords you need to check: 05
    Please enter each password: goaT321
    Please enter each password: goaT321abc123
    Please enter each password: abc123plant1231
    Please enter each password: plant1231abc21321
    Please enter each password: abc21321122132kdsd

    the loop is also going on continuously

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    Is This code that you are talking about:
     System.out.print("Please enter each password: " + (password));
    It prints the last password on the line and allows you to enter the next password immediately after it.

    causing the string to just append the previous one
    The program is not appending the password. The user that is entering the passwords sees the last password printed on the console and types the new password immediately following the last password.

    the loop is also going on continuously
    Which loop?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my simple pw verifier

    nvm im sorry...I see it now

    just figuring out how to get the loop to stop, it doesn't move on to the method Im using to print the passwords and whether they were valid or not. I just have to keep reading and figure out the code, thank you so much.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Problem with my simple pw verifier

    The loops in the check methods should be for loops not while loops.
    Its too easy to write infinite while loops
    Its hard to write infinite for loops
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. please help, simple problem
    By grandmst in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2011, 05:26 PM
  2. simple arithmetic problem
    By nadrii in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 26th, 2011, 09:35 PM
  3. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 06:19 AM
  4. probably a simple noob problem
    By Unaffiliated in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 22nd, 2011, 11:25 PM
  5. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 11:02 PM