• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Save and close a text file, and then reopen another file with the sum of the previous file

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After inputting a destination file, I am trying to open prices.txt, input values, save the prices.txt, and then have another .txt file open with the sum, tax, and total of the items on the prices.txt file. This is what I have so far.... This is from a JGrasp file, so sometimes the alignment is a little off......


 
Marshal
Posts: 76412
365
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you creating a new Process? You don't need to use NotePad to write a text file. You can do that with a PrintStream, BufferedWriter, Formatter, etc. Are you actually using that Process?
Don't mix GUI code with your logic. Get something running at the command line not using any Swing code at all. Add the Swing code later when you have your app working.
Don't use doubles for money. Don't spend lots of time converting your numbers to Strings and vice versa.
Don't use \n. Use %n (in the format() method) or BufferedWriter#newLine() (I think) instead.
Don't use an option pane to find files. Use a file chooser instead.
Do you have some sort of Price object you can put into a List? As you read the file, create a new Price object. That should be able to calculate the tax for you. Once you have got the Price object to calculate the tax, etc., you can easily get all the details out of it to write to your file. Maybe with its toString() method.
Don't create a new buffered writer for each line. Use one writer to write the entire list of prices. If you create a new file writer wrongly, you will end up with only the most recent line in the file at all.
Don't call close(). Use what this Java™ Tutorials section describes instead.
Don't put all your code in the main method.
 
Danielle Beard
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you creating a new Process? You don't need to use NotePad to write a text file. You can do that with a PrintStream, BufferedWriter, Formatter, etc. Are you actually using that Process?
Don't mix GUI code with your logic. Get something running at the command line not using any Swing code at all. Add the Swing code later when you have your app working.
Don't use doubles for money. Don't spend lots of time converting your numbers to Strings and vice versa.
Don't use \n. Use %n (in the format() method) or BufferedWriter#newLine() (I think) instead.
Don't use an option pane to find files. Use a file chooser instead.
Do you have some sort of Price object you can put into a List? As you read the file, create a new Price object. That should be able to calculate the tax for you. Once you have got the Price object to calculate the tax, etc., you can easily get all the details out of it to write to your file. Maybe with its toString() method.
Don't create a new buffered writer for each line. Use one writer to write the entire list of prices. If you create a new file writer wrongly, you will end up with only the most recent line in the file at all.
Don't call close(). Use what this Java™ Tutorials section describes instead.
Don't put all your code in the main method.



Yes, our Professor wants us to be able to open 2 separate Notepad files. The second Notepad file is suppose to contain the sum of the amounts from the first Notepad file, plus calculate the sum, tax, and total amount and display it in the second Notepad file. This is very confusing lol.
 
Marshal
Posts: 27368
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Danielle Beard wrote:Yes, our Professor wants us to be able to open 2 separate Notepad files. The second Notepad file is suppose to contain the sum of the amounts from the first Notepad file, plus calculate the sum, tax, and total amount and display it in the second Notepad file. This is very confusing lol.



Maybe your professor is one of those people who calls an ordinary text file a "Notepad file"? I can see how that could be confusing.

At any rate using a ProcessBuilder to open Notepad is unnecessary, especially as your code already includes some perfectly good code to read and write text files.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic