Java Multiple Inputs and Writing to .txt

AI Thread Summary
The discussion centers on designing a Java program that captures user input for a person's ID, a house's ID, the listing price, and the selling price, then writes this information to a text file named Sales.txt. Additionally, the program must calculate a commission based on the listing and selling prices, outputting the person's ID, house ID, and commission to another file called Commissions.txt, which should also be displayed on the screen.Key challenges include managing multiple inputs, ensuring proper formatting for IDs, and implementing exception handling for invalid input. The conversation emphasizes the importance of breaking down the task into smaller components, suggesting a step-by-step approach to input collection and output writing. It is recommended to start with basic input operations before incorporating exception handling and to utilize a loop for repeated data entry. The user is encouraged to visualize the program's flow and create a sample dialog to clarify the interaction between the program and the user.
Euler2718
Messages
90
Reaction score
3
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.
 
Technology news on Phys.org
1. you have to design the specifications, then write code?
2. if so what language & OS are you running?
 
jim mcnamara said:
1. you have to design the specifications, then write code?
2. if so what language & OS are you running?

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.
 
Morgan Chafe said:
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
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.
Morgan Chafe said:
, 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.
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.
Morgan Chafe said:
My biggest concern is not knowing how to deal with the multiple inputs and writing to the text files.

Any insight would be great.
 
  • Like
Likes Euler2718
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
 
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
Replies
15
Views
3K
Replies
4
Views
2K
Replies
2
Views
3K
Replies
7
Views
2K
Replies
8
Views
8K
Replies
1
Views
2K
Back
Top