File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Read File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read File" Watch "Read File" New topic

Read File

Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
Hello, I'm still fairly new to Java. For our assignment we were to create a shopping list.
The specs were to create four classes:
--Grocery - contains each item
--GList - contains the node based list
--InvalidAisleException - for invalid aisles

Our input file look as follows:
4 (items in the list)
milk (item)
4 (aisle)

I can get the program to work by plugging the values in as such

I am not real sure where to actually put
Scanner File1 = new Scanner(new FileReader("filename"));
Should this go in my testing class or in a method in my Glist?
I'm also not real sure on how to take what I get from a file and store it into the linked list. I know with a loop, but I'm confused on linked lists. Would this be done in a similar fashion as with an array?
Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
I got it working in my testing program...
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
No. LinkedLists are different from Arrays. An array is something like this (code tags used to improve legibility, not because this is "code."At least I think that's how the members are referred to. If they are "primitive types" then the memory locations would contain their values.

ArrayList class includes an array, as well as methods to copy the array to a new larger array whenever it runs out of space.

A LinkedList like this one works differently.. . . and most LinkedLists are actually implemented in doubly-linked fashion, where the 2nd member also has a link to the 1st member, the 3rd member has a link to the 2nd member, etc. Also your startObject or a finishObject will have a link back to the last member.

Despite the different implementation, both these classes in the API implement the List interface, which means they have methods with exactly the same headings.
Set up your Lists using the interface as a declaration:

and you have got adding to work, I see. Well done.

Declare your Scanner as a local variable in the method where you read the details from a file, and make sure you close it when you have finished reading.I think that will work because Scanners to their own exception handling; if you use a Reader object, you would have to use some very strict exception handling.

Shananne DuFrame
Ranch Hand

Joined: Nov 13, 2006
Posts: 51
My next problem is how do I create a sort method to sort by item. This would be a string value. I've read up on collections, but don't know if this will work beings I created my own linkedList class. I also tried changing the item to a char but I am unsure how to get this to work with the FileReader. It won't read the item in as a char. Is there anyway to change this line
char item =;

or what else would be my other options for sorting strings in a linked list. I found many resolves for arrays but not linked lists.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Look at this thread, where Johnathan Smith is trying to compare players in a team by batting average. Also Johnathan Smith's earlier posting on the same subject.

Look up the Comparable <T>interface, and work out how to implement it, with the compareTo(T t) method in your Grocery class. Note that String already implements Comparable, so comparing Strings is already set up and ready to use.

Look up the Java Tutorial about Collections. The bit about how sorting is done appears under "algorithms." It is easier to sort an ArrayList than a LinkedList. If you have a doubly-linked list it is easier.

More about sorting algorithms here, and it gives an example of how to implement the algorithms for arrays. The code given is in C but you should be able to translate it into Java in a minute or so.
You need to work out a "swap" routine for LinkedLists. Remember all "swap" routines need three members, first, second and temp, of the three kinds.
Assuming your LinkedList has a reference to "next" for "next member," try this sort of thing.
  • Create a "temp" Member.
  • Set "temp" equal to "next"
  • Set "" equal to ""
  • set "" equal to "temp"
  • Move to the next member
  • If the next member is null stop, otherwise repeat.
  • I think I have written that correctly
  • You will notice from Lamont's explanations that the simple sort algorithms require two loops through your collection.

    If you have a few things to sort, by all means use bubble sort, even though merge sort and quicksort are much faster. As Lamont says, for 100 members bubble and merge work about the same speed; for 10 members bubble is faster because it is simpler, and for 1000 members merge and quick are faster because they require fewer comparisons.

    If you get a NullPointerException you might have hit a null at the end of your list earlier than you thought.

    BTW: Everybody calls what Lamont describes bubble sort, but the pedants call it sinking sort; bubble sort is exactly the same but you count backwards.
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 46349
    More details about sorting here.
    I agree. Here's the link:
    subject: Read File
    It's not a secret anymore!