How do I check chars in a string for predetermined criteria?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poijoy
    New Member
    • Apr 2010
    • 5

    How do I check chars in a string for predetermined criteria?

    The program is supposed to accept a new password, but it needs to be at least 6 chars long and contain at least one number and one letter. The requirements do not say anything about special chars, so I assume they are not allowed. Unfortunately, the syntax seems sound, but the methods return an error: "illegal start of expression". public and static are pointed out. Otherwise, the rest of the code seems fine, so far. Any ideas? I'm new to Java, so I'm practicing with char and string manipulation and searching.

    I am using jGrasp to compile. Here is my code so far. I am not too concerned about someone "stealing" my code as it's a project from a book, so I'm sending the whole thing. The error returned is "PasswordDahl.j ava:78: cannot find symbol
    symbol : class string
    location: class PasswordDahl
    public static void TestLength(stri ng password)
    ^
    1 error"

    Code:
    import javax.swing.*;
    public class PasswordDahl{
    	public static void main(String[] args){
    		String password = JOptionPane.showInputDialog(null, "Enter a password between " +
    		"6 and 10 characters long using at least one number and one letter.");
    		TestLength(password);
    	}
    	public static void TestDigits(String password){
    		int i = 0;
    		char p;
    		boolean good;
    		while(i <= password.length()){
    			if(Character.isLetterOrDigit(i)){
    				good = true;
    				if(Character.isDigit(i)){
    					good = true;
    				}
    				else if(Character.isLetter(i)){
    					good = true;
    					i = password.length();
    				}
    				else{
    					good = false;
    					JOptionPane.showInputDialog(null, "Be sure to include "
    						+ "\nat least one number and one letter. \nPlease try again.");
    				}
    					++i;
    			}
    		}	
    	}
    	public static void TestLength(string password){
    		boolean good;
    		if(password.length() < (6) || password.length() > (10)){
    			JOptionPane.showInputDialog(null, "Your password is of insufficient length."
    				+ "\nIt must be between 6 and 10 characters long.\nPlease try again.");
    		}
    		else{
    			good = true;
    			TestDigits(password);
    		}
    	}
    }
    Last edited by poijoy; Aug 14 '10, 01:52 AM. Reason: Forgot information...
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    In line number 31
    Code:
    public static void TestLength(string password){
    Here you mentioned string not String (Not case of first letter).
    String datatype starts with uppercase letter. not lower case.

    So it must be

    Code:
    public static void TestLength(String password){
    Regards
    Dheeraj Joshi

    Comment

    • poijoy
      New Member
      • Apr 2010
      • 5

      #3
      I am so sorry for not getting back to you. Immediately after posting my question, my computers all died, so I was running around...again. ..figuring out a way to do my schoolwork, so I didn't get Bytes.com checked for my answer. Thank you very much. I really did have troubles with Java, so hopefully I'll be able to do it better next time I need to!

      PoiJoy

      Comment

      Working...