Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Concat

 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would like to concat few strings into one.

Below shows the code (as suggested yesterday in the previous post) of extracting certain values from string a.



and this is the output:

Cost,1= 10, 15, 12
Cost,3= 12, 11, 17
Cost,5= 15, 11, 16
Cost,7= 17, 16, 14
Cost,9= 13, 10, 12

I would like to make the above output into matrix, which has the format something like,
mat = 10 15 12
12 11 17
15 11 16
17 16 14
13 10 12

So in order for me to cast newStr3 into matrix, i thought of mapping it first into 1 string,
CostStr=10,15,12,12,11,17,15,11,16,17,16,14,13,10,12

but how can I concat newStr3 into CostStr?

I thought of doing,

String temp1 = newStr3;
temp = temp1.concat(","+ newStr3[r]);

but there's problem in the concat due to the different formatting (string and array) in temp and newStr3.

Is there any way of solving this problem??

Thanks in advance.
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karoline Lim wrote:So in order for me to cast newStr3 into matrix, i thought of mapping it first into 1 string,
CostStr=10,15,12,12,11,17,15,11,16,17,16,14,13,10,12...Is there any way of solving this problem??

There's always a way to solve a problem in programming (well, almost always) .

First off, there's quite a bit going on in your strings; you have a name, an occurrence number, an "=" sign, and then a whole bunch of values. My suggestion would be to split them up.

Just taking your "10,15,12,12,11,17,15,11,16,17,16,14,13,10,12" string though; I'd suggest having a look at String.split().
That will split all the individual values into a String array, which you can then use to create any matrix you like.

HIH

Winston
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the StringBuilder class; it is faster to invoke append() several times than use += on a String.
 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Karoline Lim wrote:So in order for me to cast newStr3 into matrix, i thought of mapping it first into 1 string,
CostStr=10,15,12,12,11,17,15,11,16,17,16,14,13,10,12...Is there any way of solving this problem??

There's always a way to solve a problem in programming (well, almost always) .

First off, there's quite a bit going on in your strings; you have a name, an occurrence number, an "=" sign, and then a whole bunch of values. My suggestion would be to split them up.

Just taking your "10,15,12,12,11,17,15,11,16,17,16,14,13,10,12" string though; I'd suggest having a look at String.split().
That will split all the individual values into a String array, which you can then use to create any matrix you like.

HIH

Winston


Thanks Winston, but I think I did not make my question clear enough sorry.

In my case, I've a .dat file that looks like,

st,1=3 st,2=5 st,3=4
a1,1=10 a1,2=15 a1,3=12
t1
a2,1=12 a2,2=11 a2,3=17
t1
a3,1=15 a3,2=11 a3,3=16
t1
a4,1=17 a4,2=16 a4,3=14
t1
a5,1=13 a5,2=10 a5,3=12
e,1=4 e,2=1 e,3=3

and then, I'm only interested to extract the an values, which are in row 1,3,5,7 and 9.

To do this, I used the method as suggested here yesterday,



and the results are,

Cost,1= 10, 15, 12
Cost,3= 12, 11, 17
Cost,5= 15, 11, 16
Cost,7= 17, 16, 14
Cost,9= 13, 10, 12

from here, I face problem of concatenating the costs (newStr3) into 10,15,12,12,11,17,15,...,12.
Even if I use stringBuilder as suggested by Campbell,




the outputs certainly will look like,

SB1 :10 15 12
SB2 :10 15 1210 15 12
SB1 :12 11 17
SB2 :12 11 1712 11 17
... etc

because it's concatenating the same newStr3 instead of newStr3[r]. I can't seem to change String newStr3 into newStr3[] during the value extraction process.

Hope i make myself clear.. sorry for the poor English.

 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karoline Lim wrote:...and the results are,

Cost,1= 10, 15, 12
Cost,3= 12, 11, 17
Cost,5= 15, 11, 16
Cost,7= 17, 16, 14
Cost,9= 13, 10, 12

Well first off, I'd get rid of all those spaces (at least for the str3 bit). Try and see if you can get:
Cost,1= 10,15,12
Cost,3= 12,11,17
Cost,5= 15,11,16...
instead.

Maybe something like this:Hopefully that will print out all your values, separated by commas.

Then you can use String.split(), as I said before.

Try the above first and see if you get what you want.

Winston

PS: I think you could also help yourself out by using more meaningful names. 'newStr3' doesn't convey much to me, except to say that it's probably a "3rd string field" of some sort. How about 'costValues'?
 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much Winston! It's just this



which I could not think of.. you are great! I've been looking at this prob. for the whole day string to char, to array, to matrix etc etc. in order for me to concatenate the newStr3. Thanks!

yea, I've changed the variables to the more meaning names. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic