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 6 of 6

Thread: Logical error and having trouble HELP!

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Logical error and having trouble HELP!

    wassup! ok i am new to this forum and would like to ask someing i am having trouble with my end of the PAT mark ( practical assessment task) ok so we need to make a password checker that allows the user to enter a password and then the program checks if it meets certain criteria in this case the criteria is as follows : Must have between 5-15 characters it must include both letters and numbers and cannot contain vowels this is my code so far ( a bit messy i know but work with me here)

    public class Password {
    public static void main(String[] args) {
    String pass;
    String pass1 = "T";
    String pass2 = "T";
    String pass3 = "F";
    pass = JOptionPane.showInputDialog("Please input a password that meets the following criteria :"
    + "\n1. Must include both letters and numbers"
    + "\n2. Must be between 5-15 characters"
    + "\n3. Must contain no vowels");
    int length = (pass.length());

    if (length < 4 ||length > 20)
    {
    pass1 = "T";
    }


    if ("T".equals(pass1))
    {
    for (int i = 0; i <= length; i++)
    {
    if (pass.charAt(i) == 'a' ||pass.charAt(i) == 'A'||pass.charAt(i) == 'e'||pass.charAt(i) == 'E' ||
    pass.charAt(i) == 'i' || pass.charAt(i) == 'I' || pass.charAt(i) == 'o' || pass.charAt(i) == 'O'||pass.charAt(i) == 'u'||
    pass.charAt(i) == 'U')
    {
    pass2 = "F";
    }
    }
    }
    if ("T".equals(pass1))
    {

    }
    if ("F".equals(pass2));
    {
    JOptionPane.showMessageDialog(null, "Sorry your password does not comply with the criteria");
    }



    if ("F".equals(pass1))
    {

    }

    }
    ok and this is the highly frustrating error i a recieving :

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(String.java:687)
    at DeathStrike.Password.main(Password.java:33)
    Java Result: 1

    please help guys!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Logical error and having trouble HELP!

    When posting code, don't forget the highlight tags.

    Why are you using "T" and "F" instead of booleans?

    How many indexes does your String have? What character is at each index? What index are you trying to access?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Logical error and having trouble HELP!

    my ide changed it automatically please give me a code suggestions

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Logical error and having trouble HELP!

    Quote Originally Posted by DeathStrike View Post
    my ide changed it automatically please give me a code suggestions
    That's not really how this works. I've asked you a few questions that you seem to have ignored. Please see the link in my signature on asking questions the smart way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logical error and having trouble HELP!

    Like a said im only in grade 10 i dont know most of the stuff you were asking about, but without your help i managed to create the code perfectly.

    public class Password {
    public static void main(String[] args) {
    String pass;
    String pass1 = "T";
    String pass2 = "T";
    String pass3 = "F";
    String pass4 = "F";
    pass = JOptionPane.showInputDialog("Please input a password that meets the following criteria :"
    + "\n1. Must include both letters and numbers"
    + "\n2. Must be between 5-15 characters"
    + "\n3. Must contain no vowels");
    int length = (pass.length());

    if (length <= 4 ||length >= 21)
    {
    pass1 = "F";
    }
    if (pass1 == "T")
    {

    if (pass.contains("a") || pass.contains("A") || pass.contains("e") || pass.contains("E") || pass.contains("i") || pass.contains("I")
    || pass.contains("o") || pass.contains("O") || pass.contains("u") || pass.contains("U"))
    {
    pass2 = "F";
    }
    if (pass2 == "F")
    {
    JOptionPane.showMessageDialog(null, "Sorry! Your password contains vowels");
    }
    else
    {
    if (pass.contains("1") || pass.contains("2") || pass.contains("3") || pass.contains("4") || pass.contains("5") || pass.contains("6")
    || pass.contains("7") || pass.contains("8") || pass.contains("9") || pass.contains("0"))
    {
    pass3 = "T";
    }
    if (pass3 == "T")
    {
    if (pass.contains("b") || pass.contains("B") || pass.contains("c") || pass.contains("C") || pass.contains("d") || pass.contains("D")
    || pass.contains("f") || pass.contains("F") || pass.contains("g") || pass.contains("G")||pass.contains("h") || pass.contains("H") || pass.contains("j") || pass.contains("J") || pass.contains("k") || pass.contains("K")
    || pass.contains("l") || pass.contains("L") || pass.contains("m") || pass.contains("M") || pass.contains("n") || pass.contains("N") || pass.contains("p") || pass.contains("P") || pass.contains("q") || pass.contains("Q")
    || pass.contains("r") || pass.contains("R") || pass.contains("s") || pass.contains("S") || pass.contains("t") || pass.contains("T") || pass.contains("v") || pass.contains("V") || pass.contains("w") || pass.contains("W")
    || pass.contains("x") || pass.contains("X") || pass.contains("y") || pass.contains("Y") || pass.contains("z") || pass.contains("Z"))
    {
    pass4 = "T";
    }
    if (pass4 == "T")
    {
    JOptionPane.showMessageDialog(null, "Your password meets all the required fields! your password is: " + pass);
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Your password does not contain any consenants");
    }
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Your password contains no numbers");
    }
    }
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Sorry! Your password is either to long or to short");
    }



    }

    }
    Thanks for nothing

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Logical error and having trouble HELP!

    Do not compare strings using ==, use the equals method instead. Anyway you should use booleans (true/false) like Kevin mentioned. When you ask for help you should follow the advice instead of ignoring it, otherwise those trying to help will be less likely to help in future.
    Improving the world one idiot at a time!

Similar Threads

  1. I have a logical error
    By n00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 02:52 PM
  2. Sum of series program logical error
    By Neel0075 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 11:26 AM
  3. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  4. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 01:17 AM
  5. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 07:58 AM

Tags for this Thread