How about:
String input = "TestInput";
StringBuilder b = new StringBuilder();
b.append(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
b.append("*").append(input.charAt(i));
}
System.out.println(b);
}
gives:
T*e*s*t*I*n*p*u*t
Is this what you wanted?
Edit:
Pshemo's suggestion - use StringJoiner (Java 8 solution)
StringJoiner sj = new StringJoiner("*");
for (int i = 0; i < input.length(); i++) {
sj.add(input.substring(i, i + 1));
}
System.out.println(sj.toString());
And no StringBuilder version:
String input="TestInput";
String y = "";
for (int i = 0; i < input.length(); i++) {
y += "*" + input.charAt(i);
}
And to your knowlage - using string concatenation is discouraged. It is better to use StringBuilder or StringJoiner
How about:
String input = "TestInput";
StringBuilder b = new StringBuilder();
b.append(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
b.append("*").append(input.charAt(i));
}
System.out.println(b);
}
gives:
T*e*s*t*I*n*p*u*t
Is this what you wanted?
Edit:
Pshemo's suggestion - use StringJoiner (Java 8 solution)
StringJoiner sj = new StringJoiner("*");
for (int i = 0; i < input.length(); i++) {
sj.add(input.substring(i, i + 1));
}
System.out.println(sj.toString());
And no StringBuilder version:
String input="TestInput";
String y = "";
for (int i = 0; i < input.length(); i++) {
y += "*" + input.charAt(i);
}
And to your knowlage - using string concatenation is discouraged. It is better to use StringBuilder or StringJoiner
In Java 8+ you can do the following
String x = "Hello";
String result = Stream.of(x.split(""))
.collect(Collectors.joining("*"));
Result H*e*l*l*o.
Basically, you are creating a stream of Strings with length 1 and collecting them using a Collector that joins them using *. In this approach you don't have to consider any spacial cases like the last/first element or remove extra * from end etc..
You could do this in a number of ways, for example...
You could attack the problem head on, for example...
StringBuilder sb = new StringBuilder(truck);
for (int index = 0; index < length; index++) {
sb.append(0, " ");
}
truck = sb.toString();
You could write a method which produced an "empty" String
public String createPath(int length) {
StringBuilder sb = new StringBuilder(length);
for (int index = 0; index < length; index++) {
sb.append(" ");
}
return sb.toString();
}
Then simply append the result, for example...
truck = createPath(n) + truck;
Or you could be really tricky and simply use String.format...
public String createPath(int length) {
return String.format("%" + length + "s", "");
}
Without going into the nuances of capturing keypresses or any of that, you presumably just want a while loop to continue to "drive" your truck until the user quits (or whatever). A very simple one might look like this:
String truck = "<o><o>-<o>~|#|¬";
int count = 0;
while(true) { // This is your loop
int n = 1; // or capture some user input, etc...
// Then "move" the truck
StringBuilder sb = new StringBuilder();
for(int i=0; i<n+count; i++) {
sb.append(" ");
}
sb.append(truck);
System.out.println(sb.toString());
count = count + n;
}
beginner here, how do i create a string by adding up chars?
How to repeatedly add characters to a String Java - Stack Overflow
Concatenating a string in Java within a loop
foreach - Looping through a list to add characters to a list of Strings in Java - Stack Overflow
Add a loop to print z spaces before each character at z. Something like,
String str = "Compile";
for (int z = 0; z < str.length(); z++) {
char ans = str.charAt(z);
for (int x = 0; x < z; x++) {
System.out.print(" ");
}
System.out.println(ans);
}
Try this.
String str = "Compile";
String spaces = "";
for (int z = 0; z < str.length(); z++) {
char ans = str.charAt(x);
System.out.println(spaces + str.charAt(z));
spaces += " ";
}
Hi All,
I am trying to understand the reason why the following code executes in quadratic time
String s = "";
for (int i = 0; i < n; ++i) {
s = s + n;
}
I understand that strings are immutable and therefore each execution of s = s + n creates a new instance of string but I am still not sure what the issue is that makes it quadratic. So, I understand there will be n string creations, but is it specifically the concatenation operator? because we are saying "take all contents of s, and add n to the end" and since we can not mutate s, copying all elements of s + appending into a new string is required?
When you change s in the inner loop, you're just re-assigning the variable s, not the value stored in your linked list. Instead, you should loop through all the elements in your list updating them one by one. I think something like this should work:
//use recursion to add paths from all children to the list
for (TreeNode t : childList){
paths.addAll(t.findStrings());
//add nodevalue to the beginning of all strings in the list
int length = paths.size();
for (int i=0; i<length; i++) {
paths.offer(nodevalue + paths.poll());
}
}
poll takes the first item from the front of the list, and offer puts the result on the back. You take the first item off, change it, then put it on the back—repeat paths.size() times and you end up with the updated items in the original order.
String is an immutable type the assignment
s = nodevalue + s;
is not regognized
A better solution should be
for (TreeNode t : childList){
final List<String> pathes = t.findStrings();
for (final String path : pathes) {
// add all pathes to paths list adding nodevalue to the beginning
paths.add(nodevalue + path);
}
}
The easiest way to for-each every char in a String is to use toCharArray():
for (char ch: "xyz".toCharArray()) {
}
This gives you the conciseness of for-each construct, but unfortunately String (which is immutable) must perform a defensive copy to generate the char[] (which is mutable), so there is some cost penalty.
From the documentation:
[
toCharArray()returns] a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
There are more verbose ways of iterating over characters in an array (regular for loop, CharacterIterator, etc) but if you're willing to pay the cost toCharArray() for-each is the most concise.
String s = "xyz";
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
}
I am struggling to wrap my head around the for each loop and how it can be applied through iteration of a defined string.
I need to step through the string character by character and for each character use a char variable to hold the value of that character. The issue is that I am not sure how to do this.
Please can someone explain this in layman's terms or provide an example of how I can achieve this?