Reputation: 253
I'm doing a project for Java 1, and I'm completely stuck on this question.
Basically I need to double each letter in a string.
"abc" -> "aabbcc"
"uk" -> "uukk"
"t" -> "tt"
I need to do it in a while loop in what is considered "Java 1" worthy. So i'm guessing that this means more of a problematic approach.
I know that the easiest way for me to do this, from my knowledge, would be using the charAt method in a while loop, but for some reason my mind can't figure out how to return the characters to another method as a string.
Thanks
[EDIT] My Code (wrong, but maybe this will help)
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index);
index++;
}
Upvotes: 1
Views: 19888
Reputation: 79550
In case you need to repeat the characters more than twice, the following code using String#repeat
be handy:
Arrays.stream(str.split(""))
.map(s -> s.repeat(2))
.collect(Collectors.joining())
e.g. changing the number to 10 in the above code will result in repeating each character 10 times.
Demo:
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String str = "abc";
System.out.println(Arrays.stream(str.split(""))
.map(s -> s.repeat(2))
.collect(Collectors.joining()));
}
}
Output:
aabbcc
Upvotes: 1
Reputation: 29710
Unicode capable - using code points instead of char
s:
public String doubleUnicode(String str) {
return str
.codePoints()
.mapToObj(Character::toString) // toString(int codePoint), not Object.toString()
.map(s -> s+s) // alternative s.repeat(2) 3,4,...
.collect(Collectors.joining());
}
Upvotes: 1
Reputation: 1
As the charAt will return the character the result will show you the numbers instead of the string. So instead of using the charAt we can use substring() to get the result as expected.
static String addString(String str)
{
String res="";
int i=0;
while(i<str.length())
{
res+=str.substring(i,i+1)+str.substring(i,i+1);
i++;
}
return res;
}
Upvotes: 0
Reputation: 298509
String s="mystring".replaceAll(".", "$0$0");
The method String.replaceAll
uses the regular expression syntax which is described in the documentation of the Pattern
class, where we can learn that .
matches “any character”. Within the replacement, $number refers to numbered “capturing group” whereas $0
is predefined as the entire match. So $0$0
refers to the matching character two times. As the name of the method suggests, it is performed for all matches, i.e. all characters.
Upvotes: 11
Reputation: 5637
Assuming this is inside a method, you should understand that you can only return once from a method. After encountering a return statement, the control goes back to the calling method. Thus your approach of returning char every time in a loop is faulty.
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index); // only the first return is reachable,other are not executed
index++;
}
Change your method to build a String and return it
public String modify(String str)
{
int index = 0;
int length = str.length();
String result="";
while (index < length) {
result += str.charAt[index]+str.charAt[index];
index++;
}
return result;
}
Upvotes: 0
Reputation: 55664
Yeah, a for loop would really make more sense here, but if you need to use a while loop then it would look like this:
String s = "abc";
String result = "";
int i = 0;
while (i < s.length()){
char c = s.charAt(i);
result = result + c + c;
i++;
}
Upvotes: 3
Reputation: 725
Your intuition is very good. charAt(i)
will return the character in the string at location i
, yes?
You also said you wanted to use a loop. A for
loop, traversing the length of the list, string.length()
, will allow you to do this. At every single node in the string, what do you need to do? Double the character.
Let's take a look at your code:
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index); //return ends the method
index++;
}
Problematically for your code, you are returning two characters immediately upon entering the loop. So for a string abc
, you are returning aa
. Let's store the aa
in memory instead, and then return the completed string like so:
int index = 0;
int length = str.length();
String newString = "";
while (index < length) {
newString += str.charAt(index) + str.charAt(index);
index++;
}
return newString;
This will add the character to newString
, allowing you to return the entire completed string, as opposed to a single set of doubled characters.
By the way, this may be easier to do as a for loop, condensing and clarifying your code. My personal solution (for a Java 1 class) would look something like this:
String newString = "";
for (int i = 0; i < str.length(); i++){
newString += str.charAt(i) + str.charAt(i);
}
return newString;
Hope this helps.
Upvotes: 2
Reputation: 3115
try this
String a = "abcd";
char[] aa = new char[a.length() * 2];
for(int i = 0, j = 0; j< a.length(); i+=2, j++){
aa[i] = a.charAt(j);
aa[i+1]= a.charAt(j);
}
System.out.println(aa);
Upvotes: 1
Reputation: 7213
public static char[] doubleChars(final char[] input) {
final char[] output = new char[input.length * 2];
for (int i = 0; i < input.length; i++) {
output[i] = input[i];
output[i + 1] = input[i];
}
return output;
}
Upvotes: 0
Reputation: 1176
You can do:
public void doubleString(String input) {
String output = "";
for (char c : input.toCharArray()) {
output += c + c;
}
System.out.println(output);
}
Upvotes: 2