Take a derivative

• 05-02-2009, 11:17 PM
McChill
Take a derivative
I want to take and display the derivative of a given input.

Code:

```import java.util.Scanner; public class Derivative {                 private int power;         private int coefficient;         private String variable;                 public static void main(String args[]) {                 Derivative d = new Derivative();                 System.out.println(d.getDerivative(args[0]));        //LINE 10                         }         public void setPower(int power) {                 this.power = power;         }         public int getPower() {                 return power;         }         public void setCoefficient(int coefficient) {                 this.coefficient = coefficient;         }         public int getCoefficient() {                 return coefficient;         }         public void setVariable(String variable) {                 this.variable = variable;         }         public String getVariable() {                 return variable;         }                 public String getDerivative(String term)         {                 String derivative;                 Scanner scanner = new Scanner(term);        //pass the term to the scanner                 if(scanner.hasNextInt())                                setCoefficient(scanner.nextInt());                //set the coefficient as the first integer                                setVariable(scanner.next());                        //set the variable as the next term after first integer                 if(scanner.hasNextInt())                         setPower(scanner.nextInt());        //set the exponent power as the next integer                                int newCoefficient = power * coefficient;        //differentiate                 int newPower = power - 1;                 derivative = newCoefficient + variable + "^" + newPower;                                        return derivative;   } }```
I tried to input 2x2, which really is 2x squared. Obviously the answer should be 4x^1, but I keep getting 02x2^-1
as a result. Whenever I call a hasNext... method with scanner, does the scanner move up a token? So that whenever I check, it moves, then I assign, and it moves, and I assign and check and etc etc. I feel like I'm doing something obviously wrong with the scanner, but I don't know how to fix it.

I also tried
Code:

```        public String getDerivative(String term)         {                 String derivative;                 Scanner scanner = new Scanner(term);        //pass the term to the scanner                 setCoefficient(scanner.nextInt());                //set the coefficient as the first integer                                setVariable(scanner.next());                        //set the variable as the next term after first integer                 setPower(scanner.nextInt());        //set the exponent power as the next integer                                int newCoefficient = power * coefficient;        //differentiate                 int newPower = power - 1;                 derivative = newCoefficient + variable + "^" + newPower;                                        return derivative;   }```
But that kept telling me mismatched input.
• 05-02-2009, 11:45 PM
OrangeDog
You haven't configured the delimiters on your scanner. With default setup, your string has one token, '2x2', which cannot be parsed as an integer. hasNextInt() will return false, next() will return the whole token and the results will be as you see them.
• 05-03-2009, 05:32 AM
McChill
Awesome thanks, you've been a big help in pointing out mistakes for me today. Hopefully I won't need to ask any more questions haha.