How to overwrite String in loop - java

public static String decrypt(final String encryptedText, final int n) {
char [] chars;
String s = encryptedText;
String s1 = "";
int buffer = 0;
int buffer2 = 1;
for (int j = 0; j < n; j++) {
if (j < 1){
chars = s.toCharArray();
}else{
chars = s1.toCharArray();
}
char [] charsUpdate = new char[chars.length];
for (int i = chars.length / 2; i < chars.length; i++) {
if (buffer % 2 == 0 && buffer <= charsUpdate.length){
charsUpdate[buffer] = chars[i];
}
buffer += 2;
}
for (int i = 0; i < chars.length / 2 ; i++) {
if (buffer2 % 2 != 0 && buffer2 < charsUpdate.length){
charsUpdate[buffer2] = chars[i];
}
buffer2 += 2;
}
s = "";
s1 = "";
for (int i = 0; i < charsUpdate.length; i++) {
s = s + charsUpdate[i];
}
s1 = s;
}
return s;
Hi, community. I have some problem here. I try to resolve some task and have stuck in moment when I need to overwrite my String in loop. I need give my old String in start of loop and overwrite this String in end of loop and give new String to start of loop, something like that, but I can't do this, because after first iteration in the start of loop I can see my empty String. Sorry for my shit-code and bad english :)

There are some other issues:
ArrayIndexOutOfBoundsException because the condition buffer <= charsUpdate.length is incorrect
Variables buffer and buffer2 are not reset at the end of the loop, so after recreation of charUpdate array during the 2nd and the following iteration the symbols are not copied from chars to charUpdate
Updated and refactored code may look as follows:
public static String decrypt(final String encryptedText, final int n) {
int len = encryptedText.length();
int half = len / 2;
String s = encryptedText;
for (int j = 0; j < n; j++) {
char[] chars = s.toCharArray();
char[] charsUpdate = new char[len];
for (int i = half, even = 0; i < len && even < len; i++, even += 2) {
charsUpdate[even] = chars[i];
}
for (int i = 0, odd = 1; i < half && odd < len; i++, odd += 2) {
charsUpdate[odd] = chars[i];
}
s = new String(charsUpdate);
}
return s;
}
But I am not sure that it produces valid results.
A simple test shows the following output:
for (int i = 1; i < 5; i++) {
System.out.println("abcdefgh (" + i + ") -> " + decrypt("abcdefgh", i));
}
output:
abcdefgh (1) -> eafbgchd
abcdefgh (2) -> gecahfdb
abcdefgh (3) -> hgfedcba
abcdefgh (4) -> dhcgbfae
----
// for odd length of the input string
abcdefg (1) -> daebfcg
abcdefg (2) -> bdfaceg
abcdefg (3) -> abcdefg
abcdefg (4) -> daebfcg

Related

How to compute the frequency of occurrence of '+' and '++' in Java?

I want to calculate the frequency of the occurrence of all the operators from an input text file. The file contains the operators + and ++. How can I distinguish their respective frequency, as my program treats ++ as 2 distinct + operators rather than 1 ++?
Here is my code (input7.txt is a test file):
public static void main(String[] args) throws IOException {
String string = new String(Files.readAllBytes(Paths.get("input7.txt"))); //String to be counted
int frequencyArray[] = new int[string.length()];
int frequencyArray2[] = new int[string.length()];
char stringArray[] = string.toCharArray(); //Array of characters
int i, j;
//Count characters
for (i = 0; i < string.length(); i++) {
frequencyArray[i] = 1;
//frequencyArray2[i] = 1;
for(j = i + 1; j < string.length(); j++)
{
if(stringArray[i] == stringArray[j])
{
frequencyArray[i]++;
stringArray[j] = '0'; //To avoid revisiting a character
}
}
}
//Display results
System.out.println("Characters and their corresponding frequencies");
for (i = 0; i < frequencyArray.length; i++) {
if (stringArray[i] != ' ' && stringArray[i] != '0') {
System.out.println(stringArray[i] +"-" + frequencyArray[i]);
}
}
}
This works for me:
String s = "sdfasd++ sdfadsf+asdf sdf++sadfasdf++sadfsdf+asdfasdf++";
// create Set with all distinct characters
Set<Character> chars = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
chars.add(s.charAt(i));
}
// count distinct characters and put Results in HashMap
Map<Character, Integer> counts = new HashMap<Character, Integer>();
for (Character c : chars) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c)
count++;
}
counts.put(c, count);
}
// Count double-Character-Operators like this
int countPlusPlus = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.substring(i, i + 2).equals("++"))
countPlusPlus++;
}
// Calculate totals like this
int singleplusTotal = counts.get('+');
System.out.println("Single Plus total" + singleplusTotal);
System.out.println("Double Plus total" + countPlusPlus);
System.out.println("Only single Plus" + (singleplusTotal - countPlusPlus * 2));

adding characters in an array at specific points

