Forums Register Login

Reading and Writing to a text file from Command Line Arguments

+Pie Number of slices to send: Send
Hi All,

I need to write a small application by which I can store Book Titles and its Author name into a text file.
Then it can have a search functionality to search for Book Title and Author name

The command line argument should be like below..

Enter 1 for Storing & Enter 2 for Retrieving : 1

Enter Author name: <Author1>
Enter Book name : <Book1>
Entry recorded...

Want to store more value (Y/N) ?: Y

Enter Author name: <Author2>
Enter Book name : <Book2>
Entry recorded...

Want to store more value (Y/N) ?: N

Exit

now my Book.txt file should contains value like

Author1 Book1
Author2 Book2


Enter 1 for Storing & Enter 2 for Retriving : 2

Search Author Name : <Author1>
Result: <Book1>


Please suggest me how to start with this. Pseudo code will also helpful.



Thanks
Sachin

+Pie Number of slices to send: Send
We don't do your homework for you, but you'd certainly want to start with java.util.Scanner. When you get the user input part working well, then start writing results to the file. For that, you'll want to look at FileWriter.
+Pie Number of slices to send: Send
repeat

- read input
- write to file

until user doesn't want to any more

WP
+Pie Number of slices to send: Send
Here is my code so far ... I have written a method to write user input data into a file




The problem is after getting the two user input values. The second value is only getting written inside the file.
Also how to write this two inputs with a space separated.



Thanks
Sachin
1
+Pie Number of slices to send: Send
Please use code tags when posting code. They make it much easier to read, and give us line numbers to refer to. I've added them for you above.

You read the author name from the user at line 11 and store the result in inputData, but then at line 15 you read the book title and store it in the same variable, which wipes out what was stored there before. There are several solutions to this. You could write the author to the file before prompting the user for the title, you could use two different variables for holding the author and title, or you could concatenate the title to the author by using += instead of just =. Any of these methods should work. Getting a space between the author and title will depend on which way you choose.
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3101 times.
Similar Threads
StaleStateException
Another set of Mock questions!!
disassociate 2 domain objects
newbee cant find string
Write enter/indent with java inside xml file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:11:06.