• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collect and merge strings  RSS feed

 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to collect and merge when the string has a specific char at the end of it, lets say +. For example lets say we have an input of "hi+" ; and then whe have next input of "there+". I somehow need to collect these inputs and return these inputs when the last char of the string is "." . So the different inputs  of: hi+ , there+ somestring. should return hitheresomestring

I hope it's all clear

Thanks.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us what you have tried yourself to solve this problem, and explain exactly where you are stuck?
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Can you show us what you have tried yourself to solve this problem, and explain exactly where you are stuck?


Hi Jasper, thanks for quick reply. I don't want to post my whole code here as it is my coursework. I can PM for you to inspect it, but some general advice would be fine too.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Baba wrote:I don't want to post my whole code here as it is my coursework. I can PM for you to inspect it, but some general advice would be fine too.

If you don't want to post your code, a general description of your approach would be fine, too, although it would be much more helpful if you at least showed us some code.  Please remember, this site is Not A Code Mill and moderators do not answer questions like this via PM -- we're all here to share and share alike.
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Hans Baba wrote:I don't want to post my whole code here as it is my coursework. I can PM for you to inspect it, but some general advice would be fine too.

If you don't want to post your code, a general description of your approach would be fine, too, although it would be much more helpful if you at least showed us some code.  Please remember, this site is Not A Code Mill and moderators do not answer questions like this via PM -- we're all here to share and share alike.


I know that this site is  Not A Code Mill, thats why I only asked for some guidance and not posted any code.

This is what I tried so far(in english)
>if it ends with "+"
>create an arraylist
>add string into arraylist
>String joined = String.join("", myarray);
>message = joined;
>receive next input.
>
>else message=input  //don't forget that the input can either end with a "." or "+"



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for the sequence of the steps, the general approach seems fine. For example, are you really creating a List every time you get input that ends with "+"? Because that's what your pseudocode suggests. Why would you do that? Wouldn't you just create a list ONE time at the very start, then just keep adding strings to it?

Also, are you adding the strings, including the "+" and "." markers? Why? Do you know about the String.substring() method?

I don't see any mention of a problem or issue that you're encountering. Besides what the above questions imply, are there any other issues you're struggling with?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Baba wrote://don't forget that the input can either end with a "." or "+"

Reading the part I quoted it seems those: !, ?, ..., ; aren't possible in given to you sample data, but clarify it just in case.

Are you bounded to use Strings only? And why the ArrayList is needed in this app I don't get at the moment?
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is it always returns the last input only, no matter its ending.  And yes, I remove "+" or "." in a different method.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Baba wrote:The problem is it always returns the last input only, no matter its ending.  And yes, I remove "+" or "." in a different method.
Well, without showing us small method with does that we just shooting blindly by trying to guess where you wrong. Isn't a good approach.
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Reading the part I quoted it seems those:
!, ?, ..., ; aren't possible in given to you sample data
, but clarify it just in case.
yes, not possible.

Are you bounded to use Strings only? And
why the ArrayList is needed in this app I don't get at the moment?


I couldnt find anything else to collect the strings that ends with a "+". how would you hold them?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Baba wrote:I couldnt find anything else to collect
My question should have sounded: Why you need to collect them? or Why you can't build it up on the go and return it once you reach the point?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Baba wrote:The problem is it always returns the last input only, no matter its ending.  And yes, I remove "+" or "." in a different method.

So my reading of your pseudocode was probably right: you're creating a new List every time you encounter a string that has "+" -- don't do that. Create the list ONE TIME, prior to getting any input, then just keep adding your input strings to the same list.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid doing that sort of things with Strings, which are immutable. String has however a mutable counterpart: this. It is specially designed for appending Strings, or inserting parts or deleting parts, so it may be much better for what you want.
There are other classes for joining Strings, like this one, but I think that probably won't do what you need. I suspect the join method uses that in the background. Joining a String to your array will probably not give you the results you want. Are yo uusing an array or a List? If a List, why are you calling it myarray?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!