Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Multiple Inputs and Writing to .txt

  1. Mar 22, 2016 #1
    Essentially, my problem is to: design a program that takes the following information:

    -a persons ID
    -a houses ID
    -listing price of house
    -selling price of house

    and write the input to a text file, called Sales.txt. Secondly, based on the list and selling price, commission is calculated based on some conditions. After everything has been entered, the persons ID, the houses ID, and the final commission price has to be written to another text file called Commissions.txt; this info on Commissions.txt also has to be printed to the screen.

    I also need to include exception handling for if the user inputs an invalid form. Let's assume the input form is that of: PPPPP HHHH LLLLLL SSSSSS
    of course the prices wouldn't really need to be concerned as the prices could theoretically be anything, but the two id's would need a particular form.

    So my problem is I haven't a clue how to start. I've learned the basics of being able to read a text file and write in the editor to the file, but how do I go about having a user input multiple lines of text and have that then written to the .txt files in question? I'm guessing there would be a while loop in there. My biggest concern is not knowing how to deal with the multiple inputs and writing to the text files.

    Any insight would be great.
     
  2. jcsd
  3. Mar 22, 2016 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    1. you have to design the specifications, then write code?
    2. if so what language & OS are you running?
     
  4. Mar 22, 2016 #3
    With all due respect, did you read my description fully? I'm not sure on how to implement my design, with regards to the multiple inputs and writing to the text file simultaneously. The language is given in the tag - java. I'm running linux mint 17.2.
     
  5. Mar 22, 2016 #4

    Mark44

    Staff: Mentor

    What does "write in the editor to the file" mean?
    Do you know how to open a file and read from it using Java? The process is similar if you want to write to a file.
    Don't get caught up in implementation details, yet. First, make a list of the things you need to do, to get an idea of your overall algorithm. I wouldn't even put in exception handling at this point -- just get your input operations working, and the output to the commssions file and the screen.

    You don't need to input multiple lines of text -- just input one piece of information at a time.

    Your algorithm should start out something like this:
    • Prompt user for ID
    • Input user ID
    • Prompt user for house ID
    • Input house ID
    • etc.
    You will probably need a loop of some kind when you build in the exception handling, but at this point, you don't.
     
  6. Mar 23, 2016 #5
    Think about the flow of your program, all of the tasks that it needs to do. The first step to writing a program is usually taking paper and pencil (actually, it's usually a whiteboard and markers) and separating the large task into several smaller, more manageable tasks. A quick whiteboard diagram of what you described:

    Screen Shot 2016-03-23 at 9.57.18 AM.png
     
  7. Mar 23, 2016 #6

    jtbell

    User Avatar

    Staff: Mentor

    More concretely, assuming this program is supposed to communicate with the user via plain text at a command line, I would write out a sample "dialog" between the program and the user:

    Please enter the following information:
    Person's ID: 12345
    House ID: 67890
    [etc.]

    Decide on some way for the program to ask the user whether he wants to continue entering data, or stop. Show a few complete "cycles" of data entry. Then write your code such as to reproduce this dialog. A sequence of dialog that repeats, indicates the location of a loop in the program.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted