This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Dec 4, 2009 4:09 PM by 843789 RSS

String method problems in a programm

843789 Newbie
Currently Being Moderated
I have a program that I am having major issues with. The program reads from a file and checks to see if the "password" in the file follows a set amount of rules. I have the print out correct and to check if the password is long enough. My problem lies in the two methods to check if there are at least two digits(enoughDigits) and the method to check if there are only letters and numbers(validCharacter). I dont know how to fix the and if anyone has any insight, please help. I cant figure it out.

Main Method
import java.util.*;* 
*import java.io.*;
import java.lang.*;*

*public class MyProgram10 {*
*  public static void main(String[] args)throws Exception {*

*     Scanner scan = new Scanner(System.in);*
*       File infile = new File("data10.txt");*  
*     Scanner input = new Scanner(infile);*


*     String menu, outputString;*
*     int option = 1;*
*     int count = 0;*
*     String [][] array = new String [50][6];     *
*     ValidatePassword vp = new ValidatePassword();      * 
*     vp.readData(input, array);*

*      menu =  "\n\t1  Check length"  *
*+                     "\n\t2  Check characters"+ * 
*                    "\n\t3  Check digits" +               + *
*+                    "\n\t4  Print information"+ *
*                    "\n\t0  Quit\n\n\n";*

*     System.out.println(menu);*
*     System.out.print("\tEnter your selection:   ");*
*     option = scan.nextInt();*

*    while (option != 0) {*

*               switch (option) {*

*                    case 1: //length*
*                                   vp.longEnough(array);                                   *
*                                   break;*

*                    case 2: //characters*
*                                   vp.validCharacter(array, count);*
*                                   break;*

*                    case 3:  //digits*
*                                   vp.enoughDigits (array);*
*                                   break;*

*                    case 4:  //print*
*                                   vp.printData (array);*
*                                   break;*

*                    default: outputString = "\nInvalid Selection\n";*
*                                     System.out.println(outputString);*
*                                     break;*

*               }*

*               System.out.println(menu);*
*               System.out.print("\tEnter your selection:   ");*
*               option = scan.nextInt();*


*     }*

*     }*
*}
*

Methods
*
import java.util.*;
import java.io.*;*
*import java.lang.*;

public class ValidatePassword {
          int count = 0;


     //Default Constructor
     public ValidatePassword(){
     }



     //Method to read file
     public int readData (Scanner input, String [][] table) {


          while (input.hasNext()) {     

               table [count][0] = input.next();
               table [count][1] = input.next();
               table [count][2] = input.next();
               count ;
          }

          return (count);     
     }


     //Method to check length
     public void longEnough (String [][] table) {

          for (int r = 0; r < count; r++){
               String alter;
               alter = table[r][2];

                    if (alter.length() < 8)
                         table [r][3] = "Invalid";
                    else table[r][3] = "valid";     

               }

          }



     //Method to check if its only characters 
     public void validCharacter (String [][] table, int count) {
          char symbol;
          String find;

               for (int r = 0; r < count; r++) {
               find = table[r][2];

               for(int i = 0; i <find.length(); i++){
                         symbol =find.charAt(i);

               if (Character.isLetterOrDigit(symbol))
                              table [r][4] = "Invalid";
                    else table[r][4] = "valid";
                         }

                    }

     }          
          //Method to check the digits
     public void enoughDigits (String [][] table) {
          int symbol = 0;
          int num = 0;
          for (int r = 0; r < count; r++){
               String temp2;
               temp2 = table[r][2];

                    for(int i = 0; i <temp2.length(); i++){
                         symbol = temp2.charAt(i);


                         if (Character.isDigit(symbol)= 2)
                              table [r][4] = "Invalid";
                              else table[r][4] = "valid";
                         }

                    }
     }




     //Method to print the data
      public void printData (String [][] table) {

          System.out.println ("First Name" +"\tLast Name"+ "\tPassword" +"\t\tLength"+ "\t\tCharacters" +"\tDigits");+

+          for (int r = 0; r < count; r++) {

               for (int c = 0; c < table[r].length; c++)
                    System.out.print(table[r][c] + "\t\t");
                    System.out.println();

          }
     }


}
The file it reads from is what is below saved in data10.txt


