Question

I just started working on some code for a password creator. I was doing some testing and I keep g...

I just started working on some code for a password creator. I was doing some testing and I keep getting this error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
at java.base/java.lang.String.charAt(String.java:693)
at PasswordCreater.check(PasswordCreater.java:56)
at Password.main(Password.java:23

In my code I am having the user create a password that is at least 6 characters long, but no more than 16. Also, include at least one lowercase letter, one uppercase letter, one number digit and one special character of !,@,#,$,%,&.

Here is my main method (still not completely done)

import java.util.*;
public class Password
{
   public static void main(String [] args)
   {
       Scanner keyboard = new Scanner(System.in);
       PasswordCreator thing = new PasswordCreator ();
       String password = "";
       boolean dragon = true;
       while(dragon)
       {   System.out.print("Would like to create your own password(1) or have one generated for you(2).Press 1 or 2 to choose: ");
           int option = keyboard.nextInt();
           if(option == 1)
           {
               boolean wolfDog = true;
               while(wolfDog)
               {  
                   wolfDog = false;
                   System.out.print("Please create a password that is atleast 6 characters long, but no more than 16. " +"\n"+
                   "Also, include at least one lowercase letter, one uppercase letter, one number digt, and one special character of " + "\n"+
                   "!,@,#,$,%,&,*: ");
                   password = keyboard.nextLine();
                   thing.check(password);
                   if(thing.check(password) == false)
                   {
                       System.out.println("You entered an invalid password, try again.");
                       wolfDog = true;
                   }
               }
              
           }
           break;
       }
   }
}

Then here is my PasswordCreator class (also not done)

import java.util.*;
public class PasswordCreator
{
   private ArrayList upperCase;
   private ArrayList lowerCase;
   private ArrayList number;
   private ArrayList special;
   public PasswordCreater()
   {
       upperCase = new ArrayList<> (26);
       for(int i = 0; i < 26; i++)
       {
           upperCase.add(String.valueOf((char)(65 + i)));
       }


       lowerCase = new ArrayList <> (26);
       for(int i = 0; i < 26; i++)
       {
           lowerCase.add(String.valueOf((char)(65 + i)));
       }
      
       number = new ArrayList <> (9);
       for (int i = 0; i < 9; i++)
       {
           number.add(i);
       }
       special = new ArrayList <> (7);
       special.add("!");
       special.add("@");
       special.add("#");
       special.add("$");
       special.add("%");
       special.add("&");
       special.add("*'");
   }      
   //verfies if password is a minimun of 6 character but nomore than 16.
   //make sure password passes restricted rules
   public Boolean check(String password)
   {
       boolean goodOrNot = true; //flag
       int z = 0; //control variable
       if(password.length() < 6 || password.length() > 16)
       {
           goodOrNot = false;
       }
       while(goodOrNot && z > 1 || z < 13 )
       {
           int i =0;
           for(; i < password.length(); i++);
           {
               //Checking for uppercase requirements
               for(int j = 0; j < upperCase.size() && !found; j++)
               {
                   if(!special.contains(Character.isUpperCase(password.charAt(i))))
                   {
                       goodOrNot = false;
                       z++;
                   }
               }
               //checking for lowercase requirements
               for(int j = 0; j < lowerCase.size() && !found; j++)
               {
                   if(!special.contains(Character.isLowerCase(password.charAt(i))))
                   {
                       goodOrNot = false;
                       z++;
                   }
               }

               //checking for special character requirements.
               for(int j = 0; j < special.size() && !found; j++)
               {
                   if(!special.contains(Character.isLetter(password.charAt(i))))
                   {
                       goodOrNot = false;
                       z++;
                   }
               }
              
               for(int j = 0; j < number.size() && !found; j++)
               {
                   if(!number.contains(Character.isDigit(password.charAt(i))))
                   {
                       goodOrNot = false;
                       z++;
                   }
               }
              
           }
       }
       return goodOrNot;
   }
}

      
  
  
      

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Password

{

   public static void main(String [] args)

   {

   Scanner keyboard = new Scanner(System.in);

   PasswordCreator thing = new PasswordCreator ();

   String password = "";

   boolean dragon = true;

   while(dragon)

   { System.out.print("Would like to create your own password(1) or have one generated for you(2).Press 1 or 2 to choose: ");

   int option = keyboard.nextInt();

   if(option == 1)

   {

   boolean wolfDog = true;

   while(wolfDog)

   {

   wolfDog = false;

   System.out.print("Please create a password that is atleast 6 characters long, but no more than 16. " +"\n"+

   "Also, include at least one lowercase letter, one uppercase letter, one number digt, and one special character of " + "\n"+

   "!,@,#,$,%,&,*: ");

   keyboard.nextLine();

   password = keyboard.nextLine();

   thing.check(password);

   if(thing.check(password) == false)

   {

   System.out.println("You entered an invalid password, try again.");

   wolfDog = true;

   }

   }

  

   }

   break;

   }

   }

}

//Then here is my PasswordCreator class (also not done)

==============================================================================

import java.util.*;

public class PasswordCreator

{

   private ArrayList upperCase;

   private ArrayList lowerCase;

   private ArrayList number;

   private ArrayList special;

   public PasswordCreator()

   {

   upperCase = new ArrayList<> (26);

   for(int i = 0; i < 26; i++)

   {

   upperCase.add(String.valueOf((char)(65 + i)));

   }

   lowerCase = new ArrayList <> (26);

   for(int i = 0; i < 26; i++)

   {

   lowerCase.add(String.valueOf((char)(65 + i)));

   }

  

   number = new ArrayList <> (9);

   for (int i = 0; i < 9; i++)

   {

   number.add(i);

   }

   special = new ArrayList <> (7);

   special.add("!");

   special.add("@");

   special.add("#");

   special.add("$");

   special.add("%");

   special.add("&");

   special.add("*'");

   }

   //verfies if password is a minimun of 6 character but nomore than 16.

   //make sure password passes restricted rules

   public Boolean check(String password)

   {

   boolean goodOrNot = true; //flag

   int z = 0; //control variable

   if(password.length() < 6 || password.length() > 16)

   {

   goodOrNot = false;

   }

   while(goodOrNot && z > 1 || z < 13 )

   {

   for(int i = 0; i < password.length(); i++)

   {

   //Checking for uppercase requirements

   for(int j = 0; j < upperCase.size() ; j++)

   {

   if(!special.contains(Character.isUpperCase(password.charAt(i))))

   {

   goodOrNot = false;

   z++;

   }

   }

   //checking for lowercase requirements

   for(int j = 0; j < lowerCase.size() ; j++)

   {

   if(!special.contains(Character.isLowerCase(password.charAt(i))))

   {

   goodOrNot = false;

   z++;

   }

   }

   //checking for special character requirements.

   for(int j = 0; j < special.size(); j++)

   {

   if(!special.contains(Character.isLetter(password.charAt(i))))

   {

   goodOrNot = false;

   z++;

   }

   }

  

   for(int j = 0; j < number.size(); j++)

   {

   if(!number.contains(Character.isDigit(password.charAt(i))))

   {

   goodOrNot = false;

   z++;

   }

   }

  

   }

   }

   return goodOrNot;

   }

}

========================================================
See Output



3 import java.util.*; 4 public class Password 6 public static void main(String [] args) Scanner keyboard-new Scanner(System.

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
I just started working on some code for a password creator. I was doing some testing and I keep g...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, I have created a program which can generate passwords. Currently the program can generate...

    In Java, I have created a program which can generate passwords. Currently the program can generate a password through a menu system of which characters to use. To complete the project, I need a system which allows the user to generate another password after the first password is created. I also need a method to check whether or not the password is complex by checking if the generated password has 3 or more character types and then displaying whether or...

  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...

  • JAVA public static String generatePassword(String gp)        {            String password="";       ...

    JAVA public static String generatePassword(String gp)        {            String password="";            boolean b= false;            for (int i =0;i            {                char ch = gp.charAt(i);                if (i == 0)                {                    password += Character.toUpperCase(ch);                }                if (ch == 'S' || ch == 's')           ...

  • Password Validation Write a program called PasswordValidationYourLastName that validates a new password, following these guidelines: Must...

    Password Validation Write a program called PasswordValidationYourLastName that validates a new password, following these guidelines: Must be at least 8 characters Must have at least 1 uppercase letter Must have at least 1 lowercase letter Must have at least 1 digit Write a program that asks for a password, and then asks again to confirm it (you can use the scanner or JOptionPane). Pass those two Strings to a method called validatePassword( ). Your validatePassword( ) should return a boolean...

  • Here is the skeleton of the password checking program. We have the character counting function (you...

    Here is the skeleton of the password checking program. We have the character counting function (you will need to finish it), and a function to check if the password is good. Change the password algorithm so that (three of the four) two uppercase, two lowercase, two digits, or one other is present. Also change the required length of the password to 10. Bonus points for making the program loop until a good password is entered. You will need to read...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • Complete the following code: You are asked to write some functionalities for a spelling checker i...

    Complete the following code: You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT