• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading and Writing to a text file from Command Line Arguments

 
Sachidananda Pattnaik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
repeat

- read input
- write to file

until user doesn't want to any more

WP
 
Sachidananda Pattnaik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!