I have a class called Polynomial with an ArrayList made up of term objects, there is an external file that is read by a Scanner object in my test class. The Scanner reads the line for 4 different key words and acts accordingly. ex. INSERT 3 2. Would call my insert method and print out 3x^2. Now I have a delete method with two parameters. When I call the method in the test class nothing happens, the same thing gets printed and nothing has been removed. Am I missing something or doing it wrong all together? Any help is greatly appreciated.
public void delete (int coeff, int expo)
{
for (int i = 0; i<terms.size(); i++)
{
Term current = terms.get(i);
terms.remove(current.getCoeff());
terms.remove(current.getExpo());
}
}
I also have a Term class that creates a term object, and has two methods to get the coefficient and exponent.
Here is a snippet of my test class:
public static void main(String[] args) throws IOException
{
// TODO code application logic here
Polynomial polyList = new Polynomial();
Scanner inFile = new Scanner(new File("operations2.txt"));
while(inFile.hasNext())
{
Scanner inLine = new Scanner(inFile.nextLine());
String insert = inLine.next();
if(insert.equals("INSERT"))
{
int coeff = inLine.nextInt();
int expo = inLine.nextInt();
polyList.insert(coeff, expo);
}
if(insert.equals("DELETE"))
{
int coeff = inLine.nextInt();
int expo = inLine.nextInt();
polyList.delete(coeff, expo);
}
}
System.out.println(polyList.toString());
}
}
Edit: this is a sample of the .txt file that is being read by the scanner class:
INSERT 3 2
INSERT 4 4
INSERT 1 6
INSERT 2 0
INSERT 5 2
INSERT 6 3
PRODUCT
DELETE 3 2
INSERT 2 7
DELETE 4 4
INSERT 4 10
Edit: Here is the Term class:
class Term
{
//instance vars
private int coefficient;
private int exponent;
public Term(int coeff, int expo)
{
coefficient = coeff;
exponent = expo;
}
public int getCoeff()
{
return coefficient;
}
public int getExpo()
{
return exponent;
}
@Override
public int hashCode()
{
return coefficient + exponent;
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof Term))
{
return false;
}
Term t = (Term)o;
return coefficient == t.coefficient && exponent == t.exponent;
}
}