PrintWriter out = new PrintWriter(outputFile);
String result = "";
for (Orders o : allOrders) {
result += o + "\n";
out.print(result);
}
this is what i have so far and the result to the file should look like:
pizza
hamburger
sandwich
quesadilla
but when i ran my test cases it said that line 2 was actually "pizza", so basically I'm wondering if my code is somehow incorrect?
UPDATE: so it passed now i was forgetting out.close() lol
Here is your code:
String result = "";
for (Orders o : allOrders) {
result += o + "\n";
out.print(result);
}
At the end of the first loop, Result is pizza\\n. After the second time, it's pizza\\nhamburger\\n.
So you'll print:
pizza <-- from the first print
pizza <-- start of second print
hamburger
pizza <-- start of third print
hamburger
sandwich
pizza <-- start of fourth print
hamburger
sandwich
quesadilla
The problem is the +=. Change it to a simple = and see how that works.
You could simply do this:
PrintWriter out = new PrintWriter(outputFile);
for (Orders o : allOrders) {
out.print(o + "\n");
}
If you want two returns like your output that you gave shows, you can simply add another "\\n".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.