Rotating List and maintaining word number












0















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.










share|improve this question























  • 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


















0















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.










share|improve this question























  • 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
















0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 8:51









GrimbyGrimby

1




1













  • 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





















  • 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



















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














1 Answer
1






active

oldest

votes


















0














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". )






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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". )






share|improve this answer
























  • 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
















0














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". )






share|improve this answer
























  • 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














0












0








0







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". )






share|improve this answer













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". )







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy