Related
I have a java morse code assignment using constructors. I got the code working but having a hard time calling the constructor.
My original code.
import java.util.Scanner;
public class morseTest {
public static void main(String[] args) {
char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
',', '.', '?' };
String morse[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." };
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().toLowerCase();
char[] chars = input.toCharArray();
String str = "";
for (int i = 0; i < chars.length; i++) {
for (int index = 0; index < alphabet.length; index++) {
if (alphabet[index] == chars[i]) {
str = str + morse[index] + " ";
}
}
}
System.out.println(str);
sc.close();
}
}
I have to call and implement a method stringToMorse() from MorseCode.java from Demo.java.
import java.util.Scanner;
import java.util.*;
public class Demo {
public static void main(String[] args) {
// PLACE CODE HERE
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().toLowerCase();
MorseCode message = new MorseCode(input);
System.out.println(message.stringToMorse());
}
}
import java.util.Scanner;
public class MorseCode {
private static char[] alphabet;
private static String[] morse;
private static String input;
public MorseCode() {
// PLACE CODE HERE
char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
',', '.', '?' };
String morse[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." };
}
MorseCode(String s) {
input = s;
}
public static String stringToMorse(String s) {
// PLACE CODE HERE
char[] chars = input.toCharArray();
String str = "";
for (int i = 0; i < chars.length; i++) {
for (int index = 0; index < alphabet.length; index++) {
if (alphabet[index] == chars[i]) {
str = str + morse[index] + " ";
}
}
}
return s;
}
When compiling, I got the error messsage
Demo.java:13: error: method stringToMorse in class MorseCode cannot be applied to given types;
System.out.println(message.stringToMorse());
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
1 error
public static String stringToMorse(String s)
This method needs one argument.
While invoking message.stringToMorse() there is no argument passed to it.
Also , In code stringToMorse method is declared as static, so it can be call via className. Please refer below code changes.
public class Demo {
public static void main(String[] args) {
// PLACE CODE HERE
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().toLowerCase();
System.out.println(MorseCode.stringToMorse(input));
}
}
Code for MorseCode class has an error in stringToMorse method. It is returning variable s which is nowhere declared.
import java.util.Scanner;
public class MorseCode {
private static char[] alphabet ={ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
',', '.', '?' };
private static String[] morse= { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".---.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
"-----", "--..--", ".-.-.-", "..--.." };
public static String stringToMorse(String input) {
// PLACE CODE HERE
char[] chars = input.toCharArray();
String str = "";
for (int i = 0; i < chars.length; i++) {
for (int index = 0; index < alphabet.length; index++) {
if (alphabet[index] == chars[i]) {
str = str + morse[index] + " ";
}
}
}
return str;
}
How do I create a random password that meets the system's length and character set requirements in Java?
I have to create a random password that is 10-14 characters long and has at least one uppercase, one lowercase, and one special character. Unfortunately, some special characters are too special and cannot be used, so I cannot use just printed ASCII.
Many of the examples on this site generate a random password or session key without enough entropy in the characters or without realistic requirements in a business setting like the ones given above, so I'm asking more pointed question to get a better answer.
My character set, every special character on a standard US keyboard except for a space:
A-Z
a-z
0-9
~`!##$%^&*()-_=+[{]}\|;:'",<.>/?
I suggest using apache commons RandomStringUtils. Use something what is already done.
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!##$%^&*()-_=+[{]}\\|;:\'\",<.>/?";
String pwd = RandomStringUtils.random( 15, characters );
System.out.println( pwd );
If you are using maven
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
otherwise download jar
UPDATE
Version with secure random. So matter of required characters left and can be solved as in comment, generate required parts separately and normal ones. Then join them randomly.
char[] possibleCharacters = (new String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!##$%^&*()-_=+[{]}\\|;:\'\",<.>/?")).toCharArray();
String randomStr = RandomStringUtils.random( randomStrLength, 0, possibleCharacters.length-1, false, false, possibleCharacters, new SecureRandom() );
System.out.println( randomStr );
I recently learned about Passay. It provides the required functionality needed in its PasswordGenerator class. It randomly generates passwords meeting the requirements similar to what is written below using CharacterRules rather than PasswordCharacterSets as I have done below. Instead of holding a list of unused indexes for random character insertion, it simply shuffles the character buffer after inserting characters that meet the requirements.
Below is left over from before, I recommend using Passay if your licensing allows it, this code should work otherwise and provides details of why the generated passwords are crytographically strong
I ended up writing this code twice. Once to get a random character result, but it turned out the distribution of characters depended on the size of the character set(whoops!). I rewrote it and now you should just copy/paste the code and change the Main.java to the character sets you want. Although it could have been done differently, I think this is a relatively straightforward approach to get the correct result and I encourage reuse, comments, criticisms, and well-thought edits.
The controls of the PasswordGenerator code is as follows:
Min/Max Length: Set using a random number
PasswordCharacterSet: It is assumed that all PasswordCharacterSets passed into PasswordGenerator consist of unique character sets, if not, the random characters will have a skew towards the duplicates.
PasswordCharacterSet Min Characters: The min characters to use for this character set.
The main bits for the actual password generation:
Randomness of Random: We're using SecureRandom which is backed by a cryptographically strong PRNG, rather than the Random class which is not.
Random character order for the password: All the indexes of the pw char array are added to the remainingIndexes array. As we call addRandomCharacters, it removes an index randomly and we use the removed index to populate the array.
Random characters: In addRandomCharacters, a random index from the character index we're using is chosen and added to the pw array.
Guaranteeing minimum characters of each type are set: We simply carve out the minimum character amount first. We choose the minimum amount of random values from each character set and then move on.
Random distribution for the remaining characters: After the minimum values have been set, we want to make the rest of the characters random across all character sets. All the characters are added to a single array. The remaining slots are filled using the same strategy for the previous character sets.
Description of password complexity: Password complexity is usually talked about in bits of entropy. Here are the number of possibilities for your keyspace:
There is at least one uppercase alpha character (out of 26), one lowercase alpha character(out of 26), one digit (out of 10), and one special character (out of 32), the way you calculate the number of possibilities is the number of possibilities for each character multiplied by the number of characters since they are randomly placed in the string. So we know the possibilities for four of the characters are:
Required Characters = 26*26*10*32=216,320
All remaining characters have 94 (26+26+10+32) possibilities each
Our calculation is:
Characters Possibilities Bits of Entropy
10 chars 216,320*94^6 = 149,232,631,038,033,920 ~2^57
11 chars 216,320*94^7 = 14,027,867,317,575,188,480 ~2^63
12 chars 216,320*94^8 = 1,318,619,527,852,067,717,120 ~2^70
13 chars 216,320*94^9 = 123,950,235,618,094,365,409,280 ~2^76
14 chars 216,320*94^10 = 11,651,322,148,100,870,348,472,320 ~2^83
With this is mind, if you want the most secure passwords, you should always choose the highest amount of characters possible which is 14 in this case.
Main.java
package org.redtown.pw;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import org.redtown.pw.PasswordGenerator.PasswordCharacterSet;
public class Main {
public static void main(String[] args) {
Set<PasswordCharacterSet> values = new HashSet<PasswordCharacterSet>(EnumSet.allOf(SummerCharacterSets.class));
PasswordGenerator pwGenerator = new PasswordGenerator(values, 10, 14);
for(int i=0; i < 10; ++i) {
System.out.println(pwGenerator.generatePassword());
}
}
private static final char[] ALPHA_UPPER_CHARACTERS = { 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private static final char[] ALPHA_LOWER_CHARACTERS = { 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private static final char[] NUMERIC_CHARACTERS = { '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9' };
private static final char[] SPECIAL_CHARACTERS = { '~', '`', '!', '#', '#',
'$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', '{',
']', '}', '\\', '|', ';', ':', '\'', '"', ',', '<', '.', '>', '/',
'?' };
private enum SummerCharacterSets implements PasswordCharacterSet {
ALPHA_UPPER(ALPHA_UPPER_CHARACTERS, 1),
ALPHA_LOWER(ALPHA_LOWER_CHARACTERS, 1),
NUMERIC(NUMERIC_CHARACTERS, 1),
SPECIAL(SPECIAL_CHARACTERS, 1);
private final char[] chars;
private final int minUsage;
private SummerCharacterSets(char[] chars, int minUsage) {
this.chars = chars;
this.minUsage = minUsage;
}
#Override
public char[] getCharacters() {
return chars;
}
#Override
public int getMinCharacters() {
return minUsage;
}
}
}
PasswordGenerator.java
package org.redtown.pw;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class PasswordGenerator {
private final List<PasswordCharacterSet> pwSets;
private final char[] allCharacters;
private final int minLength;
private final int maxLength;
private final int presetCharacterCount;
public PasswordGenerator(Collection<PasswordCharacterSet> origPwSets, int minLength, int maxLength) {
this.minLength = minLength;
this.maxLength = maxLength;
// Make a copy of the character arrays and min-values so they cannot be changed after initialization
int pwCharacters = 0;
int preallocatedCharacters = 0;
List<PasswordCharacterSet> pwSets = new ArrayList<PasswordCharacterSet>(origPwSets.size());
for(PasswordCharacterSet origpwSet : origPwSets) {
PasswordCharacterSet newPwSet = new PwSet(origpwSet);
pwSets.add(newPwSet);
pwCharacters += newPwSet.getCharacters().length;
preallocatedCharacters += newPwSet.getMinCharacters();
}
this.presetCharacterCount = preallocatedCharacters;
this.pwSets = Collections.unmodifiableList(pwSets);
if (minLength < presetCharacterCount) {
throw new IllegalArgumentException("Combined minimum lengths "
+ presetCharacterCount
+ " are greater than the minLength of " + minLength);
}
// Copy all characters into single array so we can evenly access all members when accessing this array
char[] allChars = new char[pwCharacters];
int currentIndex = 0;
for(PasswordCharacterSet pwSet : pwSets) {
char[] chars = pwSet.getCharacters();
System.arraycopy(chars, 0, allChars, currentIndex, chars.length);
currentIndex += chars.length;
}
this.allCharacters = allChars;
}
public char[] generatePassword() {
SecureRandom rand = new SecureRandom();
// Set pw length to minLength <= pwLength <= maxLength
int pwLength = minLength + rand.nextInt(maxLength - minLength + 1);
int randomCharacterCount = pwLength - presetCharacterCount;
// Place each index in an array then remove them randomly to assign positions in the pw array
List<Integer> remainingIndexes = new ArrayList<Integer>(pwLength);
for(int i=0; i < pwLength; ++i) {
remainingIndexes.add(i);
}
// Fill pw array
char[] pw = new char[pwLength];
for(PasswordCharacterSet pwSet : pwSets) {
addRandomCharacters(pw, pwSet.getCharacters(), pwSet.getMinCharacters(), remainingIndexes, rand);
}
addRandomCharacters(pw, allCharacters, randomCharacterCount, remainingIndexes, rand);
return pw;
}
private static void addRandomCharacters(char[] pw, char[] characterSet,
int numCharacters, List<Integer> remainingIndexes, Random rand) {
for(int i=0; i < numCharacters; ++i) {
// Get and remove random index from the remaining indexes
int pwIndex = remainingIndexes.remove(rand.nextInt(remainingIndexes.size()));
// Set random character from character index to pwIndex
int randCharIndex = rand.nextInt(characterSet.length);
pw[pwIndex] = characterSet[randCharIndex];
}
}
public static interface PasswordCharacterSet {
char[] getCharacters();
int getMinCharacters();
}
/**
* Defensive copy of a passed-in PasswordCharacterSet
*/
private static final class PwSet implements PasswordCharacterSet {
private final char[] chars;
private final int minChars;
public PwSet(PasswordCharacterSet pwSet) {
this.minChars = pwSet.getMinCharacters();
char[] pwSetChars = pwSet.getCharacters();
// Defensive copy
this.chars = Arrays.copyOf(pwSetChars, pwSetChars.length);
}
#Override
public char[] getCharacters() {
return chars;
}
#Override
public int getMinCharacters() {
return minChars;
}
}
}
Here is a utility that uses just vanilla Java and implements the requirements. It basically gets one of each of the required character sets. Then populates the rest with random chars from the whole set. Then shuffles it all up.
public class PasswordUtils {
static char[] SYMBOLS = "^$*.[]{}()?-\"!##%&/\\,><':;|_~`".toCharArray();
static char[] LOWERCASE = "abcdefghijklmnopqrstuvwxyz".toCharArray();
static char[] UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
static char[] NUMBERS = "0123456789".toCharArray();
static char[] ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789^$*.[]{}()?-\"!##%&/\\,><':;|_~`".toCharArray();
static Random rand = new SecureRandom();
public static String getPassword(int length) {
assert length >= 4;
char[] password = new char[length];
//get the requirements out of the way
password[0] = LOWERCASE[rand.nextInt(LOWERCASE.length)];
password[1] = UPPERCASE[rand.nextInt(UPPERCASE.length)];
password[2] = NUMBERS[rand.nextInt(NUMBERS.length)];
password[3] = SYMBOLS[rand.nextInt(SYMBOLS.length)];
//populate rest of the password with random chars
for (int i = 4; i < length; i++) {
password[i] = ALL_CHARS[rand.nextInt(ALL_CHARS.length)];
}
//shuffle it up
for (int i = 0; i < password.length; i++) {
int randomPosition = rand.nextInt(password.length);
char temp = password[i];
password[i] = password[randomPosition];
password[randomPosition] = temp;
}
return new String(password);
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(getPassword(8));
}
}
}
Using the random functionality of java.util package of rt.jar, we can create a random password of any length. below is the snippet for the same.
public class GeneratePassword {
public static void main(String[] args)
{
int length = 10;
String symbol = "-/.^&*_!#%=+>)";
String cap_letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String small_letter = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String finalString = cap_letter + small_letter +
numbers + symbol;
Random random = new Random();
char[] password = new char[length];
for (int i = 0; i < length; i++)
{
password[i] =
finalString.charAt(random.nextInt(finalString.length()));
}
System.out.println(password);
}
}
I know it is an old question, but maybe my solution will help someone with the same problem.
I'm using RandomStringUtils for password generator and then I check did password fulfilled conditions (at least one symbol, one capital letter, one small letter and one number and 8 characters long) with regex and if it didn't then I call again password generator with recursion until condition is not fulfilled. I can say that method is not gonna be called more than 3 times for sure!
public String generatePassword() {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#!#$%&";
String password = RandomStringUtils.random( 8, characters );
String regex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[#!#$%&])(?=\\S+$).{8,}$";
Pattern pattern = Pattern.compile( regex );
Matcher matcher = pattern.matcher( password );
if (matcher.matches()) {
return password;
} else {
return generatePassword(); // recursion
}
}
Maven dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
public static final Character[] ALPHA_UPPER_CHARACTERS = {'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public static final Character[] ALPHA_LOWER_CHARACTERS = {'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
public static final Character[] NUMERIC_CHARACTERS = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9'};
public static final Character[] SPECIAL_CHARACTERS = {'#', '#',
'$', '%', '^', '&', '*', '|', ';', ':', '?'};
**Note: I copied char set from #summer**
final List<Character[]> charSets = new ArrayList<>();
charSets.add(Constant.ALPHA_UPPER_CHARACTERS);
charSets.add(Constant.ALPHA_LOWER_CHARACTERS);
charSets.add(Constant.NUMERIC_CHARACTERS);
charSets.add(Constant.SPECIAL_CHARACTERS);
public String getFilterPassword() {
StringBuilder passBuilder = new StringBuilder();
final int charSetLen = charSets.size();
for (int i = 0; i < 10; i++) {
int randomLength = new Random().nextInt(charSetLen - 1);
Character[] newAlpha = charSets.get(randomLength);
int randomSetLen = newAlpha.length;
int randomAlphaLen = new Random().nextInt(randomSetLen - 1);
passBuilder.append(newAlpha[randomAlphaLen]);
}
return passBuilder.toString();
}
This program is supposed to translate English into morse code, but every time i input a word I get English letters and numbers such as "\cf0" for hi. I am pretty sure the morse code array has the right values. Could it be a formatting problem? PLease help. thanks.
MorseCode class
public class MorseCode {
public static String decode(char[] alphabet, String[] morseCode, String originalMessage) {
char currentChar;
String getMorseChar;
String convertedString = " ";
for (int i = 0; i < originalMessage.length(); i++) {
convertedString = " ";
currentChar = originalMessage.charAt(i);
getMorseChar = convert(currentChar, alphabet, morseCode);
convertedString = convertedString + getMorseChar;
}
return convertedString;
}
public static String convert (char currentChar, char[] alphabet, String[] morseCode) {
String morse = "";
for (int x = 0; x < alphabet.length; x++) {
if (currentChar == alphabet[x])
morse = morseCode[x];
}
return morse;
}
}
MorseCodeTester class
public class MorseCodeTester {
public static void main(String[] args) throws IOException {
String[] morseCode = new String[26];
char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z' };
Scanner scanner = new Scanner(System.in);
Scanner inFile = new Scanner(new File("morse.rtf"));
for (int x = 0; x < 26; x++) {
morseCode[x] = inFile.next( );
}
inFile.close();
System.out.println("What message would you like to translate?");
String originalMessage = scanner.next();
String converted = MorseCode.decode(alphabet, morseCode, originalMessage);
System.out.println(converted);
}
}
I am trying to develop a substitution cipher that uses a keyword to create a new cipher alphabet. I am new to Java (as I'm sure you will be able to tell!) and I am finding it
hard to wrap my head around the code for what I need to do.
My understanding is as follows:
If for example, the keyword is javben, I should start by finding the index of the "j" in the plainText string array, which is 9. I then want to shift the plainText[9] into cipherText[0] and move each other element over by 1. So the first pass of this would result in:
cipherText[] = {"j","a","b","c","d","e","f","g","h","i","k","l","m","n","o","p","q","r","s","t","u","v","w","r","x","y","z"}
Then I would find the "a" and it's it's already where it should be so I'll need to account for this and not shift it -- somehow. The next character is the "v" and so the process would continue.
After shifting everything in the cipher I should end up with:
plainText []= {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","r","x","y","z"}
cipherText[]= {"j","a","v","b","e","n","c","d","f","g","h","i","k","l","m","o","p","q","r","s","t","u","w","r","x","y","z"}
As you can see, I am reasonably sure that I understand the process of which to go through, however I am really struggling wrap my head around the code required for this. Help please!
import java.util.Scanner;
import java.io.*;
/**
* This program uses a keyword for a simple substitution cipher.
*
* #author Bryan
* #version Programming Project
*/
public class Cipher
{
// The main method removes duplicate characters in a word input by the user.
public static void main(String[] args) throws IOException
{
// Creatae a new scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
// prompt the user to enter a word
System.out.println("Please enter your keyword: ");
// and get their input
String input = keyboard.nextLine();
// the keyword will be built up here
String keyword = "";
while(input.length() > 0)
{
// get the first letter
char letter = input.charAt(0);
// if the letter is not already in the output
if (keyword.indexOf(letter) == -1)
{
// add it to the end
keyword = keyword + letter;
}
// that letter is processed : discard it
input = input.substring(1);
}
//this is just to confirm the duplicate letters in the keyword are removed
System.out.println(keyword);
getFile();
}
/**
* This asks the user to specify a filename which is then
* read into the program for enciphering
*/
public static void getFile()throws IOException
{
// Creatae a new scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
// Get the file name
System.out.println("Enter the file name: ");
String filename = keyboard.nextLine();
//Open the file
File file = new File(filename);
Scanner inputFile = new Scanner(file);
// Read the lines from the file until no more are left
while (inputFile.hasNext())
{
//Read the next line
String allText = inputFile.nextLine();
// Display the text
System.out.println(allText);
}
//Close the file
inputFile.close();
}
public static void alphabet()
{
String[] plainText = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] cipherText = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
}
}
This one is quite simple, just set up a function that for every letter in the key word it just takes it out of the alphabet array and then add the two arrays together with the array of letters at the beginning and the alphabet without those letters after it. E.g:
String[] cipherKeyWord(String keyWord, String[] alphabet){
ArrayList<String> finalCipher = (ArrayList) Arrays.asList(keyWord.split("(?!^)"));
//^ This splits it into a string of every word using regular expressions
ArrayList<String> newAlphabet = (ArrayList) Arrays.asList(alphabet);
newAlphabet.removeAll(finalCipher);
finalCipher.addAll(newAlphabet);
return finalCipher.toArray(new String[finalCipher.size()]);
}
package Classes;
public class SubstitutionCipherClass {
public static void main(String[] args) {
char plainText[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};
char cipherText[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'r', 'x', 'y', 'z'};
String pt = "haci";
for (int a = 0; a < pt.length(); a++) {
for (int i = 0; i < plainText.length; i++) {
if(plainText[i] == (pt.charAt(a))){
System.out.println(cipherText[i]);
}
}
}
}
}
I have done substitution cipher using following code
import java.util.*;
class SubCipher
{
public static void main(String args[])
{
String plainText = ",.<>;':\"[]{}-=_+)(*&^%$#\"#!),998683,1,x3x33,10~1,1,10~2,2,20";
String strcipherText = Encrypt(plainText);
String strdecryptedText = Decrypt(strcipherText);
System.out.println("Plain Text :"+plainText);
System.out.println("Encrypted Text :"+strcipherText);
System.out.println("Decrypted Text :"+strdecryptedText);
}
private static String Encrypt(String text)
{
byte[] textBytes = text.getBytes();
for (int i = 0; i < textBytes.length; i++)
{
int currentByteValue = (int)textBytes[i];
textBytes[i] = (byte)(currentByteValue > 255 ? currentByteValue - 255 + 2 : currentByteValue + 2);
}
String strbyte=new String(textBytes);
return strbyte;
}
private static String Decrypt(String text)
{
byte[] textBytes = text.getBytes();
for (int i = 0; i < textBytes.length; i++)
{
int currentByteValue = (int)textBytes[i];
textBytes[i] = (byte)(currentByteValue < 0 ? currentByteValue + 255 - 2 : currentByteValue - 2);
}
String strbyte=new String(textBytes);
return strbyte;
}
}
Here is my code. I am having an issue. It works just fine but when you convert morse code to english, it only prints out the morse code single digit letters. Can someone give me a solution or at least help me understand what is wrong because it is immensely frustrating.
Here is the bit of my code that matters
An example of the problem is when i put in .- it printed e and t, not a.
public class Project1
{
public static void main( String [] args )
{
System.out.println();
choice();
}
public static void choice()
{
int user_choice = 0;
user_choice = Input.getInt("Enter 1 if you want to change English to Morse code, and enter 2 to change Morse code to English");
if(user_choice == 1)
{
String output = new String();
String inital = new String();
inital = english_to_morse();
for( int k = 0; k < inital.length(); k++)
{
output += morse(inital.charAt( k ));
}
System.out.print(output);
}
if(user_choice == 2)
{
String output2 = new String();
String inital2 = new String();
inital2 = morse_to_english();
for( int k = 0; k < inital2.length(); k++)
{
System.out.println("#####"+String.valueOf(inital2.charAt( k ))+"#####");
output2 += english(String.valueOf(inital2.charAt( k )));
}
System.out.print(output2);
}
}
public static String english_to_morse()
{
String user_input = new String();
user_input = Input.getString("Enter a phrase and I'll convert it to Morse Code");
return user_input.toLowerCase();
}
public static String morse_to_english()
{
String user_input = new String();
user_input = Input.getString("Enter a phrase in Morse Code and I'll convert it to English");
return user_input.toLowerCase();
}
public static String morse(char letter)
{
String output = new String();
char[] alphabet_numbers = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };
String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };
for( int j = 0; j < alphabet_numbers.length; j++ )
{
if (alphabet_numbers[j]==letter)
{
output = morse_code[j];
}
}
return output + " ";
}
public static String english(String letter)
{
String output = new String();
String alphabet_numbers[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " };
String morse_code[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "|" };
for( int j = 0; j < morse_code.length; j++ )
{
if (morse_code[j].equals(letter))
{
output = alphabet_numbers[j];
}
}
return output + " ";
}
}
Well the reason why .- is giving et instead of a is due to the way you're reading the string.
Your code reads . and then looks up the table to determine whether it corresponds to any alpha character, in which case it does: e. Then you read a - and you look it up and you get a t.
If your input is literally just
.-.---.-.-.........-----.-.-.-.-
You're pretty much stuck because you don't know when one ends and another begins. As another example, how should one distinguish between the following strings
.
..
...
....
They are all equally valid signals, but depending on how you interpret it, you get very different results.
You can't say something like "I'll just take the longest matching string" because there is no reason why that is a valid rule.
If the sample input I provided above does not match your input, you should indicate what your input is.
The problem is in how you iterate your strings.
When going from English to Morse, it is ok to just iterate single characters as you do here:
for( int k = 0; k < inital.length(); k++) {
output += morse(inital.charAt( k ));
}
but when going from Morse to English you have to iterate several characters at once, because a symbol in Morse generally spans several characters. For instance, the Morse string .- -... -.-. has three symbols that correspond to abc in English, but they have 2, 4 and 4 characters each.
So when iterating your Morse string, you have to split it by spaces, and iterate each of the substrings. In the case above, you'll iterate .-, then -... and then -.-.:
for(String symbol : inital2.split(" ")){
output2 += english(symbol);
}
Your problem is this line
output2 += english(String.valueOf(inital2.charAt( k )));
You give ".-" as input, but it does not convert ".-", because it takes each character of the string ".-" and converts it, which gives your "e t" result.
To fix this you have to use a separator in your morse code input. You already somehow defined " " as a separator so I will use it.
if(user_choice == 2){
String output2 = new String();
String[] inital2 = morse_to_english().split(" ");
for( int k = 0; k < inital2.length; k++){
output2 += english(inital2[k]);
}
System.out.print(output2);
}