I'm working on a Caesar cipher and I've gotten the majority of the code to work as planned.
the code is supposed to
remove all special characters and spaces
bring everything to uppercase
add spaces at an inputted interval any additional leftover spaces with x's
so for example if i were to type
plaintext: Hi im Doug
key: 1
buffer: 3
my output should be
IJJ NEP VHX
now I've gotten everything to work but the buffer part
this is my code in its entirety
import java.util.Scanner;
import java.lang.String;
public class Main {
public static void main(String[] args) {
System.out.print("Enter plaintext: ");
Scanner pTextInp = new Scanner(System.in);
String pText = pTextInp.nextLine();
System.out.print("Enter key value: ");
Scanner kInp = new Scanner(System.in);
int key = kInp.nextInt();
pText = normalizeText(pText);
pText = caesarify(pText, key);
System.out.print("Enter desired grouping number: ");
Scanner grpInp = new Scanner(System.in);
int grpInt = grpInp.nextInt();
pText = groupify(grpInt, pText);
System.out.println(pText);
}
// CONVERT STRING TO A CHAR ARRAY
public static char[] sArray(String s) {
int sLen = s.length();
char[] sChar = new char[sLen + 1];
for (int i = 0; i < sLen; i++){
sChar[i] = s.charAt(i);
}
return sChar;
}
public static String caesarify(String s, int k) {
int sLen = s.length();
char cText[] = sArray(s);
for (int i = 0; i < sLen; i++){
int j = cText[i] - 65;
int l = (((j + k) % 26) + 65);
cText[i] = (char) l;
}
s = new String(cText);
return s;
}
// normalizes text (removes all spaces and special characters)
public static String normalizeText(String s) {
int sLen = s.length();
char[] t1 = s.toCharArray();
for (int i = 0; i < sLen; i++ ){
if(t1[i] < 'A' || t1[i] > 'z' || (t1[i] > 'Z' && t1[i] < 'a')) {
t1[i] = ' ';
}
else{
t1[i] = s.charAt(i);
}
}
String t = new String(t1);
t = t.replaceAll(" ", "" );
t = t.toUpperCase();
return t;
}
public static String groupify(int i , String s){
int sLen = s.length();
char[] t = new char[sLen];
for (int j = 0; j < s.length(); j++){
t[j] = s.charAt(j);
if ( j % i == 0) {
t[j] = ' ';
sLen++;
}
}
s = new String(t);
return s;
}
and this is the section in particular that i think is the issue
public static String groupify(int i , String s){
int sLen = s.length();
char[] t = new char[sLen];
for (int j = 0; j < s.length(); j++){
t[j] = s.charAt(j);
if ( j % i == 0) {
t[j] = ' ';
sLen++;
}
}
s = new String(t);
return s;
}
with this this if i input
Hi Im Doug
I get
JJ EP H
as output
Thanks a bunch
Currently your code t[i] += ' ' is adding the space character's value to value in the array. That's not what you want. Rather you want to be storing a space in next position. I also suggest that you use better names for your variable - single character variables should generally only be used for indexes.
int pos = 0;
for (int j = 0; j < input.length(); j++)
result[pos++] = input.charAt(j);
if ( j % group == 0) {
result[pos++] = ' ';
}
}
For the code You have highlighted you are missing curly brackets after the for Loop. Try to define j outside the for-loop and assign the value 0 to j in the for-loop. It would be a better practice if you Store the size of your new Char-Array in a separate variable as it is easier to read when finding bugs in the code.

Random character and number generator doesn't work in the way I want

import java.util.Random;
public class Test {
public static void main(String[] args) {
randomIDGenerator();
}
public static String randomIDGenerator() {
Random r = new Random();
char a = 0;
for (int i = 0; i < 3; i++) {
a = (char) (r.nextInt(26) + 'a');
}
int b = 0;
for (int j = 0; j < 2; j++) {
b = r.nextInt(10);
}
String h = "" + b;
String n = a + h;
System.out.println(n);
return n;
}
}
I am writing a code in Java and I want my output to be 3 characters from a to z and 2 numbers from 0 to 9 (e.g. abc01) but the program gives me 1 character and 1 number (e.g. a1). Why does the program do this despite I've put 3 and 2 into loops? From all I know the first loop must operate 3 times and the second one must operate 2 times. So at the end my output have to be 3 characters and 2 numbers. Thanks!
You generate your random items correctly, but you do not collect the results in a proper way. Each iteration of the loop writes over the results of prior iteration, leaving both a and b with the result of the last iteration.
I recommend using a StringBuilder to store characters as you generate them:
Random r = new Random();
StringBuilder res = new StringBuilder();
for (int i = 0; i < 3; i++) {
res.append((char)(r.nextInt(26) + 'a'));
}
for (int j = 0; j < 2; j++) {
res.append(r.nextInt(10)); // You could use the same trick with '0'
}
System.out.println(res);
Demo.
char and int can store one value so use String as
String a = "";
for (int i = 0; i < 3; i++) {
a += (char) (r.nextInt(26) + 'a');
}
for (int j = 0; j < 2; j++) {
a += r.nextInt(10);
}
return a;

