How do I have the entered data go into the next line of a text file





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a text file that has the date, name, description and amount.



10/10/2018, Gel, Hair Product, 2000.00


With the code I made, the user can enter a new line into the original text file:



public static void recordExpense(String filename)throws IOException{
String expense = "";
String date = "";
String description = "";
double amount = 0.0;
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)));
date = readDate(); //user enters the date
expense = readExpense(); //user enters the name of the expense
description = readDescription(); // user enters the description of the expense
amount = readAmount(); .//user enters how much it costs
pw.println(date+", "+expense+", "+description+", "+amount);
pw.close();
}catch (Exception e){
System.out.println("The file could not be found");
}
}


Instead of having the expected output like:



10/10/2018, Gel, Hair Product, 2000.00
11/10/2018, Comb, Stuff, 20.00


It would come out like this instead:



10/10/2018, Gel, Hair Product, 2000.0011/10/2018, Comb, Stuff, 20.00


How do I fix this?










share|improve this question























  • So you want it to come out like the second example, with all of it in a straight line?

    – Ishaan Javali
    Nov 24 '18 at 14:47











  • No he wants in the first

    – Themelis
    Nov 24 '18 at 14:48











  • Nope, I need it to be the first example.

    – Joey Deguzman
    Nov 24 '18 at 14:48











  • Your program is working and println() seems to failed to create a new line, try print(). Create a constant private static final String newline = System.getProperty("line.separator");and use pw.print(date+", "+expense+", "+description+", "+amount+newLine);.

    – Themelis
    Nov 24 '18 at 14:50











  • If the file already exists, and you can't touch it, you'll have to read it first to check if it contains a blank line at the end or not. And then add the missing blank line first if necessary.

    – JB Nizet
    Nov 24 '18 at 14:52


















0















I have a text file that has the date, name, description and amount.



10/10/2018, Gel, Hair Product, 2000.00


With the code I made, the user can enter a new line into the original text file:



public static void recordExpense(String filename)throws IOException{
String expense = "";
String date = "";
String description = "";
double amount = 0.0;
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)));
date = readDate(); //user enters the date
expense = readExpense(); //user enters the name of the expense
description = readDescription(); // user enters the description of the expense
amount = readAmount(); .//user enters how much it costs
pw.println(date+", "+expense+", "+description+", "+amount);
pw.close();
}catch (Exception e){
System.out.println("The file could not be found");
}
}


Instead of having the expected output like:



10/10/2018, Gel, Hair Product, 2000.00
11/10/2018, Comb, Stuff, 20.00


It would come out like this instead:



10/10/2018, Gel, Hair Product, 2000.0011/10/2018, Comb, Stuff, 20.00


How do I fix this?










share|improve this question























  • So you want it to come out like the second example, with all of it in a straight line?

    – Ishaan Javali
    Nov 24 '18 at 14:47











  • No he wants in the first

    – Themelis
    Nov 24 '18 at 14:48











  • Nope, I need it to be the first example.

    – Joey Deguzman
    Nov 24 '18 at 14:48











  • Your program is working and println() seems to failed to create a new line, try print(). Create a constant private static final String newline = System.getProperty("line.separator");and use pw.print(date+", "+expense+", "+description+", "+amount+newLine);.

    – Themelis
    Nov 24 '18 at 14:50











  • If the file already exists, and you can't touch it, you'll have to read it first to check if it contains a blank line at the end or not. And then add the missing blank line first if necessary.

    – JB Nizet
    Nov 24 '18 at 14:52














0












0








0








I have a text file that has the date, name, description and amount.



10/10/2018, Gel, Hair Product, 2000.00


With the code I made, the user can enter a new line into the original text file:



public static void recordExpense(String filename)throws IOException{
String expense = "";
String date = "";
String description = "";
double amount = 0.0;
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)));
date = readDate(); //user enters the date
expense = readExpense(); //user enters the name of the expense
description = readDescription(); // user enters the description of the expense
amount = readAmount(); .//user enters how much it costs
pw.println(date+", "+expense+", "+description+", "+amount);
pw.close();
}catch (Exception e){
System.out.println("The file could not be found");
}
}