Dzinyo Amekudzi     Atlanta
Leri Atwater Valdosta12
Rachel Bouknight 12Macon
Travis Dampier Quitman
Wayne Davis Tallahassee
James Farnsworth Tifton12345
Sharon Fulp Sava12nnah
Kevin Funderburk Mari45Eta
Richard Giuly JacksonVille567
William Hagan ThomasVille-12
Jason Hampton Orlando$456
Heimir Helgason RayCity567
Joseph Jira Perry45Abc
Mohsan Khan LakePark11
Jeff Ley Live555Oak
Ronesia Malloy Ray123
Michele Massey RayCity999
  • 1. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    How to fix what? Does your program compile? If not, what's the error message? If it does compile, does it run without exceptions? If not, what's the exception? If it does run without exceptions, does it work? If not, how does it diverge from correct behavior?
  • 2. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    I am trying to fix the compile errors in validCharacter and enoughDigits methods. If you remove these two methods it compiles and runs the menu fine. The errors are the following in enoughDigits method:

    ValidatePassword.java:79: unexpected type
    required: variable
    found : value
                             if (Character.isDigit(symbol)= 2)

    What i am not understanding is how to change the value to a variable and count it. I have tried many paths such as for loops and if statements and im just not getting it.
  • 3. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    tabarber84 wrote:
    I am trying to fix the compile errors in validCharacter and enoughDigits methods. If you remove these two methods it compiles and runs the menu fine. The errors are the following in enoughDigits method:

    ValidatePassword.java:79: unexpected type
    required: variable
    found : value
                             if (Character.isDigit(symbol)= 2)

    What i am not understanding is how to change the value to a variable and count it. I have tried many paths such as for loops and if statements and im just not getting it.
    Well, look at the line:
    if (Character.isDigit(symbol)= 2)
    Character.isDigit is a function call that returns a value (a boolean value). Then you're using "=", which is the assignment operator. So you're trying to assign a number to a value, which doesn't make sense. That's like doing this:
    5 =2;
    So that's wrong. Maybe you meant to use "==", the equality operator?

    But even that doesn't quite make sense, because isDigit returns a boolean, not a number, so it makes no sense to check to see if it's equal to 2.

    What are you trying to accomplish on that line?
  • 4. Re: String method problems in a programm
    795426 Newbie
    Currently Being Moderated
    Character.isDigit(aChar) returns a boolean signifying whether the character you passed in was, in fact, a digit. Use that test to count the number of digits in your string (this will require a loop). Once you know how many digits you have in total in your string, you'll know if it meets your validation requirements.
  • 5. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    if (Character.isDigit(symbol)= 2)
    The isDigit method returns a boolean, then you attempt to assign 2 to it. Doesn't compute.

    Maybe you mean to do a comparison. Then it should be == but then you are trying to compare a boolean to an integer. Doesn't compute.
  • 6. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    could you show me a generic example due to my book doesnt do so well
  • 7. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    What i was trying to accomplish was to check to see how many are digits and count them at the same time. by the "=2" i was trying to see if i could make the count equal 2 then it say its valid. Do i need to make an" int count" and just "count++" every time it hits a digit? If so, how would i go about that? Would i put a "count++" after the (character.isDigit) line?
  • 8. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    tabarber84 wrote:
    Do i need to make an" int count" and just "count++" every time it hits a digit?
    Sure. Or you can rely on magic.
  • 9. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    Someone stated another for loop. Are you suggesting after the two for loops to have the (Character.isDigit)line THEN a for loop? Im not trying to ask stupid questions, im just trying to learn and figure out my problem at the same time.
  • 10. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    tabarber84 wrote:
    What i was trying to accomplish was to check to see how many are digits and count them at the same time. by the "=2" i was trying to see if i could make the count equal 2 then it say its valid.
    So that was, what, test code?

    Why did you think that was valid syntax? You can't just make things up and hope they work.
    Do i need to make an" int count" and just "count++" every time it hits a digit? If so, how would i go about that? Would i put a "count++" after the (character.isDigit) line?
    It wouldn't make sense to declare a count variable as the result of the number being a digit.
    You'd declare count once, set it to zero, then loop through the string, checking each digit, and incrementing count when it hits a digit.
    Then when you're done with that loop, you'd know the number of digits in the string.

    By the way -- it looks like you have that 2d array to hold data you're working on, plus to hold results from the work. That's pretty ugly.
    If you want to hold a bunch of related info about a single item, then create a class to represent the info.
    Then you can have a 1d array of objects of that class. Much simpler.

    Also, please indent sanely. You're making your code harder to read by lining up unrelated things.
  • 11. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    So my problem now is that i can get the method to go through its cycle but it isnt checking all the passwords, just the first. Where am i going wrong? The re isn't any display errors, it just wont check the rest. Any suggestions?
    //Method to check the digits
         public void enoughDigits (String [][] table, int rowCt) {
              char symbol;
              int num = 0;
              int ct = 0;
              for (int r = 0; r < rowCt; r++){
                   String temp2;
                   temp2 = table[r][2];
         
                   for(int i = 0; i < temp2.length(); i++){
                        symbol = temp2.charAt(i);
                                                                          
                        if (Character.isDigit(symbol))
                             num++;
                        }//end for
                        
                             if (num < 2)                              
                                  table [r][5] = "Invalid";
                             else 
                                  table[r][5] = "-";     
                                                                                         
                   }//end for
         }//end enoughdigits
  • 12. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    Maybe rowCt is wrong. Maybe the table was never properly populated. Who knows. Visual inspection almost never works for debugging.

    Add debuging statements to see where things are going wrong. That's just a statement like this:
    System.err.println("starting method enoughDigits, rowCt is " + rowCt);
    Put those in to see what the inputs and outputs of various methods are, what values important variables have in your program, return values, etc.

    And seriously, get rid of that awful 2d string array.
  • 13. Re: String method problems in a programm
    843789 Newbie
    Currently Being Moderated
    Im sorry i cant do anything about the 2d array. Thats the way the teacher wants us to do it. My final problem is that my method "validCharacters" is running but im not getting int numb to reset after every time it checks a string. It stays at the number its at and set "invalid" to the rest of the strings. Any suggestions?
         public void validCharacter (String [][] table, int charCt) {
              char symbol;
              int numb = 0;
                   for (int r = 0; r < charCt; r++) {
                        String find = table[r][2];
                        
                        for(int i = 0; i < find.length(); i++){
                             symbol = find.charAt(i);
                                  
                             if (!Character.isLetterOrDigit(symbol))
                                  numb ++;
                             }//end for(int i = 0; i <find.length(); i++)
                                  
                                  if(numb != 0)
                                       table[r][4] = "Invalid";
                                  else
                                       table[r][4] = "-";
                   }
                   
         }          
  • 14. Re: String method problems in a programm
    699554 Newbie
    Currently Being Moderated
    tabarber84 wrote:
    Im sorry i cant do anything about the 2d array. Thats the way the teacher wants us to do it. My final problem is that my method "validCharacters" is running but im not getting int numb to reset after every time it checks a string. It stays at the number its at and set "invalid" to the rest of the strings. Any suggestions?
    You wrote the code didn't you?

    When is a String checked in your code? Set numb to 0 at that point.

    Mel
1 2 Previous Next