#Dividing a string into x rows and y columns

74 messages · Page 1 of 1 (latest)

haughty tulip
#

How would one go about dividing a string (lets say its 1000 characters long) into x amount of rows and y amount of columns? The string contains randomly generated characters

compact sequoiaBOT
#

This post has been reserved for your question.

Hey @haughty tulip! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

half hollow
#

do you want to split them into separate strings as a String[] or a single string with newlines

dapper mist
#

you split the string in x parts
you split each part in y parts

haughty tulip
#

would an array make sense here or do i not need it?

half hollow
#

you would need to add newlines every y characters and then truncate off the rest i guess

half hollow
dapper mist
#

Do you want all cells to have the same length?

#

and what do you want to do with the result?

half hollow
#

it's meant to be a grid of characters rather than a grid of strings

haughty tulip
dapper mist
haughty tulip
#

theres no table, am i missing smth?

dapper mist
#

I used "table" as "anything with rows and columns"

half hollow
#

you didn't give adequate detail

haughty tulip
haughty tulip
half hollow
#

how is that related

#

you didn't give adequate detail overall

#

that's why dan's going in a different direction

#

also are you just going to truncate chars beyond x*y

dapper mist
#

What do you want to do with the result? How many columns and rows?

#

Is it one character per element or multiple characters?

haughty tulip
#

the amount of rows and columns will be given to the method

haughty tulip
#

yes

dapper mist
#

Oh also: What have you tried so far?

haughty tulip
#

ehm

#

i had a for loop within another one

#

hold up

dapper mist
#

and you still haven't said what you want to do with the result

haughty tulip
#

for (int i = 0; i < amountOfLines; i++) {
for (int j = 0; j < charsPerLine; j++) {
c = (char) (rnd.nextInt(26) + 'a');
generatedString = generatedString + c;
writer.write(c);
}
writer.write("\n");
}

#

had this

haughty tulip
dapper mist
#

so you just want to split the string into multiple lines of equal size?

haughty tulip
#

and see if that keyword is in that string

haughty tulip
#

i had this running, but then i wanted to add the option to find that keyword in that string, and make it uppercase

#

so if lets say the keyword "abc" is in the string, it replaces it and writes "ABC" instead

dapper mist
#

And you want to generate the data randomly on the fly?

haughty tulip
#

but this was hard to add with my code because i was instantly writing that string into a file, so i scratched that and created the string first and THEN made it write to a file, so i can (if i choose to) can edit the string before printing it, if that makes sense

haughty tulip
#

"c = (char) (rnd.nextInt(26) + 'a');" basically this

dapper mist
#
for(int i=0;i<rows;i++){
  for(int j=0;j<columns;j++){
    writer.write((char) (rnd.nextInt(26) + 'a'));
  }
  writer.write('\n');
}
#

you mean something like that?

haughty tulip
#

yea thats pretty much what i did, right?

dapper mist
#

yes but without the generatedString

#

What do you want differently?

haughty tulip
#

but if i were to do it like this

#

the issue with this is

#

if i wanted to replace the keyword in that file, i would have to go through the whole string again

#

so its smarter to look for the keyword first inside the array, edit it, and then print it, right?

dapper mist
#

ah ok

haughty tulip
#

do u see my problem? hope i worded that somewhat properly

dapper mist
#
StringBuilder lineBuilder=new StringBuilder(columns);
for(int i=0;i<rows;i++){
  for(int j=0;j<columns;j++){
    lineBuilder.append((char) (rnd.nextInt(26) + 'a'));
  }
  writer.write(lineBuilder.toString().replace(yourKeyword, targetText));
  lineBuilder.setLength(0)
  writer.write('\n');
}
#

like this?

haughty tulip
#

i.. think?

#

gotta google stringbuilder lmao

dapper mist
#

it's just a modifiable String

#

bascially

haughty tulip
#

but why is it needed/better? a normal string can do the same, no?

#

ohhh

dapper mist
#

well, you would create new strings over and over

haughty tulip
#

so is this similar to an array of strings?

#

anyways i think this works, imma play around with it a bit

dapper mist
#

A StringBuilder is similar to a single String but you can modify it

haughty tulip
#

thanks! 🙂

compact sequoiaBOT
# haughty tulip thanks! 🙂

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.