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
  • 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')           ...

  • 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;   ...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • Create a program via Java that has atleast 8 characters long, one upper case, and one...

    Create a program via Java that has atleast 8 characters long, one upper case, and one lower case, and one digit. here is my code:     public static void main(String[] args)     {         //variables defined         String passwords;         char password =0;                 //settings up password condition to false         boolean passwordcondition= false;         boolean size=false;         boolean digit=false;         boolean upper=false;         boolean lower=false;         //inputting a scanner into the system         Scanner keyboard = new Scanner(System.in);...

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
ADVERTISEMENT