Results 1 to 6 of 6
  1. #1
    Imperator is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Need help tweeking some code

    Hello, I'm really awful at java and I need help with this.

    while (valid && index < 0)
    {
    if (!Character.isDigit(password.charAt(index)))
    valid = false;
    index++;
    }

    I was wondering if it would be possible to make this statement return true if there is at least one or more digits in the array

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Need help tweeking some code

    Well, first your condition is incorrect. CharAt() takes a positive value, which means index must be >= to zero. Which means your while loop will never be entered because index < 0 will immediately be false (or if index < 0 your CharAt() method will throw an index out of bounds exception).

    And since this is not a method, it won't return anything. I suggest you replace your while loop with a for loop. It is better suited for this type of test.
    Use String.length() as the limit of the for loop.

    Regards,
    Jim
    Last edited by jim829; 02-19-2014 at 12:52 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Imperator is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need help tweeking some code

    import java.util.Scanner;

    public class password2 {
    public static void main(String[] args)
    {

    Scanner keyboard = new Scanner(System.in);

    String password;


    System.out.println("Please enter a password that contains six characters, at least one uppercase, one lowercase and one digit");
    password = keyboard.nextLine();

    if (isValid(password))
    {
    System.out.println("This password is valid");
    }
    else
    {
    System.out.println("This password is invalid");

    }
    }

    private static boolean isValid(String password)
    {
    boolean valid = true;
    int index = 0;


    if (password.length() != 6)
    valid = false;


    while (valid && index >= 1)
    {
    if (!Character.isLetter(password.charAt(index)))
    valid = false;
    index++;
    }


    while (valid && index >= 1)
    {
    if (!Character.isDigit(password.charAt(index)))
    valid = false;
    index++;
    }

    while (valid && index >= 1)
    {
    if (!Character.isLowerCase(password.charAt(index)))
    valid = false;
    index++;
    }

    while (valid && index >= 1)
    {
    if (!Character.isUpperCase(password.charAt(index)))
    valid = false;
    index++;
    }


    // Return the results
    return valid;
    }
    }


    this is the whole thing that I'm working on. I do not understand how I can make it so that it doesn't go "out of bounds" It goes out of bounds when I enter 6 letters but it doesn't when I enter 6 digits, It's very strange (

    also wondering how I can make it so that it will return true so long as there is at least one or more digits, uppercases, or lowercases.
    Last edited by Imperator; 02-19-2014 at 01:46 AM.

  4. #4
    Imperator is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need help tweeking some code

    Thanks, I will try that. )

  5. #5
    Imperator is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need help tweeking some code

    like this?

    for (valid && index >= password.length())
    Last edited by Imperator; 02-19-2014 at 01:41 AM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Need help tweeking some code

    No. Like this:

    Java Code:
    for (int j = 0; j < password.length(); j++) {
      // rest of code here.
    }
    Since you are not certain how to use for loops you should check out the Oracle tutorials in my signature.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Requesting help on tweeking a JCheckBox JList
    By benjamin.b in forum New To Java
    Replies: 4
    Last Post: 06-07-2011, 02:45 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •