Question

In: Computer Science

Using Java: Write a program that uses hash tables and reads in a string from the...

Using Java:

Write a program that uses hash tables and reads in a string from the command line and a dictionary of words from standard input, and checks whether it is a "good" password. Here, assume "good" means that it (i) is at least 8 characters long, (ii) is not a word in the dictionary, (iii) is not a word in the dictionary followed by a digit 0-9 (e.g., hello5), (iv) is not two words separated by a digit (e.g., hello2world).

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
public class x{
   public static void main(String[] args) {
       //Intializing a HashTable
       Hashtable<String, Integer> h = new Hashtable<String,Integer>();
       Scanner s=new Scanner(System.in);
       System.out.println("Enter how many words to add in dictionary:");
       int dictSize=s.nextInt();
       //Filling the Dictionary
       for(int i=0;i<dictSize;i++){
           System.out.println("Enter dictionary Word:");
           String word=s.next();
           h.put(word,new Integer(1));
       }
       //Input a string
       System.out.println("Enter Password:");
       String password=s.next();
       //Check for password size
       if(password.length()<8){
           System.out.println("Not a Good Password,String length less than 8");
           System.exit(0);
       }
       //Check if password is one of dictionary word
       if(h.containsKey(password)){
           System.out.println("Not a Good Password,String present in dictionary");
           System.exit(0);
       }
       //To extract the part before digit
       String letterPart="";
       for(int i=0;i<password.length();i++){
           char as=password.charAt(i);
           if(as>='0' && as<='9'){
               letterPart=password.substring(0,i);
               //If any letter is present after number then not a good password
               if(i!=(password.length()-1)){
                   System.out.println("Not a Good Password,Digit present between words");
                   System.exit(0);
               }
           }
       }
       //Check if password is not a dictionary word followed by digit
       if(h.containsKey(letterPart)){
           System.out.println("Not a Good Password,String present in dictionary");
           System.exit(0);
       }
       System.out.println("Good Password!!");

   }
}


Related Solutions

in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT