This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
Extremely close to having this task finished but can't see which part of this is holding me back. If anybody could put me on the right track I'd be very thankful. the following is the error code that eclipse gives me each time I try to run this.
**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at lab01.EncodeDecode.backMap(EncodeDecode.java:162)
at lab01.EncodeDecode.Decode(EncodeDecode.java:68)
at lab01.EncodeDecode.(EncodeDecode.java:26)
at lab01.EncodeDecodeTester.main(EncodeDecodeTester.java:14)**
package lab01;
import java.util.*;
/**
*
* #author David Bierbrauer,
*
*/
public class EncodeDecode
{
//method declaration
static String[] originalList,encodedList,decodedList;
static int total;
public EncodeDecode(String[] oL)
{
//instance variable declaration
total = oL.length;
originalList = new String[total];
encodedList = new String[total];
decodedList = new String[total];
originalList = oL;
encodedList= Encode(originalList);
decodedList = Decode(encodedList);
}
public static String[] Encode (String[] originalList)
{
//declare control variables
String currentWord = "", codedWord = "";
char currentChar = ' ';
int i = 0, j = 0, stringLength = 0;
for (i=0; i < total ; i++)
{
currentWord = originalList[i];
stringLength = currentWord.length();
for (j = 0; j < stringLength; j++)
{
currentChar = currentWord.charAt(j);
codedWord = codedWord +forwardMap(currentChar);
}
encodedList[i] = codedWord;
codedWord = "";
}
return encodedList;
}
public static String[] Decode (String[] encodedList)
{
String currentWord = "", encodedWord = "";
char currentChar = ' ';
int i =0, j=0, stringLength = 0;
for(i = 0; i < total; i++)
{
currentWord = encodedList[i];
stringLength = currentWord.length();
for(j = 0; j < stringLength; j++)
{
currentChar = currentWord.charAt(j);
encodedWord = encodedWord + backMap(currentChar);
}
decodedList[i] = encodedWord;
encodedWord = "";
}
return decodedList;
}
public static char forwardMap(char currentChar)
{
char newChar = ' ';
int i = 0;
String encodeMapUpper = "CDEFGHIJKLMNOPQRSTUVWXYZAB";
String encodeMapLower = "cdefghijklmnopqrstuvwxyzab";
String encodeMapNumber = "2345678901";
char [] encodeArrayUpper = encodeMapUpper.toCharArray();
char [] encodeArrayLower = encodeMapLower.toCharArray();
char [] encodeArrayNumber = encodeMapNumber.toCharArray();
if(encodeMapUpper.indexOf(currentChar) != -1)
{
for( i = 0; i < encodeArrayUpper.length; i++)
{
if(currentChar == encodeArrayUpper[i])
{
newChar = encodeArrayUpper[(i+2) % 26];
}
}
}
else if(encodeMapLower.indexOf(currentChar) != -1)
{
for( i = 0; i < encodeArrayLower.length; i++)
{
if(currentChar == encodeArrayLower[i])
{
newChar = encodeArrayLower[(i+2) % 26];
}
}
}
else if(encodeMapNumber.indexOf(currentChar) != -1)
{
for( i = 0; i < encodeArrayNumber.length; i++)
{
if(currentChar == encodeArrayNumber[i])
{
newChar = encodeArrayNumber[(i+2) % 10];
}
}
}
else
{
//element is a special character
newChar = currentChar;
}
return newChar;
}
public static char backMap(char currentChar)
{
char newChar = ' ';
int i = 0;
String decodeMapUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String decodeMapLower = "abcdefghijklmnopqrstuvwxyz";
String decodeMapNumber = "0123456789";
char[] decodeArrayUpper = decodeMapUpper.toCharArray();
char[] decodeArrayLower = decodeMapLower.toCharArray();
char[] decodeArrayNumber = decodeMapNumber.toCharArray();
if (decodeMapUpper.indexOf(currentChar) != -1)
{
for (i=0; i < decodeArrayUpper.length; i++)
{
if (currentChar == decodeArrayUpper[i])
{
newChar = decodeArrayUpper[(i - 2) % 26];
}
}
}
else if(decodeMapLower.indexOf(currentChar) != -1)
{
for (i=0; i < decodeArrayLower.length; i++)
{
if (currentChar == decodeArrayLower[i])
{
newChar = decodeArrayLower[(i - 2) % 26];
}
}
}
else if(decodeMapNumber.indexOf(currentChar) != -1)
{
for (i=0; i < decodeArrayNumber.length; i++)
{
if (currentChar == decodeArrayNumber[i])
{
newChar = decodeArrayNumber[(i - 2) % 10];
}
}
}
else
{
newChar = currentChar;
}
return newChar;
}
//get methods
public String[] getEncodedList() { return encodedList;}
public String[] getDecodedList() { return decodedList;}
}
This is the tester class bellow just in case.
package lab01;
public class EncodeDecodeTester
{
public static void main(String[] args)
{
EncodeDecode testEncoder;
int x = 0;
String[] output = new String[5];
String[] oL = new String[] {"catdog","24","keys","Duck","PIZZA!"};
//create encoder
testEncoder = new EncodeDecode(oL);
System.out.println("Encoded list:");
for( x = 0; x < output.length; x++)
{
output = testEncoder.getEncodedList();
System.out.println(output[x]);
}
System.out.println();
System.out.println("Decoded List:");
for(x = 0; x < output.length; x++)
{
output = testEncoder.getDecodedList();
System.out.println(output[x] + " ");
}
System.out.println();
System.out.println("End");
}
}
Please help I am completely lost for words on what I did wrong here.
The Java % operator doesn't always produce a number between 0 and the second operand. Replace (i - 2) % 26 (which can produce -2) with (i + 24) % 26 and similarly in other places.
Related
I have the below String variable
String s = "abc,xyz,lmn,ijk";
I want to extract only the portion of the String (i.e - 'lmn')
And, Should not use in-built functions like, SubString(), Split(), IndexOf(). But I can use charArray()
And this question was asked in my interview.
I tried the below code,
But not sure how to proceed. Can any one please provide your thoughts?
String s = "abc,xyz,lmn,ijk";
int counter = 0;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] == ',') {
counter++;
}
}
Here's one way:
public static void main(String[] args) {
String s = "abc,xyz,lmn,ijk";
char[] ch = s.toCharArray();
int counter = 0;
int place = 2;
for (int i = 0; i < ch.length-2; i++) {
if(ch[i] == ',') {
counter++;
}
if(counter == place && ch[i] != ',') {
System.out.print(ch[i]);
}
}
}
It prints everything after the second comma, but before the third one.
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "abc,xyz,lmn,ijk";
StringBuffer sb=new StringBuffer();
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(','==(ch[i]))
{
if (sb.toString().equals("lmn")) {
System.out.println(sb.toString());
}
else
{
int length=sb.length();
sb.delete(0, length);
}
}
else
{
sb.append(ch[i]);
}
}
}
I would do it this way.
String s = "abc,xyz,lmn,ijk";
String x = "c,x"; // String to found
String r = "";
boolean coincidence = false;
int a=0; // Initial index if of the first character in x is found
int b=0; // Last index If it was possible to search for the last character of x
int c=0; // Index "iterator" on String x
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(c == x.length())
break;
else{
if(ch[i] == x.charAt(c) && !coincidence){
a = i; b = i; c++;
coincidence = true;
}
else if(ch[i] == x.charAt(c) && coincidence){
b++; c++;
}else{
coincidence = false;
a = 0; b = 0; c = 0;
}
}
}
System.out.println("String: " + s);
System.out.println("String to find: " + x);
System.out.println("Was found? " + ((coincidence)? "Yes" : "No"));
if(coincidence){
System.out.println("Intervals indexes in String: ["+a + "," + b +"]");
// String extration
for (int i = a; i <= b; i++)
r += s.charAt(i);
System.out.println("String extracted: " + r);
}
Using java, input string="aabbcdeaaaabbb" and the output must be aaaa, as sequence here is having repeated 4 times a. Can anyone help me to get this "aaaa" as output using java implementation.
Algorithm to find the Longest substring having same character repeated.
for eg:
I/P: aabbcdefaaaacccccc O/P: cccccc
Please check my program below and suggest any optimization for faster processing:
public class LongestSubString {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
System.out
.println("Enter a word to find longest substring with same characters repeated");
String word = reader.readLine();
System.out.println("Entered word is: " + word);
System.out.println("Longest repeated characters substring is: "
+ subStringFinder(word));
}
/*
*longest substring finder with same character repeated
*/
public static String subStringFinder(String word) {
char[] tokens = word.toCharArray();
int len = tokens.length;
int wordLen = word.length();
System.out.println("len of input word: " + wordLen);
List<String> myList = new ArrayList<>();
StringBuilder strConcat = new StringBuilder("");
for (int j = 0; j <= len - 1; j++) {
if (j + 1 > len - 1) {
if ((strConcat.length() >= 1)
&& (strConcat.charAt(strConcat.length() - 1) == (tokens[j]))) {
strConcat.append("" + tokens[j]);
myList.add(strConcat.toString());
}
}
else {
if (tokens[j] == tokens[j + 1]) {
if ((strConcat.length() >= 1)
&& (strConcat.charAt(strConcat.length() - 1) == (tokens[j]))) {
strConcat.append("" + tokens[j]);
myList.add(strConcat.toString());
} else {
strConcat = new StringBuilder("");
strConcat.append("" + tokens[j]);
}
} else {
if ((strConcat.length() >= 1)
&& (strConcat.charAt(strConcat.length() - 1) == (tokens[j]))) {
strConcat.append("" + tokens[j]);
myList.add(strConcat.toString());
} else {
strConcat = new StringBuilder("");
strConcat.append("" + tokens[j]);
}
}
}
}
int max = 0, index = 0;
for (int i = 0; i < myList.size(); i++) {
String strEle = myList.get(i);
int strLen = strEle.length();
if (max < strLen) {
max = strLen;
index = i;
}
}
return myList.get(index);
}
}
I believe your code is overly complicated. You don’t need a StringBuilder nor an ArrayList. I tried to understand your intention, but then skipped it and wrote my own version instead. Hope it helps anyway.
public static String subStringFinder(String word) {
if (word == null || word.isEmpty()) {
return word;
}
char currentChar = word.charAt(0);
int longestStart = 0;
int longestLength = 0;
int currentStart = 0;
int currentLength = 1;
for (int ix = 1; ix < word.length(); ix++) {
if (word.charAt(ix) == currentChar) {
currentLength++;
} else {
if (currentLength > longestLength) {
longestStart = currentStart;
longestLength = currentLength;
}
currentChar = word.charAt(ix);
currentStart = ix;
currentLength = 1;
}
}
if (currentLength > longestLength) {
longestStart = currentStart;
longestLength = currentLength;
}
return word.substring(longestStart, longestStart + longestLength);
}
String in = "aabbcdeaaaabbb";
String t;
ArrayList<String> out = new ArrayList<String>();
String l="";
int c=0;
String n;
for(int i=0;i<in.length;i++) {
n=in.substring(i, i+1); //get the current character
if(n.equals(l)){
l=n; c++;
}
else {
t=n;
for(int j=1;j<c;j++) {
n+=t;
}
c=0;
out.add(n);
c=0;
}
}
I want to shift each i in a given string one index to the right. How can I do that? For example:
"Chit Nyein Oo is nothing.";
becomes
"Chti Nyeni Oo si nothnig.";
If i occurs in the last index, it need not change its position.
Use string.replaceAll
string.replaceAll("i(.)", "$1i");
DEMO
EDIT: NOW it works for all conditions. Last letter in the String is 'i' or not, it works.
public class t4 {
public static void main(String[] args) {
String input = "Chit Nyein Oo is nothing.";
char o = 'i';
int indexes = 0;
if(input.charAt(input.length()-1) != 'i'){ //Test if last letter is not 'i'
for (int i = 0; i < input.length(); i++) {
if(input.charAt(i) == o){
indexes++;
}
}
int []positions = new int[indexes];
for (int i = 0; i < input.length(); i++) {
if(input.charAt(i) == o){
positions[indexes-1] = i;
indexes--;
}
}
char[] characters = input.toCharArray();
for (int i = 0; i < positions.length; i++) {
if(characters[input.length()-1] != 'i'){
char temp = characters[positions[i]];
characters[positions[i]] = characters[positions[i]+1];
characters[positions[i]+1] = temp;
} else {
continue;
}
}
String swappedString = new String(characters);
System.out.println(input);
System.out.println(swappedString);
} else { //so last letter is i
char t = input.charAt(input.length()-1);
String ha = input.substring(0, input.length()-1);
input = ha;
for (int i = 0; i < input.length(); i++) {
if(input.charAt(i) == o){
indexes++;
}
}
int []positions = new int[indexes];
for (int i = 0; i < input.length(); i++) {
if(input.charAt(i) == o){
positions[indexes-1] = i;
indexes--;
}
}
char[] characters = input.toCharArray();
for (int i = 0; i < positions.length; i++) {
if(characters[input.length()-1] != 'i'){
char temp = characters[positions[i]];
characters[positions[i]] = characters[positions[i]+1];
characters[positions[i]+1] = temp;
} else {
continue;
}
}
String swappedString = new String(characters);
swappedString = swappedString + Character.toString(t);
System.out.println(input);
System.out.println(swappedString);
}
}
}
You can do this using a StringBuilder.
class Test {
public static void main(String[] args) {
String input = "Chit Nyein Oo is nothingi";
int len = input.length();
StringBuilder sb = new StringBuilder();
// System.out.println(sb);
for(int i=0; i<len; i++) {
char charAti = input.charAt(i);
if(charAti == 'i' && i<len-1) {
sb.append(input.charAt(i+1));
sb.append(charAti);
i++;
}
else {
sb.append(charAti);
}
}
System.out.println(sb);
}
}
I have to do a RLE algorithm in java with the escape character (Q)
Example 1 if i have an input like:
77777 => 57
BBBBBBBBB => 10B
FBFB8923 => 004FBFB8923
2365553422 => 005236555342200
this is the code that i made:
public String coderRLE(string text) {
String res = new String();
char[] charArray = text.toCharArray();
char caractere = 0;
int num = 0;
int i = 0;
for (char c : charArray) {
if (c != caractere && i != 0) {
if (num >= 2) {
res += num;
res += caractere;
} else {
res += caractere;
}
num = 1;
} else {
num++;
}
caractere = c;
i++;
}
if (num >= 2) {
res += num;
res += caractere;
} else {
res += caractere;
}
return res;
}
public String decoderRLE(String text) {
String res = new String();
char[] charArray = text.toCharArray();
for (int i = 0;i<charArray.length-1;i++) {
char s = charArray[i];
if (!Character.isDigit(s)) {
res += s;
} else {
int num = Integer.parseInt(String.valueOf(s));
for (int j = 0; j < num - 1; j++) {
res += charArray[i+1];
}
}
}
return res;
}
the problem is with number like thisaaabbcccc666iii => aaabbcccc6633333ii
Try,
public static String encode(String source) {
StringBuffer dest = new StringBuffer();
for (int i = 0; i < source.length(); i++) {
int runLength = 1;
while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) {
runLength++;
i++;
}
dest.append(runLength);
dest.append(source.charAt(i));
}
return dest.toString();
}
if the input is aaabbcccc666iii it compress it as 3a2b4c363i
String example = "aaabbcccc666iii";
System.out.println(encode(example));
Output
3a2b4c363i
Given a string how can i figure out the number of times each char in a string repeats itself
ex: aaaabbaaDD
output: 4a2b2a2D
public static void Calc() {
Input();
int count = 1;
String compressed = "";
for (int i = 0; i < input.length(); i++) {
if (lastChar == input.charAt(i)) {
count++;
compressed += Integer.toString(count) + input.charAt(i);
}
else {
lastChar = input.charAt(i);
count = 1;
}
}
System.out.println(compressed);
}
What you'r looking for is "Run-length encoding". Here is the working code to do that;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RunLengthEncoding {
public static String encode(String source) {
StringBuffer dest = new StringBuffer();
// iterate through input string
// Iterate the string N no.of.times where N is size of the string to find run length for each character
for (int i = 0; i < source.length(); i++) {
// By default run Length for all character is one
int runLength = 1;
// Loop condition will break when it finds next character is different from previous character.
while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) {
runLength++;
i++;
}
dest.append(runLength);
dest.append(source.charAt(i));
}
return dest.toString();
}
public static String decode(String source) {
StringBuffer dest = new StringBuffer();
Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]");
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
int number = Integer.parseInt(matcher.group());
matcher.find();
while (number-- != 0) {
dest.append(matcher.group());
}
}
return dest.toString();
}
public static void main(String[] args) {
String example = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
System.out.println(encode(example));
System.out.println(decode("1W1B1W1B1W1B1W1B1W1B1W1B1W1B"));
}
}
This program first finds the unique characters or numbers in a string. It will then check the frequency of occurance.
This program considers capital and small case as different characters. You can modify it if required by using ignorecase method.
import java.io.*;
public class RunLength {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
System.out.println("Please enter the string");
String str = br.readLine();//the input string is in str
calculateFrequency(str);
}
private static void calculateFrequency(String str) {
int length = str.length();
String characters[] = new String[length];//to store all unique characters in string
int frequency[] = new int[length];//to store the frequency of the characters
for (int i = 0; i < length; i++) {
characters[i] = null;
frequency[i] = 0;
}
//To get unique characters
char temp;
String temporary;
int uniqueCount = 0;
for (int i = 0; i < length; i++) {
int flag = 0;
temp = str.charAt(i);
temporary = "" + temp;
for (int j = 0; j < length; j++) {
if (characters[j] != null && characters[j].equals(temporary)) {
flag = 1;
break;
}
}
if (flag == 0) {
characters[uniqueCount] = temporary;
uniqueCount++;
}
}
// To get the frequency of the characters
for(int i=0;i<length;i++){
temp=str.charAt(i);
temporary = ""+temp;
for(int j=0;i<characters.length;j++){
if(characters[j].equals(temporary)){
frequency[j]++;
break;
}
}
}
// To display the output
for (int i = 0; i < length; i++) {
if (characters[i] != null) {
System.out.println(characters[i]+" "+frequency[i]);
}
}
}}
Some hints: In your code sample you also need to reset count to 0 when the run ends (when you update lastChar). And you need to output the final run (after the loop is done). And you need some kind of else or continue between the two cases.
#Balarmurugan k's solution is better - but just by improving upon your code I came up with this -
String input = "aaaabbaaDD";
int count = 0;
char lastChar = 0;
int inputSize = input.length();
String output = "";
for (int i = 0; i < inputSize; i++) {
if (i == 0) {
lastChar = input.charAt(i);
count++;
} else {
if (lastChar == input.charAt(i)) {
count++;
} else {
output = output + count + "" + lastChar;
count = 1;
lastChar = input.charAt(i);
}
}
}
output = output + count + "" + lastChar;
System.out.println(output);