Instead of having the expected output like:



10/10/2018, Gel, Hair Product, 2000.00
11/10/2018, Comb, Stuff, 20.00


It would come out like this instead:



10/10/2018, Gel, Hair Product, 2000.0011/10/2018, Comb, Stuff, 20.00


How do I fix this?










share|improve this question














I have a text file that has the date, name, description and amount.



10/10/2018, Gel, Hair Product, 2000.00


With the code I made, the user can enter a new line into the original text file:



public static void recordExpense(String filename)throws IOException{
String expense = "";
String date = "";
String description = "";
double amount = 0.0;
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)));
date = readDate(); //user enters the date
expense = readExpense(); //user enters the name of the expense
description = readDescription(); // user enters the description of the expense
amount = readAmount(); .//user enters how much it costs
pw.println(date+", "+expense+", "+description+", "+amount);
pw.close();
}catch (Exception e){
System.out.println("The file could not be found");
}
}


Instead of having the expected output like:



10/10/2018, Gel, Hair Product, 2000.00
11/10/2018, Comb, Stuff, 20.00


It would come out like this instead:



10/10/2018, Gel, Hair Product, 2000.0011/10/2018, Comb, Stuff, 20.00


How do I fix this?







java text






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 14:44









Joey DeguzmanJoey Deguzman

427




427













  • So you want it to come out like the second example, with all of it in a straight line?

    – Ishaan Javali
    Nov 24 '18 at 14:47











  • No he wants in the first

    – Themelis
    Nov 24 '18 at 14:48











  • Nope, I need it to be the first example.

    – Joey Deguzman
    Nov 24 '18 at 14:48











  • Your program is working and println() seems to failed to create a new line, try print(). Create a constant private static final String newline = System.getProperty("line.separator");and use pw.print(date+", "+expense+", "+description+", "+amount+newLine);.

    – Themelis
    Nov 24 '18 at 14:50











  • If the file already exists, and you can't touch it, you'll have to read it first to check if it contains a blank line at the end or not. And then add the missing blank line first if necessary.

    – JB Nizet
    Nov 24 '18 at 14:52



















  • So you want it to come out like the second example, with all of it in a straight line?

    – Ishaan Javali
    Nov 24 '18 at 14:47











  • No he wants in the first

    – Themelis
    Nov 24 '18 at 14:48











  • Nope, I need it to be the first example.

    – Joey Deguzman
    Nov 24 '18 at 14:48











  • Your program is working and println() seems to failed to create a new line, try print(). Create a constant private static final String newline = System.getProperty("line.separator");and use pw.print(date+", "+expense+", "+description+", "+amount+newLine);.

    – Themelis
    Nov 24 '18 at 14:50











  • If the file already exists, and you can't touch it, you'll have to read it first to check if it contains a blank line at the end or not. And then add the missing blank line first if necessary.

    – JB Nizet
    Nov 24 '18 at 14:52

















So you want it to come out like the second example, with all of it in a straight line?

– Ishaan Javali
Nov 24 '18 at 14:47





So you want it to come out like the second example, with all of it in a straight line?

– Ishaan Javali
Nov 24 '18 at 14:47













No he wants in the first

– Themelis
Nov 24 '18 at 14:48





No he wants in the first

– Themelis
Nov 24 '18 at 14:48













Nope, I need it to be the first example.

– Joey Deguzman
Nov 24 '18 at 14:48





Nope, I need it to be the first example.

– Joey Deguzman
Nov 24 '18 at 14:48













Your program is working and println() seems to failed to create a new line, try print(). Create a constant private static final String newline = System.getProperty("line.separator");and use pw.print(date+", "+expense+", "+description+", "+amount+newLine);.

– Themelis
Nov 24 '18 at 14:50





