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;
}
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
add a comment |
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
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 andprintln()seems to failed to create a new line, tryprint(). Create a constantprivate static final String newline = System.getProperty("line.separator");and usepw.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
add a comment |
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
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
java text
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 andprintln()seems to failed to create a new line, tryprint(). Create a constantprivate static final String newline = System.getProperty("line.separator");and usepw.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
add a comment |
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 andprintln()seems to failed to create a new line, tryprint(). Create a constantprivate static final String newline = System.getProperty("line.separator");and usepw.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
add a comment |
1 Answer
1
active
oldest
votes
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);
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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, tryprint(). Create a constantprivate static final String newline = System.getProperty("line.separator");and usepw.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