java, how to find the amount of letter in a string without repeats

I'm trying to build a function, that gets a string of letters, and prints the amount of each letter in the string.
for example:
input: String = "aaabbaccxyxyx"
output: 4a2b2c3x2y
This is what I've come up with:
public class Q1 {
public static String numLetters(String s){
String end = new String();
int counter = 0;
char c,d;
for(int i=0; i<s.length();i++){
c = s.charAt(i);
for(int j=0; j<s.length();j++){
d = s.charAt(j);
if(c == d){
counter++;
}
}
end = end + counter+c;
counter = 0;
}
return end;
}
but, this is the output: 4a4a4a2b2b4a2c2c3x2y3x2y3x
A lot of repeats..
Any help how to make it right?
Keep in mind, the function needs to return a string, not just prints it out.
Thanks! =)
I would make an int array to keep the count of each letter in in the string. Because there are 26 letters, the length of the array should be 26:
public static String numLetters(String s) {
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
count[(int)(c - 'a')]++;
}
String ans = "";
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
ans += String.valueOf(count[i]) + (char)(i + 'a');
}
}
return ans;
}
A straightforward variant could look like this:
public static String countChars(String arg) {
String res = "";
boolean[] counted = new boolean[arg.length()];
for (int i = 0; i < counted.length; i++) {
if (!counted[i]) {
char c = arg.charAt(i);
int counter = 1;
for (int j = i + 1; j < counted.length; j++) {
if (arg.charAt(j) == c) {
counter++;
counted[j] = true;
}
}
res += counter + "" + c;
}
}
return res;
}
If you want to keep your original structure, I suggest using a StringBuilder so that you can delete characters that you have already seen. In case you delete a character, you have to adjust your indexes i and j.
public static String numLetters(String str){
StringBuilder s = new StringBuilder(s);
String end = new String();
int counter = 0;
char c,d;
for(int i=0; i<s.length();i++){
c = s.charAt(i);
for(int j=0; j<s.length();j++){
d = s.charAt(j);
if(c == d){
s.deleteCharAt(j);
if (i >= j) i--;
j--;
counter++;
}
}
end = end + counter+c;
counter = 0;
}
return end;
}
Try this:
int count = StringUtils.countMatches("a.b.c.d", ".");

Getting All Possibilities - Schoolwork

What I need to do is take a String array with each element having an exact length of 2, and find all possible combinations of the the elements, using each character within each String. By that I mean the String array {"Ss", "Ff"} returns "SF", "Sf", "sF", "sf". I have already tried a loop method that counts the iteration and then chooses a letter based on that, but it only works for arrays with a single element:
public String [] generatePossibilities(String [] s)
{
if(s[0].length() != 2)
throw new IllegalArgumentException();
String [] r = new String [s.length * 2];
for(int i = 0; i < r.length; i++)
{
r[i] = getPossibility(i, s);
}
return r;
}
private String getPossibility(int iteration, String [] source)
{
int [] choose = new int [source.length];
for(int i = 0; i < choose.length; i++)
{
choose[i] = 0;
}
for(int i = choose.length - 1; i >= 0; i--)
{
if(iteration < 1)
break;
choose[i] = 1;
iteration--;
}
String result = "";
for(int i = 0; i < source.length; i++)
result += source[i].substring(choose[i], choose[i] + 1);
return result;
}
Solved Thanks Sven!
public String [] generatePossibilities(String [] s)
{
if(s[0].length() != 2)
throw new IllegalArgumentException();
ArrayList<String> ra = new ArrayList<String>();
for(int i = s.length - 1; i >= 0; i--)
{
for(int j = 0; j < s[i].length(); j++)
{
String c = s[i].substring(j, j + 1);
if(ra.size() < 2)
{
ra.add(c);
}
else
{
for(int k = 0; k < ra.size(); k++)
{
String s1 = ra.get(k);
if(s1.substring(0, 1).equalsIgnoreCase(c))
continue;
else
{
s1 = c + s1;
ra.add(s1);
}
}
}
}
for(int j = 0; j < ra.size(); j++)
{
if(ra.get(j).length() != s.length - i)
{
ra.remove(j);
j--;
}
}
}
String [] r = new String [ra.size()];
for(int i = 0; i < r.length; i++)
{
r[i] = ra.get(i);
}
return r;
}
I would iterate the array of character tuples from last element to first. In each step you append to each current character the possibilities of the last iteration. You therefore double the elements in each step.
So for your example in the first iteration you have {Ff} and this would result to the two strings "F" and "f". In the next step you take each character of {Ss} and append each string of the last step to it getting "SF", "Sf", "sF" and "sf". You could then continue with further character tuples.

Categories

Resources