Your program is working and println() seems to failed to create a new line, try print(). Create a constant private static final String newline = System.getProperty("line.separator");and use pw.print(date+", "+expense+", "+description+", "+amount+newLine);.

– Themelis
Nov 24 '18 at 14:50













If the file already exists, and you can't touch it, you'll have to read it first to check if it contains a blank line at the end or not. And then add the missing blank line first if necessary.

– JB Nizet
Nov 24 '18 at 14:52





If the file already exists, and you can't touch it, you'll have to read it first to check if it contains a blank line at the end or not. And then add the missing blank line first if necessary.

– JB Nizet
Nov 24 '18 at 14:52












1 Answer
1






active

oldest

votes


















0














Your file obviously does not end with a newline. If you want to add a new text on a new line, you need to print this newline first and then print the text.



Replace:



pw.println(date+", "+expense+", "+description+", "+amount);


With:



pw.print(System.lineSeparator()+date+", "+expense+", "+description+", "+amount);





share|improve this answer





















  • 1





    That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

    – JB Nizet
    Nov 24 '18 at 14:50













  • @JBNizet My mistake, fixed.

    – Martin Heralecký
    Nov 24 '18 at 14:52












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459294%2fhow-do-i-have-the-entered-data-go-into-the-next-line-of-a-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your file obviously does not end with a newline. If you want to add a new text on a new line, you need to print this newline first and then print the text.



Replace:



pw.println(date+", "+expense+", "+description+", "+amount);


With:



pw.print(System.lineSeparator()+date+", "+expense+", "+description+", "+amount);





share|improve this answer





















  • 1





    That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

    – JB Nizet
    Nov 24 '18 at 14:50













  • @JBNizet My mistake, fixed.

    – Martin Heralecký
    Nov 24 '18 at 14:52
















0














Your file obviously does not end with a newline. If you want to add a new text on a new line, you need to print this newline first and then print the text.



Replace:



pw.println(date+", "+expense+", "+description+", "+amount);


With:



pw.print(System.lineSeparator()+date+", "+expense+", "+description+", "+amount);





share|improve this answer





















  • 1





    That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

    – JB Nizet
    Nov 24 '18 at 14:50













  • @JBNizet My mistake, fixed.

    – Martin Heralecký
    Nov 24 '18 at 14:52














0












0








0







Your file obviously does not end with a newline. If you want to add a new text on a new line, you need to print this newline first and then print the text.



Replace:



pw.println(date+", "+expense+", "+description+", "+amount);


With:



pw.print(System.lineSeparator()+date+", "+expense+", "+description+", "+amount);





share|improve this answer















Your file obviously does not end with a newline. If you want to add a new text on a new line, you need to print this newline first and then print the text.



Replace:



pw.println(date+", "+expense+", "+description+", "+amount);


With:



pw.print(System.lineSeparator()+date+", "+expense+", "+description+", "+amount);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 14:51

























answered Nov 24 '18 at 14:49









Martin HeraleckýMartin Heralecký

3,19921135




3,19921135








  • 1





    That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

    – JB Nizet
    Nov 24 '18 at 14:50













  • @JBNizet My mistake, fixed.

    – Martin Heralecký
    Nov 24 '18 at 14:52














  • 1





    That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

    – JB Nizet
    Nov 24 '18 at 14:50













  • @JBNizet My mistake, fixed.

    – Martin Heralecký
    Nov 24 '18 at 14:52








1




1





That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

– JB Nizet
Nov 24 '18 at 14:50







That will add blank lines to the file. It's also quite strange to use the line separator sometimes, and n some other times.

– JB Nizet
Nov 24 '18 at 14:50















@JBNizet My mistake, fixed.

– Martin Heralecký
Nov 24 '18 at 14:52





@JBNizet My mistake, fixed.

– Martin Heralecký
Nov 24 '18 at 14:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459294%2fhow-do-i-have-the-entered-data-go-into-the-next-line-of-a-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings