I am using String Tokenizer and Linked Lists, and the Linked Lists are required for this assignment. There is an external file in there are numerous lines of polynomials (one per line). Using String Tokenizers and Linked List, I am running a while loop which captures two lines on each pass and adds them to linked lists. After the numbers have been loaded into linked lists, the goal is to add those polynomials together from their linked list and create a new linked list that contains that polynomial.
For example, first two lines in the file are this:
2x^4 -5x^3 +9x^2 -10
3x^4 -6x^3 +10x^2 -11
=
5x^4 -11x^3 +19x^2 -21
Here is my code:
public class PolynomialAddition
{
static File dataInpt;
static Scanner inFile;
public static void main(String[] args) throws IOException
{
dataInpt=new File("C:\\llpoly.txt");
inFile=new Scanner(dataInpt);
StringTokenizer myTokens;
String line,polyTerm;
Node firstL=new Node();
Node secondL=new Node();
line=inFile.nextLine();
myTokens=new StringTokenizer(line);
polyTerm=myTokens.nextToken();
firstL.value=polyTerm.substring(0,polyTerm.indexOf("x"));
firstL.value2=polyTerm.substring(polyTerm.indexOf("^")+1);
}
}
Here is my node class:
public class Node
{
public Object value;
public Object value2;
public Node next;
public Node()
{
value=null;
value2=null;
next=null;
}
public Node (Object value, Object value2, Node next)
{
this.value=value;
this.value2=value2;
this.next=next;
}
}
The problem comes after this where some lines are not complete while the line they have to be added to is complete like -12x^8 +5x^2 -3 and 8x^3 +2x
The answer to this is supposed to be -12x^8 +8x^3 +5x^2 +2x -3
What can I do to solve this?
Ok, after lenghty labour in the chat, this is what ‘we’ came up with. I realize this is just blurting the answer, to an extent.
Even so, having a solid implementation in clean style Java 1.4 code could do a lot to help your understanding.
Special attention has been given to printing the result in tabulated form, aligning terms of different operand in the columns of their respective exponent.
Code
There are two files:
Node.java
PolynomialAddition.java
Sample Data:
Input:
Output: