printing area's of an array on different lines depending on section

so, I've been trying to figure out how to write different parts of an array out to different lines of a txt file for a project that i've been working on. I have tried multiple methods but it usually writes all of the info to one line. I am hoping to print out the different parts of the array without going in and writing out every single variable of the array that I want to have on each line. I can provide the code if needbe to make things easier.
59 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @Royalrex25! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant 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.
Royalrex25
Royalrex25OP3w ago
here is my code to help with potentially figuring out my issue
JavaBot
JavaBot3w ago
119ms
dan1st
dan1st3w ago
What exactly is the issue there? What exactly is written in a single line?
Royalrex25
Royalrex25OP3w ago
so, I need the names to be on one line, the passwords on the next, the item names on the line below the passwords, and the prices to be on the line below the items but everything gets printed on one line per customer. each customer is seperated properly, but I need the sections mentioned above to be printed on 4 seperate lines do you want the output txt file?
dan1st
dan1st3w ago
So you want all names of all users in one line? instead of one line for each user?
Royalrex25
Royalrex25OP3w ago
no, I want the users seperated the way they are but each section of each user to be on a seperate line per person: Name Passwords Items Costs but I want the users to stay seperated
dan1st
dan1st3w ago
So there can be multiple names for a user - what should happen in that case?
Royalrex25
Royalrex25OP3w ago
I mean with the username, Name, and Last Name on the same line I made sure that no user would have multiple names
dan1st
dan1st3w ago
What is writer.write(names.trim().replaceAll(", $", "") + "\n"); supposed to do?
Royalrex25
Royalrex25OP3w ago
it's supposed to write the next section to a new line but it didn't work
dan1st
dan1st3w ago
Are multiple sections written in a single line?
Royalrex25
Royalrex25OP3w ago
yep, but it's supposed to be one section per line
dan1st
dan1st3w ago
Can you try printing names via System.out.println()? Does that print the correct thing?
Royalrex25
Royalrex25OP3w ago
well, its supposed to print to a txt file
dan1st
dan1st3w ago
I mean for testing If you just print names, does the print the right thing in its own line? Or does it maybe print names and also the other stuff in a single line yes I think that makes sense
Royalrex25
Royalrex25OP3w ago
it doesn't print anything out
dan1st
dan1st3w ago
nothing or an empty line?
Royalrex25
Royalrex25OP3w ago
heres the txt file
Royalrex25
Royalrex25OP3w ago
empty line
dan1st
dan1st3w ago
Can you try
System.out.println("names: "+names);
System.out.println("passwds: "+passRecord);
System.out.println("items: "+itemRecord);
System.out.println("prices: "+priceRecord);
System.out.println("names: "+names);
System.out.println("passwds: "+passRecord);
System.out.println("items: "+itemRecord);
System.out.println("prices: "+priceRecord);
for testing Is that the input or the output?
Royalrex25
Royalrex25OP3w ago
output
dan1st
dan1st3w ago
oh I see
Royalrex25
Royalrex25OP3w ago
and the code you asked me to put in only prints the word but nothing else
dan1st
dan1st3w ago
So you are iterating over the customer info the first entry is Username: JennyH
Royalrex25
Royalrex25OP3w ago
yep, but it only prints out the word specified in the console
dan1st
dan1st3w ago
but that isn't equal to anything so you just add it to names then you encounter FirstName: Jenny but it also doesn't equal anything so you add it to names etc
Royalrex25
Royalrex25OP3w ago
gotta go, bot I'll continue later thanks for the help so far
dan1st
dan1st3w ago
What you can do is split each of the Strings by : then the first element is the entry name and the second element is the value
Royalrex25
Royalrex25OP3w ago
how would that work in terms of printing everything out?
dan1st
dan1st3w ago
you can still print it out as you are currently doing it I don't think printing is the issue
Royalrex25
Royalrex25OP3w ago
then what do you think it is?
dan1st
dan1st3w ago
how you are getting the variables you are printing
Royalrex25
Royalrex25OP3w ago
i coded it so that the program prints the string
dan1st
dan1st3w ago
for (String info : customerInfo) {
String[] split = info.split(": ");
if(split.length > 1){
String value = split[1];
switch(split[0]){
case "Username" -> names += value;
case "PassRecord" -> passRecord += value;
case "ItemRecord" -> itemRecord += value;
case "PriceRecord" -> priceRecord += value;
default -> {};
}
}
}
writer.write(names+"\n");
writer.write(passRecord+"\n");
writer.write(itemRecord+"\n");
writer.write(priceRecord+"\n");
for (String info : customerInfo) {
String[] split = info.split(": ");
if(split.length > 1){
String value = split[1];
switch(split[0]){
case "Username" -> names += value;
case "PassRecord" -> passRecord += value;
case "ItemRecord" -> itemRecord += value;
case "PriceRecord" -> priceRecord += value;
default -> {};
}
}
}
writer.write(names+"\n");
writer.write(passRecord+"\n");
writer.write(itemRecord+"\n");
writer.write(priceRecord+"\n");
maybe you want to do something alone the lines of that?
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Royalrex25
Royalrex25OP3w ago
where in the code would I place it?
dan1st
dan1st3w ago
where you previously had the same loops and write the output
Royalrex25
Royalrex25OP3w ago
cool, thanks for the help, ill try it out now
dan1st
dan1st3w ago
btw that's just an example of what it could look like
Royalrex25
Royalrex25OP3w ago
wdym "just an example"? and the "default -> {};" line errors out
dan1st
dan1st3w ago
idk whether that's exactly what you want I think you can just do default -> "";
Royalrex25
Royalrex25OP3w ago
give me an error saying it's not a statement
dan1st
dan1st3w ago
ok then you can just delete that line
Royalrex25
Royalrex25OP3w ago
will it break anything if I do?
dan1st
dan1st3w ago
no
Royalrex25
Royalrex25OP3w ago
so, now it just prints the first entry
Royalrex25
Royalrex25OP3w ago
Here is the txt
dan1st
dan1st3w ago
oh these are multiple entries after each other that's why you did that
Royalrex25
Royalrex25OP3w ago
yeah
dan1st
dan1st3w ago
I think that one was fine except it should have been if (info.equals("PassRecord: ")) { (note the additional space) and also for the others
Royalrex25
Royalrex25OP3w ago
wait, which section of the code was this for again?? I forgot yeah, it's like that in the code u there?
dan1st
dan1st3w ago
but you forgot a space in your code
Royalrex25
Royalrex25OP3w ago
where?
dan1st
dan1st3w ago
like here before the " and also for the others
Royalrex25
Royalrex25OP3w ago
ohh, now I see what you meant IT WORKS!!!! THANK YOU!!!!!!!
JavaBot
JavaBot3w ago
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.
Royalrex25
Royalrex25OP3w ago
you have no idea how long it took me to get that working Thank you so much!!!
JavaBot
JavaBot3w ago
Post Closed
This post has been closed by <@595765455436644367>.
Want results from more Discord servers?
Add your server