Im at a point in my program where I need to set marks in a string before I pass it through another method, I have it so that every 4th char will have a "|" inserted, this is to mark a row break. Not I want to take each char in between the outter marks "|" and put a ",". that two char array method wont work here otherwise I would have tryed to use that but I'm not looking for a char array.
public static String matrixFormatter(String x){
x = x.substring(0, 4) + "|" + x.substring(4, x.length());
return x;
}
this works so far, now I want to add a "," between each char, I thought the code below would work and this would be easy but I was wrong.
public static String matrixFormatter(String x){
for(int i = 0; i<=x.length(); i+=4){
for(int j = 0; j<=x.length(); i++){
x = x.substring(0, i) + "|" + x.substring(i, x.length());
x = x.substring(0, j) + "|" + x.substring(j, x.length());
}
}
return x;
}
The code below adds a "," between characters in the string.
public static String matrixFormatter(String x){
String result;
for(int i = 0; i<x.length()-1; i++){
result += x.substring(i, i+1) + ",";
}
return result+",";
}
try this regex
s = s.replaceAll("(?<=.)(?=.)", ",");
It can be done with StringBuffer and also with Joiner of Guava library:
public static void main(String[] args) {
String s = "example";
System.out.println(withBuilder(s));
System.out.println(withJoiner(s));
}
private static String withJoiner(String s) {
return Joiner.on(",").join(Chars.asList(s.toCharArray()));
}
private static String withBuilder(String s)
{
StringBuilder builder = new StringBuilder(s);
int index = 1;
for (int i = 0; i < s.length() ; i++)
{
builder.insert(index, ",");
index +=2;
}
return builder.toString();
}
Output is:
e,x,a,m,p,l,e,
e,x,a,m,p,l,e
It can be done in one method:
public static String matrixFormatter(String x) {
List<String> chars = Arrays.asList(x.split(""));
String result = chars.get(0);
for (int i = 1; i < chars.size(); i++) {
if (i % 4 == 0)
result += "|" + chars.get(i);
else
result += "," + chars.get(i);
}
return result;
}
calling with:
System.out.println(matrixFormatter("12345678"));
outputs:
1,2,3,4|5,6,7,8
public static String matrixFormatter(String x) {
resultstr = "";
int i = 0;
while(i < x.length()) {
// If end of string: only add character.
if (i == x.length() - 1) {
resultstr += x.substring(i, i + 1);
} else {
if ( ((i + 1) % 4) == 0) {
resultstr += x.substring(i, i + 1) + "|";
} else {
resultstr += x.substring(i, i + 1) + ",";
}
}
i++;
}
return resultstr;
}
Haven't got Java installed but tested the concept via PHP-code:
function matrixFormatter($x) {
$resultstr = "";
$i = 0;
while($i < strlen($x)) {
if ($i == strlen($x) - 1) {
$resultstr .= $x[$i];
} else {
if ( (($i + 1) % 4) == 0) {
$resultstr .= $x[$i] . "|";
} else {
$resultstr .= $x[$i] . ",";
}
}
$i++;
}
return $resultstr;
}
matrixFormatter("abcdefghijklmnopq") returns "a,b,c,d|e,f,g,h|i,j,k,l|m,n,o,p|q".
I'm not sure I understand you question correctly, you should probably add some input and expected output to be more clearer.
String a = "abcdefghijklmnop";
String a2 = "";
for (int i = 0; i < a.length(); i++) {
if (i != 0) {
if(i % 4 == 0){
a2 += "|";
} else{
a2 += ",";
}
}
a2 += a.charAt(i);
}
System.out.println(a2);
This will produce the output a,b,c,d|e,f,g,h|i,j,k,l|m,n,o,p
Related
I'm trying to display the number of times a letter appears within a string and outputting it in a new string (compressedString).
For example: aabcccccaaa should display a2b1c5a3.
So far, I got a2 to display only because I've included the break statement. If I took that out, then I would get StringIndexOutOfBoundsException.
My question is: How would I continue going through the whole string to obtain the rest of the aforementioned output without getting StringIndexOutOfBoundsException?
I ran it through debugger but it still isn't clear to me.
public class Problem {
public static void main(String []args) {
String str = "aabcccccaaa";
System.out.println(compressBad(str));
}
public static String compressBad(String str) {
int countConsecutive = 0;
String compressedString = "";
for(int i = 0; i < str.length(); i++) {
countConsecutive++;
if(str.charAt(i) != str.charAt(i + 1)) {
compressedString += "" + str.charAt(i) + countConsecutive;
break;
}
}
return compressedString;
}
}
modify your for loop to terminate when i < str.length() - 1--this is because you are comparing the character at i to the character at i + 1, which makes your loop go out of bounds.
Try this
public class Problem {
public static void main(String []args) {
String str = "aaabc";
System.out.println(compressBad(str));
}
public static String compressBad(String str) {
int countConsecutive = 0;
String compressedString = "";
for(int i = 0; i < str.length(); i++) {
countConsecutive++;
//avoid index out of bounds error
if(str.length() == (i + 1)){
compressedString += ""+ str.charAt(i) + countConsecutive;
countConsecutive = 0;
break;
}
else if(str.charAt(i) != str.charAt(i + 1)){
compressedString += ""+ str.charAt(i) + countConsecutive;
countConsecutive = 0;
}
}
return compressedString;
}
}
The other answers have good solutions, but I thought I would just add what I came up with:
public class Problem {
public static void main(String []args) {
String str = "aabcccccaaa";
System.out.println(compressBad(str));
}
public static String compressBad(String str) {
if (str.length() == 1) return str + "1"; // Handles single character strings
int countConsecutive = 0;
String compressedString = "";
for (int i = 0; i < str.length(); i++) {
if (i > 0) {
countConsecutive++;
if (str.charAt(i) != str.charAt(i-1)) {
compressedString += "" + str.charAt(i-1) + countConsecutive;
countConsecutive = 0;
}
if (i == str.length()-1) {
countConsecutive++; // Needs to be incremented for the last character
compressedString += "" + str.charAt(i) + countConsecutive;
}
}
}
return compressedString;
}
}
Your condition should be like this:
if(i+1 < str.length() && str.charAt(i) != str.charAt(i + 1))
because when you are is at last index of your string then also you are comparing i'th index with i+1 th index.
But after correcting this, still, this code will not give you the expected output.
This is how I would change the code.
public static String compressBad(String str) {
String compressedString = "";
if (str != null && str.length() > 0) {
int countConsecutive = 1;
char prevChar = str.charAt(0);
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) != prevChar) {
// End of a run. Update compressedString and reset counters
compressedString += String.valueOf(prevChar) + countConsecutive;
prevChar = str.charAt(i);
countConsecutive = 1;
continue;
}
countConsecutive++;
}
compressedString += String.valueOf(prevChar) + countConsecutive;
}
return compressedString;
}
Mukit09 has already mentioned the reason for your StringIndexOutOfBoundsException.
I offer you a more efficient implementation, using String Builder for concatenating strings:
private static String comppressedString(String str) {
if(str == null || str.equals("")) {
return str;
}
if(str.length() == 1) {
return str + "1";
}
StringBuilder sb = new StringBuilder();
sb.append(str.charAt(0)); // Add first letter
int j = 1; // Counter for current sequence length.
for (int i = 0; i < str.length() - 1; i++) {
if(str.charAt(i) != str.charAt(i + 1)) { // end of characters sequence.
sb.append(j); // Add length of previous sequence.
if(j > 1) {
j = 1; // Minimum sequence length is 1
}
sb.append(str.charAt(i+1)); // Add character of next sequence.
} else {
j++; // increase counter, in order to get the length of the current sequence.
}
}
sb.append(j); // Add length of last sequence.
return sb.toString();
}
public static void main(String[] args) {
System.out.println(comppressedString("")); // empty string
System.out.println(comppressedString("a")); // a1
System.out.println(comppressedString("ab")); // a1b1
System.out.println(comppressedString("abba")); // a1b2a1
System.out.println(comppressedString("aabcccccaaa")); // a2b1c5a3
}
please for help
I need retrurn in 'String dst;' the same result as console
e.g. input txt = "aabbc"; which gives return dst = "a2b2c1"
public String compres(String txt) {
String dst = "";
char character;
int count;
for (int i = 0; i < txt.length(); i++) {
character = txt.charAt(i);
count = 1;
while (i < txt.length() - 1 && txt.charAt(i + 1) == character) {
count++;
i++;
}
System.out.print(character);
System.out.print(count);
}
return dst;
}
Use StringBuilder.
public String compres(String txt) {
StringBuilder dst = new StringBuilder();
char character;
int count;
for (int i = 0; i < txt.length(); i++) {
character = txt.charAt(i);
count = 1;
while (i < txt.length() - 1 && txt.charAt(i + 1) == character) {
count++;
i++;
}
System.out.print(character);
System.out.print(count);
dst.append(character).append(count);
}
return dst.toString();
}
today my mate gave me easiest sollution i need add only in my loop:
dst += character + "" + count;
instead of double sysout
I am trying to print a polynomial from a given number.
I did the example below, but for something like 100 it will print 1x^2+, when I want just x^2. What I'm looking for is how can I make it to not print + and at the same time get rid of coefficients that are 1.
Edit: I did it, it prints perfectly. Feel free to use it.
private static String S_frumos(int poli) {
String s = "";
for (int i = 0; i < String.valueOf(poli).length(); i++) {
int nr = Character.getNumericValue(S_GetCoefs(poli, i));
if (nr != 0) {
if (i == String.valueOf(poli).length() - 1) {
s = s + nr;
} else if (i == String.valueOf(poli).length() - 2) {
if ((S_zero(poli, i + 1) == 1)) {
if (nr != 1) {
s = s + nr + "x";
} else {
s = s + "x";
}
} else {
if (nr != 1) {
s = s + nr + "x" + "+";
} else {
s = s + "x" + "+";
}
}
} else if ((S_zero(poli, i + 1) == 1)) {
if (nr != 1) { s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1);}
else { s = s + "x^" + (String.valueOf(poli).length() - i - 1);}
} else {
if (nr != 1){ s = s + nr + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
else { s = s + "x^" + (String.valueOf(poli).length() - i - 1) + "+";}
}
}
}
return s;
}
private static int S_GetCoefs(int poli, int x) {
return String.valueOf(java.lang.Math.abs(poli)).charAt(x);
}
To store something of an unknown length... then you can still use an int/double array, just gets slightly more complicated.
public static void main(String[] args)
{
// Say the size is given in a command line argument.
int coefficientNumber = Integer.parseInt(args[0]);
int[] poly = new int[coefficientNumber];
for (int i = 0; i < poly.length; i++)
{
poly[i] = 0;
}
// Set the highest coeffient to 1 (if there is 3 coefficients, this is coefficient
// of x^2, if 4 coefficients, this is coefficient of x^3
poly[0] = 1;
printPoly(poly);
}
// To print a polynomial of unknown length.
// If the coefficient is 0, don't print it.
private static void printPoly(int[] poly)
{
String output = "";
for (int index = 0; index < poly.length; index++)
{
if (poly[index] != 0)
{
// If this is the first coefficient with a value
if (output.length() == 0)
output = poly[index] + "x^" + (poly.length - (index + 1));
// Else if there is already some coefficient with values printed.
else
output += " + " + "x^" + (poly.length - (index + 1));
} // if
} // for
System.out.println(output);
} // printPoly
First of all, storing a polynomial in one variable isn't a great idea as if you have coefficients of more than 9 you'll get confused.
A better method imo (without making a polynomial class) is to store the polynomial in an int/double array.
public static void main(String[] args)
{
// To store the polynomial x^2, you could do the following:
int[] poly = new int[3];
poly[0] = 1;
poly[1] = 0;
poly[2] = 0;
printPoly(poly);
}
// To print it:
private static void printPoly(int[] poly)
{
String output = "";
if (poly[0] != 0)
output += poly[0] + "x^2"
if (poly[1] != 0)
{
if (output.size() > 0)
output += " + " + poly[1] + "^x";
else
output += poly[1] + "x";
}
if (poly[2] != 0)
{
if (output.size() > 0)
output += " + " + poly[2];
else
output += poly[2];
}
}
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length.
strDist("catcowcat", "cat") → 9
strDist("catcowcat", "cow") → 3
strDist("cccatcowcatxx", "cat") → 9
my solution
public int strDist(String str, String sub) {
int i = sub.length();
int j = str.length();
int count = 0;
if (str.length() == 1 && str.equals(sub)) {
return 1;
} else if (str.length() < sub.length() || str.length() <= 1) {
return 0;
}
if (str.substring(0, i).equals(sub)) {
if (str.substring(str.length() - i, str.length()).equals(sub)) {
return str.length();
} else {
strDist(str.substring(0, str.length() - i), sub);
}
} else {
strDist(str.substring(1, str.length()), sub);
}
return 0;
}
tell me how to correct my code?
Why does this need to be done with recursion?
Edit: fixed code to handle case where sub is not present in str, or only present once.
public int strDist(String str, String sub) {
int last=str.lastIndexOf(sub);
if (last != -1) {
int first=str.indexOf(sub);
if (first != last)
return last - first + sub.length();
}
}
return 0;
}
Recursion is great, if it is suited to the problem. In this case, recursion doesn't add value, and writing it with recursion for the sake of recursion makes the code inefficient.
This will , "compute recursively the largest substring which starts and ends with sub and return its length" as you described.
public class PuzzlingRecursion {
static String substringFound = "";
public static void main(String[] args) {
String sentence = "catcowcat";
String substring = "cat";
int sizeString = findNumberOfStrings(sentence, substring, 0);
System.out.println("you are searching for: " + substring);
System.out.println("in: " + sentence);
System.out.println("substring which starts and ends with sub and return its length is:"+substringFound + ", " + sizeString);
}
private static int findNumberOfStrings(String subStringPassed,
String setenecePassed, int count) {
if (subStringPassed.length() == 0) {
return count + 0;
}
if (subStringPassed.length() < setenecePassed.length()) {
return count + 0;
}
count++;
String lastStringMiddle = subStringPassed.replaceAll("(.*?)" + "("
+ setenecePassed + ")" + "(.*?)" + "(" + setenecePassed + ")"
+ "(.*?.*)", "$3");
if (subStringPassed.contains(setenecePassed)
&& lastStringMiddle.length() != setenecePassed.length()) {
if (subStringPassed.contains(setenecePassed)
&& lastStringMiddle.contains(setenecePassed)) {
// only found one item no pattern but according to the example
// you posted it should return the length of one word/substring
count = setenecePassed.length();
substringFound = subStringPassed;
return count;
}
}
// makesure the lastSrtringMiddle has the key we are search
if (!lastStringMiddle.equals(subStringPassed)) {
subStringPassed = subStringPassed.replaceFirst(setenecePassed, "");
String lastString = subStringPassed.substring(0,
subStringPassed.lastIndexOf(setenecePassed));
if (null != lastString && !"".equals(lastString)) {
count = lastStringMiddle.length() + setenecePassed.length()
+ setenecePassed.length();
substringFound = setenecePassed + lastStringMiddle
+ setenecePassed;
subStringPassed = "";
}
return findNumberOfStrings(subStringPassed, setenecePassed, count);
}
return count;
}
}
I think this is much nicer recursive solution:
public int strDist(String str, String sub) {
if (str.length()==0) return 0;
if (!str.startsWith(sub))
return strDist(str.substring(1),sub);
if (!str.endsWith(sub))
return strDist(str.substring(0,str.length()-1),sub);
return str.length();
}
This is my code.
public static void countContinuosOccurence() {
String first = "ABBCDDDEFGGH";
StringBuffer result = new StringBuffer();
int count = 1;
for (int i = 1; i < first.length(); i++) {
if (first.charAt(i) == (first.charAt(i - 1))) {
count++;
} else {
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(i - 1));
} else {
result.append(first.charAt(i - 1));
}
count = 1;
}
}
System.out.println("First String is:"+ first);
System.out.println("Result is:" + result);
}
The result is:
First String is:ABBCDDDEFGGH
Result is:A2BC3DEF2G
It is missing the last character? May someone help me to solve this?
Not top-performing, but simplest code:
final String in = "ABBCDDDEFGGH" + '\u0000';
final StringBuilder b = new StringBuilder();
char prev = in.charAt(0);
int rpt = 0;
for (int i = 1; i < in.length(); i++) {
final char curr = in.charAt(i);
if (curr == prev) rpt++;
else {
b.append(rpt == 0? prev : "" + (rpt + 1) + prev);
rpt = 0; prev = curr;
}
}
System.out.println(b);
After the for loop ends, you'll need to append the count and the character of the last run of character(s) to the result:
public static void countContinuosOccurence() {
String first = "ABBCDDDEFGGH";
StringBuffer result = new StringBuffer();
int count = 1;
int i;
for (i = 1; i < first.length(); i++) {
if (first.charAt(i) == (first.charAt(i - 1))) {
count++;
} else {
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(i - 1));
} else {
result.append(first.charAt(i - 1));
}
count = 1;
}
}
// ADD THIS - to take care of the last run.
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(i - 1));
} else {
result.append(first.charAt(i - 1));
}
System.out.println("First String is:"+ first);
System.out.println("Result is:" + result);
}
public static void countContinuosOccurence() {
String[] input = "ABBCDDDEFGGH".split("");
String out = "";
for (int i = 0; i < input.length; i++) {
int repeatedCharCount = 1;
String currentChr = input[i];
if (!(i == input.length - 1)) {
while (input[i].equals(input[i + 1])) {
repeatedCharCount++;
i++;
}
}
out = out + repeatedCharCount + currentChr;
}
System.out.println(out);
}
There is also a hidden problem, that is that if you are terminating with a sequence with more than one occurrence, you will not write anything.
The simplest way to solve this problem and the problem you detected is to add a final check after the for block
[...]
}
int l = first.length();
if (count > 1) {
result.append(String.valueOf(count) + first.charAt(l - 1));
} else {
result.append(first.charAt(l - 1));
}
}
System.out.println("First String is:"+ first);
System.out.println("Result is:" + result);
}
import java.util.*;
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
String first = "ABBCDDDEFGGHhhhhh456456456{{{67}}}";
StringBuffer result = new StringBuffer();
result.append(first);
System.out.println(result);
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i = 0; i < first.length(); i++) {
char c = first.charAt(i);
if (map.containsKey(c)) {
int cnt = map.get(c);
map.put(c, ++cnt);
} else {
map.put(c, 1);
}
}
Set set = map.entrySet();
// Get an iterator
Iterator itr = set.iterator();
// Display elements
while(itr.hasNext()) {
Map.Entry me = (Map.Entry)itr.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println("Hello World1");
}
}