public String getEncryption(String text){
String x = "";
for(int i = 0; i < text.length(); i++){
String sub = text.substring(i, i+1);
System.out.println(i + " = " + sub);
String en = encrypt_code[Integer.parseInt(sub)];
System.out.println("Result:" + en);
x = x.concat(en);
}
return x;
}
I have this coding which works perfectly but I need to know how to use the charAt method to convert this and works in the same way? I have this so far
public String getEncryption2(String text){
String x = "";
for(int i = 0; i < text.length(); i++){
char ch = text.charAt(i);
You're reading every char as a substring, and parse it as an int. So, you get for example "4" as the substring, and parse it to get the int 4.
If you use charAt(), you'll get the char '4' instead. To transform it to the integer 4, you just need to subtract '0' from it:
String en = encrypt_code[ch - '0'];
Note that replacing the whole string by a new one at each iteration is time consuming. You should use a StringBuilder instead:
StringBuilder x = new StringBuilder();
for (...) {
x.append(en);
}
return x.toString();
Related
I need to write encryptor.
The first letter needs to be converted to its ASCII code.
The second letter needs to be switched with the last letter
It should make from this "65 119esi 111dl 111lw 108dvei 105n 97n 111ka"
This "A wise old owl lived in an oak"
But when I check is char a digit i have an error.
public static void encryptThis(String text) {
String [] text_arr = text.split("\\s");
for(int i = 0; i < text_arr.length;i++){
String word = text_arr[i];
int length = word.length();
char [] char_word = new char [length];
char_word = word.toCharArray();
int k = 0;
length = char_word.length;
char [] numb = new char [length];
for (int j = 0;j < length; j++){
//In this place
if (Character.isDigit(char_word[i])){
numb[k] = char_word[i];
char_word[i] = ' ';
k++;
System.out.println(numb[k]);
}
}
int number = Integer.parseInt(numb.toString(),8);
String edit_char_word = char_word.toString();
String final_str = new StringBuffer(edit_char_word).reverse().toString().trim();
final_str = number + final_str;
text_arr[i] = final_str;
}
String fin_text = text_arr.toString();
return fin_text;
}
I wrote the encryptThis method from the beginning and tried to stay as simple to get readable code, here is
a full working example. As you did not mention what to do if a number is the first "letter" there is no
special handling for this - an "1" will be encrypted to (ascii) 49...
public class Main_So {
public static void main(String[] args) {
System.out.println("https://stackoverflow.com/questions/62460366/in-function-isdigit-java-lang-arrayindexoutofboundsexception-5");
String input = "A wise old owl lived in an oak";
String outputExpected = "65 119esi 111dl 111lw 108dvei 105n 97n 111ka";
System.out.println("input: " + input);
String output = encryptThis(input);
System.out.println("output encryptThis: " + output);
System.out.println("output expected: " + outputExpected);
}
public static String encryptThis(String text) {
String[] text_arr = text.split("\\s");
String outputString = "";
for (int i = 0; i < text_arr.length; i++) {
String word = text_arr[i];
int length = word.length();
int ascii0 = (int) word.charAt(0);
String word0 = String.valueOf(ascii0); // first char as ascii code
String subString = word.substring(1);
if (length > 2) { // switch chars if string length > 2
char char1 = word.charAt(1);
char charEnd = word.charAt(length - 1);
StringBuilder wordBuilder = new StringBuilder(subString);
wordBuilder.setCharAt(0, charEnd);
wordBuilder.setCharAt((length - 2), char1);
subString = String.valueOf(wordBuilder);
}
outputString = outputString + word0 + subString + " ";
}
return outputString;
}
}
This is the result:
input: A wise old owl lived in an oak
output encryptThis: 65 119esi 111dl 111lw 108dvei 105n 97n 111ka
output expected: 65 119esi 111dl 111lw 108dvei 105n 97n 111ka
I have a question. I'm struggling with a exercise where I am asked to get from user a string, a character which I want to duplicate in this string, and a number - how many times I want to duplicate. Eg: string input: dog; character: o; number:4. Output: doooog. My question is how can I achieve this result?
Scanner sc = new Scanner(System.in);
System.out.println("enter your string");
String text = sc.nextLine();
System.out.println("enter the character that will be repeated");
char character= sc.next().charAt(0);
System.out.println("enter the number of repetitions");
int repetitions = sc.nextInt();
for (int i = 0; i < text.length(); i++) {
char z = text.charAt(i);
if(z==character) {
// repeat character in string put by user * repetitions
}
}
System.out.println(text);
If you are using Java 8, you can use String.join and replace like this :
String str = "dog";
int length = 4;
String s = "o";
str = str.replace(s, String.join("", Collections.nCopies(length, s)));// doooog
read more about Collections::nCopies
To complete your code, insert the following:
String ans="";
for (int i = 0; i < text.length(); i++) {
char z = text.charAt(i);
if(z==character) {
for(int j=0;j<repetitions;j++)
ans+=z;
}
else
ans+=z;
}
However, there is a far more elegant way of doing this:
String replacement="";
for(int i=0;i<repetitions;i++)
replacement+=character;
String ans = text.replaceAll(String.valueOf(character), replacement);
The below should work:
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char z = text.charAt(i);
if(z==character) {
// repeat character in string put by user * repetitions
for (int j=1;j<=repetitions;j++){
buffer.append(z);
}
}else{
buffer.append(z);
}
}
System.out.println(buffer.toString());
If you are not using Java 8 you can try this.
StringBuilder builder = new StringBuilder();
String repetitionString = String.format("%" + repetitions + "s", character).replace(' ', character);
for (int i = 0; i < text.length(); i++) {
char z = text.charAt(i);
builder.append(z == character ? repetitionString : z);
}
text = builder.toString();
System.out.println(text);
In this solution we are using String.format for repeating the character that we want instead of a loop.
String.format("%" + repetitions + "s", z).replace(' ', z)
This is similar to what we can do in Python using the * operator, ie
z * repeatitions
which will repeat the character in z required number of times.
Another approach by which you can avoid the loops is using String.replace() method.
String repetitionString = String.format("%" + repetitions + "s", character).replace(' ', character);
text = text.replace(String.valueOf(character), repetitionString);
System.out.println(text);
I am purely novice in Java. Yesterday I was working on a home work. I had to split a string character by character and store those letters in an array. Then print this array. (DONE)
Then concatenate a single character (let's say 'a') with the every element of that array and print the results. (NOT DONE)
And at-last concatenate all those elements and create a string and print it. (NOT DONE)
String name = "Rocky";
int i;
int size = name.length();
char strings[] = new char[size];
for (i = 0; i <= size; i++) {
strings[i] = name.charAt(i);
System.out.println(strings[i]); // 1st output is done
}
The second output (concatenated char) should be:
Ra
oa
ca
ka
ya
The third output (single concatenated string) should be:
Raoacakaya
Finally I have done this and it works after-all its my homework maybe its not all up to standard. Thanks all for for replying.
String a="a";
String name="Rocky";
String temp="";
int i;
String array[]=name.split("");
String array2[]=new String[name.length()+1];
for(i=1; i<=name.length();i++)
{
System.arraycopy( array, 0, array2, 0, array.length );
System.out.println(array[i]);
}
System.out.println();
for(i=1; i<=name.length();i++)
{
System.out.println(array2[i]+a);
array2[i]=array2[i]+a;
}
for (i=1; i<array2.length;i++)
{
temp=temp+array2[i];
}
System.out.println();
System.out.println(temp);
}
}
First, you don't need to use int size, just use name.length() instead. As for the outputs, you can do it this way:
char c2 = 'a';
String all = "";
for(char c : strings)
{
String s = String.valueOf(c) + String.valueOf(c2);
System.out.println(s); // 2nd output
all += s;
}
System.out.println(all); // 3rd output
I hope I get you right. If so, this should do the trick:
public static void homeWork() {
final String name = "Rocky";
final char toAdd = 'a';
char[] array = name.toCharArray();
String concatenated = concatenate(array, toAdd);
System.out.println("Concatenated : " + concatenated);
}
public static String concatenate(char[] array, char toAdd) {
StringBuilder buidler = new StringBuilder();
for (char c : array) {
String toAppend = String.valueOf(c) + String.valueOf(toAdd);
System.out.println(toAppend);
buidler.append(toAppend);
}
return buidler.toString();
}
You run through the array with a for each loop and append
all characters with the letter you put in "toAdd".
Print the result for every loop and print the end result,
after the method returns.
Output:
Ra
oa
ca
ka
ya
Concatenated : Raoacakaya
For 2nd output:
char a = 'a';
String all = "";
for(char c : strings)
{
String s = String.valueOf(c) + String.valueOf(a);
System.out.println(s);
}
For 3rd output:
char a = 'a';
String wholeString= "";
for(char c : strings)
{
String s = String.valueOf(c) + String.valueOf(a);
System.out.print(s);
wholeString+= s;
}
System.out.println(wholeString);
I need to get a new string based on an old one and a lag. Basically, I have a string with the alphabet (s = "abc...xyz") and based on a lag (i.e. 3), the new string should replace the characters in a string I type with the character placed some positions forward (lag). If, let's say, I type "cde" as my string, the output should be "fgh". If any other character is added in the string (apart from space - " "), it should be removed. Here is what I tried, but it doesn't work :
String code = "abcdefghijklmnopqrstuvwxyzabcd"; //my lag is 4 and I added the first 4 characters to
char old; //avoid OutOfRange issues
char nou;
for (int i = 0; i < code.length() - lag; ++i)
{
old = code.charAt(i);
//System.out.print(old + " ");
nou = code.charAt(i + lag);
//System.out.println(nou + " ");
// if (s.indexOf(old) != 0)
// {
s = s.replace(old, nou);
// }
}
I commented the outputs for old and nou (new, but is reserved word) because I have used them only to test if the code from position i to i + lag is working (and it is), but if I uncomment the if statement, it doesn't do anything and I leave it like this, it keeps executing the instructions inside the for statmement for code.length() times, but my string doesn't need to be so long. I have also tried to make the for statement like below, but I got lost.
for (int i = 0; i < s.length(); ++i)
{
....
}
Could you help me with this? Or maybe some advices about how I should think the algorithm?
Thanks!
It doesn't work because, as the javadoc of replace() says:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
(emphasis mine)
So, the first time you meet an 'a' in the string, you replace all the 'a's by 'd'. But then you go to the next char, and if it's a 'd' that was an 'a' before, you replace it once again, etc. etc.
You shouldn't use replace() at all. Instead, you should simply build a new string, using a StringBuilder, by appending each shifted character of the original string:
String dictionary = "abcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder(input.length());
for (int i = 0; i < input.length(); i++) {
char oldChar = input.charAt(i);
int oldCharPositionInDictionary = dictionary.indexOf(oldChar);
if (oldCharPositionInDictionary >= 0) {
int newCharPositionInDictionary =
(oldCharPositionInDictionary + lag) % dictionary.length();
sb.append(dictionary.charAt(newCharPositionInDictionary));
}
else if (oldChar == ' ') {
sb.append(' ');
}
}
String result = sb.toString();
Try this:
Convert the string to char array.
iterate over each char array and change the char by adding lag
create new String just once (instead of loop) with new String passing char array.
String code = "abcdefghijklmnopqrstuvwxyzabcd";
String s = "abcdef";
char[] ch = s.toCharArray();
char[] codes = code.toCharArray();
for (int i = 0; i < ch.length; ++i)
{
ch[i] = codes[ch[i] - 'a' + 3];
}
String str = new String(ch);
System.out.println(str);
}
My answer is something like this.
It returns one more index to every character.
It reverses every String.
Have a good day!
package org.owls.sof;
import java.util.Scanner;
public class Main {
private static final String CODE = "abcdefghijklmnopqrstuvwxyz"; //my lag is 4 and I added the first 4 characters to
#SuppressWarnings("resource")
public static void main(String[] args) {
System.out.print("insert alphabet >> ");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
char[] char_arr = s.toCharArray();
for(int i = 0; i < char_arr.length; i++){
int order = CODE.indexOf(char_arr[i]) + 1;
if(order%CODE.length() == 0){
char_arr[i] = CODE.charAt(0);
}else{
char_arr[i] = CODE.charAt(order);
}
}
System.out.println(new String(char_arr));
//reverse
System.out.println(reverse(new String(char_arr)));
}
private static String reverse (String str) {
char[] char_arr = str.toCharArray();
for(int i = 0; i < char_arr.length/2; i++){
char tmp = char_arr[i];
char_arr[i] = char_arr[char_arr.length - i - 1];
char_arr[char_arr.length - i - 1] = tmp;
}
return new String(char_arr);
}
}
String alpha = "abcdefghijklmnopqrstuvwxyzabcd"; // alphabet
int N = alpha.length();
int lag = 3; // shift value
String s = "cde"; // input
StringBuilder sb = new StringBuilder();
for (int i = 0, index; i < s.length(); i++) {
index = s.charAt(i) - 'a';
sb.append(alpha.charAt((index + lag) % N));
}
String op = sb.toString(); // output
In an attempt to filter the bad words, I found the 'replace' function in java is not as handy as intended.
Please find below the code :
Eg : consider the word 'abcde' and i want to filter it to 'a***e'.
String test = "abcde";
for (int i = 1; i < sdf.length() - 1; i++) {
test= test.replace(test.charAt(i), '*');
}
System.out.print(test);
Output : a***e
But if the String is String test = "bbcde";, the output is ****e. It seems, if the word has repetitive letters(as in here), the replace function replaces the repetitive letters
too.
Why is it so? I want to filter the words excluding the first and last letter.
That is because String.replace(char, char) replaces all occurrences of the first character (according to its Javadoc).
What you want is probably more like this:
char[] word = test.toCharArray();
for (int i = 1; i < word.lengh - 1; i++) { // make sure to start at second char, and end at one-but-last char
word[i] = '*';
}
System.out.println(String.copyValueOf(word));
since String.replace(char, char) replaces all occurrences of specified char, this would be a better approach for your requirement:
String test = "abcde";
String replacement = "";
for (int i = 0; i < sdf.length(); i++) {
replacement += "*";
}
test= test.replace(sdf, replacement );
System.out.print(test);
It seems, if the word has repetitive letters(as in here), the replace function replaces the repetitive letters too. Why is it so?
Why? Because that's just how it works, exactly as the API documentation of String.replace(char oldChar, char newChar) says:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
If you just want to replace the content of the string by the first letter, some asterisks and the last letter, then you don't need to use replace at all.
String test = "abcde";
if (test.length() >= 1) {
StringBuilder result = new StringBuilder();
result.append(test.charAt(0));
for (int i = 0; i < test.length() - 2; ++i) {
result.append('*');
}
result.append(test.charAt(test.length() - 1));
test = result.toString();
}
System.out.println(test);
public static void main(String[] args) {
String test = "bbcde";
String output = String.valueOf(test.charAt(0));
for (int i = 1; i < test.length() - 1; i++) {
output = output + "*";
}
output = output + String.valueOf(test.charAt(test.length() - 1));
System.out.print(output);
}
You should use the replaceAll-Function:
Link
With this you can replace all times you find a given substring in a string (f.e. "abcde") and replace all these with another string (f.e. "a***e").
String test = "abcde";
String replacement = "";
for (int i = 0; i < test.length(); i++) {
if (i==0 || i==test.length()-1){
replacement += test.charAt(i);
} else {
replacement += "*";
}
}
sdf = sdf.replaceAll(test, replacement);
System.out.print(test);