#Dividing a string into x rows and y columns
74 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @haughty tulip! Please use
/closeor theClose Postbutton 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.
do you want to split them into separate strings as a String[] or a single string with newlines
you split the string in x parts
you split each part in y parts
i was thinking a single string, is that a bad idea?
would an array make sense here or do i not need it?
you would need to add newlines every y characters and then truncate off the rest i guess
both make sense for different situations
Do you want all cells to have the same length?
and what do you want to do with the result?
it's meant to be a grid of characters rather than a grid of strings
im sorry what do u mean by cell?
an element in the table
@dapper mist
theres no table, am i missing smth?
I used "table" as "anything with rows and columns"
you didn't give adequate detail
u asked, and i said its just one long string
i guess this is the way to go
????
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
What do you want to do with the result? How many columns and rows?
Is it one character per element or multiple characters?
the amount of rows and columns will be given to the method
yes
.
Oh also: What have you tried so far?
and you still haven't said what you want to do with the result
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
i want to generate n random characters, lets say 1000, and then i want to search for a keyword
so you just want to split the string into multiple lines of equal size?
and see if that keyword is in that string
yea pretty much
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
And you want to generate the data randomly on the fly?
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
yep
"c = (char) (rnd.nextInt(26) + 'a');" basically this
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?
yea thats pretty much what i did, right?
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?
ah ok
do u see my problem? hope i worded that somewhat properly
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?
declaration: module: java.base, package: java.lang, class: StringBuilder
it's just a modifiable String
bascially
well, you would create new strings over and over
so is this similar to an array of strings?
anyways i think this works, imma play around with it a bit
A StringBuilder is similar to a single String but you can modify it
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.