Campbell Ritchie wrote:Welcome to the Ranch
Did they really advise you to use a GUI for reading a file? As a general rule, get your application working without a GUI (i.e. reading from the file) and the add a GUI later.
Why are you using a List to add things to if it doesn't already contain them? My concept of a List is that it is supposed to contain duplicates, so why not use another datatype, possibly a kind of Set, which doesn't support duplicates already.
I am not sure why you are getting an empty List returned. Let's have a look at what you have posted, however.
Which class is the List declared in? I presume that is an instance field (which should be private). You don't have a setXXX method, because it doesn't set the List, so I think you should change its name. It is adding something not setting it. I think a List<GroupID> would be better than a List<String> but creating a GroupID class is probably not your first priority just at the moment. Similarly your getXXX method is a bit hazardous. I know of four ways to return a reference to a List, which I have written before, but can't seem to find:-
1: Return the List as is, which is the obvious way to do it but anybody getting a reference to that List can change it, so that is probably a bad idea. 2: Return a read‑only copy: return Collections.unmodifiableList(myList); That is a read‑only copy; subsequent changes to the original List are reflected in the unmodifiable copy. 3: Return a new List: return new ArrayList(myList); That is a competely independent List; if the recipient changes it, your class will never know anything about it. 4: Return an unmodifiable copy: return Collections.unmodifiableList(new ArrayList(myList)); That List cannot be changed, so it is an immutable snapshot of the state of your object at the time the getXXX method was called. The recipient can of course copy that List into a new List.Remember that the state of any mutable objects in an immutable List can still be changed; that is not a problem with things immutable like Strings. You will have to decide which option fits your requirements best. No 2 might be best, but you have to decide that for yourself.
You didn't say in your action listener how you are using the run local variable. You are creating a file cleaner object (run is probably not a good name for that) and I can't see any way you are adding anything to it. You have a call to get the List, but I I can't seem to see where you are adding anything to the List.Then you have another file cleaner object in the analyse file method, and I can't seem to see anything being added to that.
Beware: you now have two file cleaner objects which operate completely independently of each other.
Watch this space.
Campbell Ritchie wrote:Please consider whether you shouldn't make that inner class and the analyse file method private.
That looks good. Maybe some things will eventually appear in other classes.
Josh Herron wrote:. . . I will have my FileCleaner class and then the following methods. . . .
Yes, it is annoying, isn't it. We have all experienced such frustration.
. . . I was beyond frustrated . . .
Campbell Ritchie wrote:For line by line reading a buffered reader will probably run faster than a Scanner. You can do it for about the same complexity of code:-[code=java]try
(BufferedReader reader =
Passing a Path or a File or a String describing the path to the file as a method argument would be a good way to do it. You would write something like
Josh Herron wrote:. . . I'm not sure where I am declaring the path to the file, do I set a method variable with the full path to the myGroupIDFile.txt?
That's a pleasure What a nice thing to say about us: thank you.
. . . thank you so much for providing guidance without making me feel stupid, I have looked at other forums and many of the users just cut down people asking questions instead of helping them look for ways to improve.
You managed to get it inside the code tags, so I thought it wasn't there at all. I only found it a minute or two ago when I noticed how long the lines in the code tags were. I shall do something to restore it.
Josh Herron wrote:I'm not sure why my second response wasn't there. . . .
That is, as far as I can tell, your second response restored, minus my quoted text.
Josh Herron wrote:. . .
I figured different methods for each function is best practice... I just don't fully grasp how to open the file and read each line and then pass that line value to the next method to see if it contains the value I'm looking for; and if so pass the line value again to the next method to determine if the value found already exists in the ArrayList.
. . .