• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing Content to file not working

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm a java newbie so go easy on me A brief background on what I'm trying to do with my program. I want to take an input from a user using JOptionPane when they click on the "add a word" button. The word will then be added to a file that has been created when the program is first executed. At the moment I can get the word to write to the file however, I am having issues in only allowing 1 instance of the word to write to the file. I want a dialog box to show when the user tries to enter a word that already exists in the file. I would also like to store the words in alphabetical order in the file but again I'm not entirely sure how to go about implementing this. I've copied below the code I have done so far. Any help would be greatly appreciated

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
What happens when you write that text to the file from a command‑line program? Do you get one line, or do you get multiple lines?

So many people think a GUI program is a GUI with a program attached. It isn’t. It is a program hidden behind the GUI. Get the program working first, then you can put the GUI on top.
Your program will need a public interface with methods like public void writeToFile(String text, File f) in.
By the way: never use \r or \n unless you have been told you require that particular sequence. There is a BufferedWriter method which write line ends, with the correct combination for your system.
 
Mary Brennan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell Richie . Thanks for getting back to me. I've reworked my code a bit and I have each word being added to the .txt file now. I can delete words from the file too but I'm having a slight problem in that the same word can get entered multiple times. I have written code that I thought would check each line of the file and check to see if that word was equal to the word the user was trying to enter. Unfortunately it doesn't seem to do what I thought it would. I've copied the code for the add word button below if you would be able to have a look? In regards to "\r\n" what would you suggest instead?

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In regards to "\r\n" what would you suggest instead?


As Campbell has already said use the BufferedWriter to do so. It has a newLine() method which adds the appropriate newline char(s) for your system.

BTW I would advise you use HashSet rather than Vector. Vector is a legacy class that isn't often used in new code. Also, as you only want one of each word storing them in a set is a better approach. If you want them sorted into their natural order use a TreeSet instead.

I'm not sure I fully understand the logic of what you are trying to do. If the number of words is low enough to be guaranteed to fit into a collection then why not read the file into the collection on starting your program and then add/delete words from the collection to keep it in sync with your file. Checking if you already have a word is then simply a case of using the collection's contains() method. If, on the other hand, you may have too many words for a collection then you will have to read the file each time you want to add a word but then why are you storing the file words that don't equal the word you have in a Vector.

I suggest you put the code to one side for a moment and write out the steps you would take if you were doing this manually.
 
Mary Brennan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony. If I knew how to use HashSet or TreeSet I would. Unfortunately I haven't yet covered these in college and my lecturer has suggested we use vectors or ArrayLists - though they are urging us to use Vectors instead of ArrayLists.

I'll try and clarify what I am trying to do. When the user enters a word I want to add it to the text file however, if the user enters a word that is already included in the text file I want a dialog box to display informing the user that the word already exists. The word therefore will not be stored a second time. Hope that makes sense. I have to order alphabetically also but I can worry about that once I can ensure that only one instance of a word appears in the dictionary.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've no idea why your tutor would be pushing you towards Vectors. The only advantage they have over ArrayLists is they are synchronized and hence thread safe but that is unlikely to be an issue for you (and there are simple ways of making ArrayLists thread safe anyway).

Given your restrictions I would use an ArrayList. You can do as I said earlier, adding all the words in the file to the ArrayList on startup and then check the ArrayList when you want to add a new word.
BTW you can easily sort an ArrayList of Strings into alphabetical order using the java.utils.Collections.sort method
 
Greenhorn
Posts: 9
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should think about what your code is supposed to be doing first. Referring to your actionPerformed() Method: I assume you want to read the whole file and check if the added word is already present. Have a look at line 30, what does it do and what is it supposed to do?

As for Vector vs any other kind of collection, imho you do not have to worry about this kind of stuff right now... maybe later, when you got the basic functionality working
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree you get the basic functionality working first. But if you can find a collections framework class which makes it easier, so much the better.
I would suggest you ask why you have been told to use Vector. I know there are some teachers who are actually many years out of date and teach in a fashion which was current > 10 years ago.
You actually have a conflict there. You are trying to write into a text file, whilst needing the functionality of a Set. Reading a large text file to see whether it contains a particular word is only slightly faster than writing that word on the keyboard. Reading a 1000000‑element HashSet to see whether it contains a particular word will take something in the order of 2 nanoseconds.
Adding is maybe even faster.
If at all possible, get permission to use a SetI won’t say any more, but you can now see how you can test whether you have already used a word.

I had to break some of the long lines of code in your post, and get rid of the blank lines; both make the code difficult to read. Always use spaces for indenting, not tabs. You have gone one better: you used both.
 
Mary Brennan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the suggestions. I decided to go with a Vector and to load the words in once the application is executed and output the words in the vector once it is exited. Makes everything much easier than what I had been making it for myself.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
It is surprising how thinking about what you are doing makes it so much easier, isn’t it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic