Results 1 to 13 of 13
  1. #1
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default Trouble with For loop and variables in a program

    Hi,

    Im relatively new to java, and as part of a course that i am on, need to write an application to validate a password entered by a user.

    A while ago, i had my program at least semi working, but now i have done something, and it doesnt, lol.

    I was wondering if anyone could identify any problems with my for loop, and subsequent code?

    String getUsername;
    String getPassword;
    int getLength;
    char c;

    char a;


    System.out.println("Please enter your username...");
    getUsername = EasyIn.getString();
    System.out.println("Welcome " + getUsername + "!");
    System.out.println("Please enter your password for validation...");
    getPassword = EasyIn.getString();
    getLength = getPassword.length();
    System.out.println("Password length = " + getLength);

    System.out.println("Password is fine");
    // loops over string and reports back numbers of letters

    for (int i = 0; i < getLength; i++)
    {
    c=getPassword.charAt(i);
    System.out.println("Letter " + i);
    System.out.println(" = " + c);
    boolean start = true;

    do {

    // Checks for space
    boolean b_space = Character.isWhitespace(c);
    if (b_space == true)
    do {
    System.out.println("You have a space in your password, please remove it and try again");
    //start = false;
    }
    while(b_space == true);
    if (b_space == false)
    System.out.println("Password has no spaces");


    The easyIn class is something that i was provided with to get input from users. The main problem seems to be that the loop prints the "password has no spaces" bit for as many times at the length of the password. The loop is mainly supposed to count the letters in the password, so, if a password was fish, letter 0 would be f, letter 1 would be i and so on.

    if someone could tell me where im going wrong, i would very much appreciate it, im going mad!

    thanks alot

    Sam

  2. #2
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    I''m not sure without looking at the easyIn class, but is Character defined somewhere.

    Java Code:
    boolean b_space = Character.isWhitespace(c);
    if not that's where your problem is.

  3. #3
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks for your reply!

    I did look at that, and you're right, it isnt defined, anywhere! However, it doesnt complain about the fact that it isnt defined. i always thought character was built in to the java language, or Eclipse.

    Do you know how i would define it, and what as?

    TIA!

  4. #4
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    hmm, After closely looking at your code you can use Character like you did. Give me a few minute and I should get back to you.

    Ok, your problem is you are using to many loops for your purpose just use the main for..loop. Below is my fix for you.

    Java Code:
     for (int i = 0; i < getLength; i++)
                    {
                            c=getPassword.charAt(i);
                            System.out.println("Letter " + i);
                            System.out.println(" = " + c);
                            boolean start = true;
    
    
    
                            // Checks for space
                            boolean b_space = Character.isWhitespace(c);
                            if (b_space == true)
                                    System.out.println("You have a space in your password, please remove it and try again");
                           
                    }
    System.out.println("Password has no spaces");
    You can play with it and make the output they way you like. For example my code above will continue to print even if there is a whitespace.
    Last edited by theonly; 05-06-2008 at 12:59 AM.

  5. #5
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks loads for that mate, it got rid of the loop problems

    However, i now have new problems.

    if i run the program, i get this...
    Please enter your username...
    sam
    Welcome sam!
    Please enter your password for validation...
    s am
    Password length = 4
    Password is fine
    You have a space in your password, please remove it and try again
    Password has no spaces


    As you can see, it adds the last message to the end, even though there is a space. I thought i might solve this by just removing the second message and having one big message at the end.

    I was wondering, how would i put the rest of my checking statements in? E.g, ive tried to insert a statement after your lines so it will check if there are capitals in the password, and it doesnt work at all.

    Do you know how i would add additional statements?

    Thanks for your help

    Sam

  6. #6
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    // Checks for space
    boolean b_space = Character.isWhitespace(c);
    if (b_space == true)
    System.out.println("You have a space in your password, please remove it and try again");

    boolean b_ucase = Character.isUpperCase(c);
    if (b_ucase == false)
    System.out.println("You have no uppercase letters");

    boolean b_lcase = Character.isLowerCase(c);
    if (b_lcase == false)
    System.out.println("You have no lower letters");

    boolean b_digit = Character.isDigit(c);
    if (b_digit == false)
    System.out.println("You have no digits");



    }


    I have tried using this, but it doesnt really work for the digit part. I think my program is essentialy doing what it is supposed to: checking each char for the type - but my problem is that it is reporting on every one.

    e.g

    Please enter your username...
    sam
    Welcome sam!
    Please enter your password for validation...
    TTYG676
    Password length = 7
    = T
    You have no lower letters
    You have no digits
    = T
    You have no lower letters
    You have no digits
    = Y
    You have no lower letters
    You have no digits
    = G
    You have no lower letters
    You have no digits
    = 6
    You have no uppercase letters
    You have no lower letters
    = 7
    You have no uppercase letters
    You have no lower letters
    = 6
    You have no uppercase letters
    You have no lower letters


    (the = signs are purely there for making sure that the letters are at least picked up)

  7. #7
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    Ok, so are you require to print out every letter in the password or just to check everyone without printing them.

  8. #8
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Just a hint put your code in code braces there is a button in the advanced reply.
    My IP address is 127.0.0.1

  9. #9
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    @ theonly

    i dont need to print the letters, just to check if the entered password conforms to the requirements.

    That does mean checking every letter, but as you can see from my last post, its not quite doing it correctly. Do you have any ideas?

    @ Zosden

    Noted, thanks mate! :)

  10. #10
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    Why yes I do have an idea :)
    You can use a few nested if-statements to check each character, and if java encounter an error output an error message and then have java shut down the program.

  11. #11
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    I have tried doing nested IF statements before, but i dont think that gets rid of the fact that for every letter it is still printing the statements every time - an example of this is in post 6, in bold.

    Could you possibly elaborate? I do apologize for taking up so much time, java isnt my strong point! :/

  12. #12
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    Ok, what are your conditions that correspond with correct input. I.E. everything should be lowerCase, uppercase, etc..

  13. #13
    dablyz is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    WEll, first i need to take the password that was entered and check that password for

    * No spaces -isWhiteSpace
    * At least one number -isDigit
    * At least one uppercase letter -isUpperCase
    * At least one lowercase number -isLowerCase

    The problem im having at the moment is that the loop is reporting the error statement (i.e there is a space in the password) over every letter in the password

    Thanks

Similar Threads

  1. compiling trouble
    By capacitator in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-10-2008, 10:12 PM
  2. Replies: 2
    Last Post: 05-05-2008, 01:56 PM
  3. Compile Trouble
    By adelgado0723 in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 02:02 AM
  4. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 09:29 AM
  5. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 07-31-2007, 11:12 PM

Posting Permissions

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