Rotating List and maintaining word number
I'm at my wits end with this problem and would love some feedback.
I am currently trying to rotate a string to the right line by line (input coming from a file). The input could potentially have many lines, so the rotation needs to happen per line.
For example, rotating this to the right by 5
"and line breaks^$@ "
would end up being: "^$@ and line breaks"
Now for my code:
I'm working with a List and have done the following (pardon the random print lines, was debugging the old way :] ):
private static List<String> rflag(String value, List<String> lines) {
List<String> newLines = new ArrayList<>();
int rvalue = Integer.parseInt(value);
for (String line : lines) {
StringBuilder sb = new StringBuilder();
if (!line.isEmpty()) {
List <Character> chars= new ArrayList<>();
for(char ch: line.toCharArray()){
chars.add(ch);
}
Collections.rotate(chars, rvalue);
sb.append(chars);
// System.out.println("sb" + sb);
String text = sb.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim();
newLines.equals(text);
//System.out.println("nl" + newLines); //taking a look at output
//System.out.println("t-" + text); //taking a look at output
}
}
return newLines;
}
If I input something like "abcXYZ", my output ends up being [b, c, X, Y, Z, a] or bcXYZa with the removal of brackets and commas.
My main problem is that, while I can remove the brackets and commas, I'm not preserving the lines or words in an input. Anyone that can shoot me some ideas (or alternatives)? I feel like I may be overthinking this but I've spent quite a bit of time and have become frustrated :
Thanks in advanced.
java string arraylist rotation
add a comment |
I'm at my wits end with this problem and would love some feedback.
I am currently trying to rotate a string to the right line by line (input coming from a file). The input could potentially have many lines, so the rotation needs to happen per line.
For example, rotating this to the right by 5
"and line breaks^$@ "
would end up being: "^$@ and line breaks"
Now for my code:
I'm working with a List and have done the following (pardon the random print lines, was debugging the old way :] ):
private static List<String> rflag(String value, List<String> lines) {
List<String> newLines = new ArrayList<>();
int rvalue = Integer.parseInt(value);
for (String line : lines) {
StringBuilder sb = new StringBuilder();
if (!line.isEmpty()) {
List <Character> chars= new ArrayList<>();
for(char ch: line.toCharArray()){
chars.add(ch);
}
Collections.rotate(chars, rvalue);
sb.append(chars);
// System.out.println("sb" + sb);
String text = sb.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim();
newLines.equals(text);
//System.out.println("nl" + newLines); //taking a look at output
//System.out.println("t-" + text); //taking a look at output
}
}
return newLines;
}
If I input something like "abcXYZ", my output ends up being [b, c, X, Y, Z, a] or bcXYZa with the removal of brackets and commas.
My main problem is that, while I can remove the brackets and commas, I'm not preserving the lines or words in an input. Anyone that can shoot me some ideas (or alternatives)? I feel like I may be overthinking this but I've spent quite a bit of time and have become frustrated :
Thanks in advanced.
java string arraylist rotation
What is the purpose ofnewLines.equals(text);
?
– Arnaud
Nov 21 '18 at 8:53
I think I was just trying different things. Can be ignored. Originally I had it as newLines.add(sb.toString());
– Grimby
Nov 21 '18 at 8:57
1
@Grimby can you just provide your sample input and expected output and the output that you are getting?
– Nawnit Sen
Nov 21 '18 at 9:10
Sure. If the input is: "and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". So as you can see the rotation is correct, but the spacing is off.
– Grimby
Nov 21 '18 at 9:13
add a comment |
I'm at my wits end with this problem and would love some feedback.
I am currently trying to rotate a string to the right line by line (input coming from a file). The input could potentially have many lines, so the rotation needs to happen per line.
For example, rotating this to the right by 5
"and line breaks^$@ "
would end up being: "^$@ and line breaks"
Now for my code:
I'm working with a List and have done the following (pardon the random print lines, was debugging the old way :] ):
private static List<String> rflag(String value, List<String> lines) {
List<String> newLines = new ArrayList<>();
int rvalue = Integer.parseInt(value);
for (String line : lines) {
StringBuilder sb = new StringBuilder();
if (!line.isEmpty()) {
List <Character> chars= new ArrayList<>();
for(char ch: line.toCharArray()){
chars.add(ch);
}
Collections.rotate(chars, rvalue);
sb.append(chars);
// System.out.println("sb" + sb);
String text = sb.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim();
newLines.equals(text);
//System.out.println("nl" + newLines); //taking a look at output
//System.out.println("t-" + text); //taking a look at output
}
}
return newLines;
}
If I input something like "abcXYZ", my output ends up being [b, c, X, Y, Z, a] or bcXYZa with the removal of brackets and commas.
My main problem is that, while I can remove the brackets and commas, I'm not preserving the lines or words in an input. Anyone that can shoot me some ideas (or alternatives)? I feel like I may be overthinking this but I've spent quite a bit of time and have become frustrated :
Thanks in advanced.
java string arraylist rotation
I'm at my wits end with this problem and would love some feedback.
I am currently trying to rotate a string to the right line by line (input coming from a file). The input could potentially have many lines, so the rotation needs to happen per line.
For example, rotating this to the right by 5
"and line breaks^$@ "
would end up being: "^$@ and line breaks"
Now for my code:
I'm working with a List and have done the following (pardon the random print lines, was debugging the old way :] ):
private static List<String> rflag(String value, List<String> lines) {
List<String> newLines = new ArrayList<>();
int rvalue = Integer.parseInt(value);
for (String line : lines) {
StringBuilder sb = new StringBuilder();
if (!line.isEmpty()) {
List <Character> chars= new ArrayList<>();
for(char ch: line.toCharArray()){
chars.add(ch);
}
Collections.rotate(chars, rvalue);
sb.append(chars);
// System.out.println("sb" + sb);
String text = sb.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim();
newLines.equals(text);
//System.out.println("nl" + newLines); //taking a look at output
//System.out.println("t-" + text); //taking a look at output
}
}
return newLines;
}
If I input something like "abcXYZ", my output ends up being [b, c, X, Y, Z, a] or bcXYZa with the removal of brackets and commas.
My main problem is that, while I can remove the brackets and commas, I'm not preserving the lines or words in an input. Anyone that can shoot me some ideas (or alternatives)? I feel like I may be overthinking this but I've spent quite a bit of time and have become frustrated :
Thanks in advanced.
java string arraylist rotation
java string arraylist rotation
asked Nov 21 '18 at 8:51
GrimbyGrimby
1
1
What is the purpose ofnewLines.equals(text);
?
– Arnaud
Nov 21 '18 at 8:53
I think I was just trying different things. Can be ignored. Originally I had it as newLines.add(sb.toString());
– Grimby
Nov 21 '18 at 8:57
1
@Grimby can you just provide your sample input and expected output and the output that you are getting?
– Nawnit Sen
Nov 21 '18 at 9:10
Sure. If the input is: "and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". So as you can see the rotation is correct, but the spacing is off.
– Grimby
Nov 21 '18 at 9:13
add a comment |
What is the purpose ofnewLines.equals(text);
?
– Arnaud
Nov 21 '18 at 8:53
I think I was just trying different things. Can be ignored. Originally I had it as newLines.add(sb.toString());
– Grimby
Nov 21 '18 at 8:57
1
@Grimby can you just provide your sample input and expected output and the output that you are getting?
– Nawnit Sen
Nov 21 '18 at 9:10
Sure. If the input is: "and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". So as you can see the rotation is correct, but the spacing is off.
– Grimby
Nov 21 '18 at 9:13
What is the purpose of
newLines.equals(text);
?– Arnaud
Nov 21 '18 at 8:53
What is the purpose of
newLines.equals(text);
?– Arnaud
Nov 21 '18 at 8:53
I think I was just trying different things. Can be ignored. Originally I had it as newLines.add(sb.toString());
– Grimby
Nov 21 '18 at 8:57
I think I was just trying different things. Can be ignored. Originally I had it as newLines.add(sb.toString());
– Grimby
Nov 21 '18 at 8:57
1
1
@Grimby can you just provide your sample input and expected output and the output that you are getting?
– Nawnit Sen
Nov 21 '18 at 9:10
@Grimby can you just provide your sample input and expected output and the output that you are getting?
– Nawnit Sen
Nov 21 '18 at 9:10
Sure. If the input is: "and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". So as you can see the rotation is correct, but the spacing is off.
– Grimby
Nov 21 '18 at 9:13
Sure. If the input is: "and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". So as you can see the rotation is correct, but the spacing is off.
– Grimby
Nov 21 '18 at 9:13
add a comment |
1 Answer
1
active
oldest
votes
Changing
`.replace(",", "") //remove the commas`
to
`.replace(", ", "") //remove the commas`
makes this code work as You expect it.
`System.out.println(rflag("2", Arrays.asList("and line breaks")));//output: ksand line brea`
("and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". )
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408286%2frotating-list-character-and-maintaining-word-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Changing
`.replace(",", "") //remove the commas`
to
`.replace(", ", "") //remove the commas`
makes this code work as You expect it.
`System.out.println(rflag("2", Arrays.asList("and line breaks")));//output: ksand line brea`
("and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". )
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
add a comment |
Changing
`.replace(",", "") //remove the commas`
to
`.replace(", ", "") //remove the commas`
makes this code work as You expect it.
`System.out.println(rflag("2", Arrays.asList("and line breaks")));//output: ksand line brea`
("and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". )
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
add a comment |
Changing
`.replace(",", "") //remove the commas`
to
`.replace(", ", "") //remove the commas`
makes this code work as You expect it.
`System.out.println(rflag("2", Arrays.asList("and line breaks")));//output: ksand line brea`
("and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". )
Changing
`.replace(",", "") //remove the commas`
to
`.replace(", ", "") //remove the commas`
makes this code work as You expect it.
`System.out.println(rflag("2", Arrays.asList("and line breaks")));//output: ksand line brea`
("and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". )
answered Nov 21 '18 at 9:33
Alermikon Alermikon
214
214
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
add a comment |
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
hahaha i think this is it! i'll give it a go in a bit- it's late now so will try again in the morning :) Thanks lots!
– Grimby
Nov 21 '18 at 10:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408286%2frotating-list-character-and-maintaining-word-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the purpose of
newLines.equals(text);
?– Arnaud
Nov 21 '18 at 8:53
I think I was just trying different things. Can be ignored. Originally I had it as newLines.add(sb.toString());
– Grimby
Nov 21 '18 at 8:57
1
@Grimby can you just provide your sample input and expected output and the output that you are getting?
– Nawnit Sen
Nov 21 '18 at 9:10
Sure. If the input is: "and line breaks^$@" , the output currently is "^ $ @ a n d l i n e b r e a k s" but it should be "^$@ and line breaks". So as you can see the rotation is correct, but the spacing is off.
– Grimby
Nov 21 '18 at 9:13