Exercise 6.34 Java Write a StringInfo method public void replace (char lo, char hi): The executor replaces every instance of the char value lo by the char value hi and makes no other change in itself.
method code: the way I started it: public void replace (char lo, char hi) { for (int k =0; k < itself.length(); k++) { if (
Here is the book chapter 6 I'm using
okay
um
supposed u have a char like 'heyholomoholoho'
how do you think I should continue the if statement? The exercise is specifically asking for executor to replace every instance of the char value lo by the char value hi
do you know how to see the first char in that expression
okay, but what do I do next?
I don't think I know how to see the first char. how do you see it?
what if I write this: if (itself.charAt (k) == lo) can that be a good start?
@dan815 what do you say?
u are going to have to look up the syntax and update me
but the implementation should be simple, You have checking 2 characters at a time and replacing them with lo
so you do if (itself(index)==l) { if (itself(index+1)==o) { itself(index)=h itself(index+1)=i } }
when both of the if statments are met then it changes
and it only goes to the 2nd ifstatement if it finds an l
see if there is a string slicing method
then u can do if (itself(index:index+2)==lo) { itself(index:index+2)=hi }
I have this so far: for (int k =0; k < itself.length(); k++) { if (itself.charAt (k) == lo) { if (itself.charAt (k) + 1 == 0) { itself.charAt (k) = hi; } } }
Do you think I did this right?
i dont know im not too familiar with C syntax
why dont u do some tests
i don't know the language of C. I know just java. :)
^^
To quote you Elle: ``` if (itself.charAt (k) + 1 == 0) { itself.charAt (k) = hi; } ``` the charAt(int); of the String class returns a char variable.. I'm not quite sure what you're trying to accomplish with ``` if(itself.charAt(k) + 1 == 0) ``` However it's possible to cast a char variable to an int into ascii; where each char has a corresponding int. While I know this is off topic - never hurts to learn more. ``` System.out.print("\'A\' Ascii equivalent = " + ((int)'A') + "\n\'B\' Ascii equivalent = " + ((int)'B') + "\n\'Z\' Ascii equivalent = " + ((int)'Z')); ``` would have a console output of: ``` 'A' Ascii equivalent = 65 'B' Ascii equivalent = 66 'Z' Ascii equivalent = 90 ``` And the part of code you have inside all the if statements, intending it to replace the lo char in the String with a hi char is not acting how you might intend. You should throw these things into the compiler to make sure they work first; and if not, ask why something is syntactically incorrect. ``` itself.charAt (k) = hi; ``` The charAt(k) will return a char at index k of the itself String.. so essentially this is doing this: ``` char hi = 'z'; 'r' = hi; ``` You can't use the assignment operator ( = ) without a variable to store the information. It's the same thing as doing: ``` 2 = 5; ``` If you want to replace the character in the String object, you can utilize the substring method, which can isolate parts of a string; there are 2 main helpful overloaded substring methods, one takes (int), and the other takes (int, int) arguments. The (int x) argument one, will start at index 'x', and cut off everything before it -- so ``` String sally = "wub wub wub"; sally = sally.substring(4); System.out.print(sallly); ``` will have a console output of: ``` wub wub ``` note that the argument is inclusive -- though it acts (as many many things do in java) with 0 being equal to the first character in the String. Now with the 2nd overloaded method; the first argument is the same, and the 2nd argument specifies where to cut it off (note that the 2nd argument is exclusive) - so ``` String g55 = "thejavawizard > harrypotter"; System.out.print(g55.substring(7, 14) + "\n"); g55 = g55.substring(7, 14).concat(g55.substring(16)); System.out.print(g55); ``` the 7 points to the 'w' in wizard, the 14 points to the '>', but because the 2nd argument is exclusive, it will not include the '>'. I introduce the concat() method of the String class, this method very simply 'concatenates' -- appends 2 Strings together. So it is "wizard".concat("harrypotter"); The console output would be: ``` wizard wizard harrypotter ``` If you wanted to replace a character, you could use the substring method to do this... or you could also learn the StringBuilder class, which is good for manipulating Strings. Though for the purpose of this program you need to write - @vish_fab has an efficient, and simple algorithm for this purpose.
@alprincenofl
Java strings are immutable (unchangeable or unmodifiable) objects. This have a rather serious consequence which is that all mutating operations on a string will result in (return) a new string and not manipulate the old one. Because of this we know it'll be much faster to replace all necessary characters in one go, only reading the characters in the original string once. @vish_fab is on to this but I would refrain from calling the implementation efficient. This is because of a common trap in most programming languages - string concatenation. Each time we concatenate two string we have to copy all chars in both strings to a new array. The problem is best illustrated by an example ```java String hw = "Hello World!"; String result = ""; for (int i=0; i<hw.length(); i++) result += hw.charAt(i); ``` is equal to ```java String hw = "Hello World!"; String result; result = ""+"H"; // Copy 2 chars result = "H"+"e"; // Copy 2 chars result = "He"+"l"; // Copy 3 chars result = "Hel"+"l"; // Copy 4 chars ... // ... result = "Hello World"+"!"; // Copy 12 chars ``` In total we copy 1 + (1 + 2 + 3 + 4 + ... + 12) chars, which can be expressed as \[1 + \sum_{k=1}^{12} k\]12 is the length of the string "Hello World!" and if repeat this with any other string we'll find that the last number in the sum always equals length of the string. We can now generalize and call the length N, the amount of read operations to copy a string will then be equal to \[1 + (1 + 2 + 3 + ... + N) = 1 + \sum_{k=1}^{N} k = \frac{ 1 }{ 2 } N^2 + \frac{ 1 }{ 2 } N + 1\]A input of N characters will result in a quadratic growth of the number of chars copied. This is not very effective. The solution is to use a StringBuilder ```java String hw = "Hello World!"; StrngBuilder sb = new StringBuilder(hw.length()); for (int i=0; i<hw.length(); i++) sb.append(hw.charAt(i)); ``` StringBuilder is similar to a mutable string and uses an internal buffer to store all data making it very fast. The argument passed to the constructor tells the StringBuilder how long it should make the buffer initially. This isn't necessary but it will make sure the StringBuilder doesn't have to do a lot of internal copying to increment the buffer size.
tl;dr Always use StringBuilder (in some cases StringBuffer) to manipulate strings in java. String concatenation is very ineffective.
Wow, powerful knowledge. Thanks for sharing.
Thank You @Lyrae , @woodrow73 , and everyone else. :) Elle
Join our real-time social learning platform and learn together with your friends!