This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Polynomial LinkedList from Text File (Modification) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polynomial LinkedList from Text File (Modification)" Watch "Polynomial LinkedList from Text File (Modification)" New topic
Author

Polynomial LinkedList from Text File (Modification)

Kayla Keen
Greenhorn

Joined: Nov 26, 2011
Posts: 1
I want to modify the below code to read the Polynomial Terms from a text file (vs. hard-coded values)

Furthermore, read data from text file in the format:

P1 = 3 5 1 -1 0 8

P2 = 5 6 2 -1 1 7 0 -4

etc...

Name the values P(x) and input the remaining data... Any advice?


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3901
    
  18

Welcome to CodeRanch!

Why are you implementing your own linked list? Why don't you just let your Polynomial class consist of a List<Term>, which if you wanted to, you could sort with a Comparator<Term> that ranks terms by their exponents? Or you could even use a TreeMap.

This will get rid of a lot of ugly C-like code (for(Monomial tmp = head; tmp != null; tmp = tmp.next); eww) and you can focus on your problem.

As for your input problem, you could give the Polynomial class a load(InputStream) method, which will scan the stream for pairs of integers until it hits end-of-line, or maybe some other delimiter.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
 
jQuery in Action, 3rd edition
 
subject: Polynomial LinkedList from Text File (Modification)
 
It's not a secret anymore!