# Method return type problem

Printable View

• 05-05-2009, 05:11 AM
McChill
Method return type problem
Here is my program. It is supposed to take the derivative of a given function and display it.

Code:

```import java.util.Scanner; public class Derivative {                 private double power;         private double coefficient;         private String variable;         public Scanner scanner;         static Derivative d = new Derivative();         String[] terms;         String[] derivative;                 public static void main(String args[]) {                         System.out.println("The derivative of " + args[1] + " is\n");                         System.out.println(d.getDerivative(args[1]));         }         public void setPower(double power) {                 this.power = power;         }         public double getPower() {                 return power;         }         public void setCoefficient(double coefficient) {                         this.coefficient = coefficient;         }         public double getCoefficient() {                 return coefficient;         }         public void setVariable(String variable) {                 this.variable = variable;         }         public String getVariable() {                 return variable;         }                 public String getDerivative(String function)         {                 analyze(function);        //analyze the function given                 String answer = "";                 differentiate(function);        //differentiate the function                 for(int i = 0; i < derivative.length; i++)                         answer = answer + derivative[i];                 return answer;   }                 public void analyze(String function) {                 scanner = new Scanner(function);        //pass the function to the scanner                 int x = 1;                 String[] terms = new String[x];                 while(scanner.hasNext()) {                //while loop through the function                         if(scanner.hasNextDouble())        {                                 setCoefficient(scanner.nextDouble());                //set the coefficient as the first double                         }                         setVariable(scanner.next());                        //set the variable as the next function after first double                         if(scanner.hasNextDouble())        {                                 setPower(scanner.nextDouble());        //set the exponent power as the next double                                        }                         if(scanner.next() == "+") {                                 terms[x - 1] = "" + d.getCoefficient() + d.getVariable() + d.getPower();        //set the term into the array                                 x++;                                                //make the array one slot bigger                         }                 }                                                                        }         public String[] differentiate(String analyzedFunction) {                 String[] derivative = new String[terms.length];                 for(int i = 0; i < terms.length; i++) {                         double newCoefficient = d.getPower() * d.getCoefficient();        //exponent times coefficient                         double newPower = d.getPower() - 1.0;                                        //exponent power minus 1                         derivative[i] = newCoefficient + d.getVariable() + " ^" + newPower;                         if(i < terms.length)                                 derivative[i] = derivative + " + ";                 }                 return derivative[];                        //HERE IS A PROBLEM                         }                 }```
My compiler is telling me
"Syntax error on "[", expression expected after this token"
"Type mismatch: cannot convert from String to String[]"

Why am I not able to return the whole array? Eclipse's suggestion is change the return type to just a String, but I want to return a String array with each element being a differentiated term. How can I fix this?
• 05-05-2009, 05:14 AM
OrangeDog
derivative is an array. derivative[i] is a String in the array. derivative[] is a syntax error.

Arrays cannot be dynamically resized once declared. x++ increases the value of x, not the size of the terms array. If you want a growable array, use ArrayList
Arrays (The Java Tutorials)

Declaring local variables overrides the instance variables. Thus your methods make no changes to the instance variables, but just discard their results.
Variables (The Java Tutorials)
• 05-05-2009, 05:15 AM
Fubarable
This is your method:
Code:

```  public String[] differentiate(String analyzedFunction)   {     String[] derivative = new String[terms.length];     for (int i = 0; i < terms.length; i++)     {       double newCoefficient = d.getPower() * d.getCoefficient(); //exponent times coefficient       double newPower = d.getPower() - 1.0; //exponent power minus 1       derivative[i] = newCoefficient + d.getVariable() + " ^" + newPower;       if (i < terms.length) derivative[i] = derivative + " + ";     }     return derivative; //HERE IS A PROBLEM   }```
Note that the method is supposed to return a String array.

Also note that the derivative variable is already a String array. Thus you can simply call

return derivative;

not

return derivative[];
• 05-05-2009, 06:40 AM
McChill
oh okay, I get it thanks. I have a question after changing it up.
Code:

```import java.util.Scanner; import java.util.ArrayList; public class Derivative {                 private double power;         private double coefficient;         private String variable;         public Scanner scanner;         public ArrayList<String> terms;         public ArrayList<String> derivative;         static Derivative d = new Derivative();                 public static void main(String args[]) {                         System.out.println("The derivative of " + args[0] + " is\n");                         System.out.println(d.getDerivative(args[0]));         }         public void setPower(double power) {                 this.power = power;         }         public double getPower() {                 return power;         }         public void setCoefficient(double coefficient) {                         this.coefficient = coefficient;         }         public double getCoefficient() {                 return coefficient;         }         public void setVariable(String variable) {                 this.variable = variable;         }         public String getVariable() {                 return variable;         }                 public String getDerivative(String function)         {                 analyze(function);        //analyze the function given                 String answer = "";                 differentiate(function);        //differentiate the function                 for(int i = 0; i < derivative.size(); i++) {                         answer = answer + derivative.get(i);                         if(i < derivative.size())                                 answer = answer + " + ";                 }                 return answer;   }                 public void analyze(String function) {                 scanner = new Scanner(function);        //pass the function to the scanner                 terms = new ArrayList<String>();                 while(scanner.hasNext()) {                //while loop through the function                         if(scanner.hasNextDouble())        {                                 setCoefficient(scanner.nextDouble());                //set the coefficient as the first double                         }                         setVariable(scanner.next());                        //set the variable as the next token after first double                         if(scanner.hasNextDouble())        {                                 setPower(scanner.nextDouble());        //set the exponent power as the next double                                }                         else        setPower(1);                        //if the next token wasn't a double, set the power to 1                         terms.add(d.getCoefficient() + d.getVariable() + d.getPower());        //set the term into the arraylist                         if(scanner.next() == "+") {                        //***HERE***                                 scanner.skip("+");                                //***HERE***                         }                                                                        //***HERE***                                        }                                                                        }         public ArrayList<String> differentiate(String analyzedFunction) {                 derivative = new ArrayList<String>();                 for(String term: terms) {                         double newCoefficient = d.getPower() * d.getCoefficient();        //exponent times coefficient                         double newPower = d.getPower() - 1.0;                                        //exponent power minus 1                         derivative.add(newCoefficient + d.getVariable() + " ^" + newPower);                 }                 return derivative;                                                }                 }```
The problem is the last if statement in analyze(String function). If i erase the if statement and enter in only one term, the program works fine. But if I leave that if statement in there and try to enter in more than one, I get a statement saying
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Derivative.analyze(Derivative.java:70)
at Derivative.getDerivative(Derivative.java:46)
at Derivative.main(Derivative.java:16)
I'm assuming that the problem is with the if statement I pointed out. I want to just skip over the + symbols. Like if the next token is a + or - symbol, I want to just skip over it and begin scanning the next term. I looked in the Scanner class page and ArrayList class page at Java 2 Platform SE 5.0, but I didn't see anything. Maybe I overlooked it in my java noobness. I really just need a way to skip over the addition and/or subtraction symbols. Or if the problem is something else, could you point it out to me and direct me to a place of info? Thanks.
• 05-05-2009, 04:39 PM
McChill
to the top
• 05-05-2009, 06:17 PM
xcallmejudasx
you can just add a delimiter to your scanner that checks for + or - and it will automatically skip them.
• 05-05-2009, 09:12 PM
McChill
Am I able to have two delimiters for a scanner? I put in the code
Code:

`scanner.useDelimiter("\\s+");`
in analyze after I intialize the scanner. However the program is displaying some odd results for an answer. I'm assuming it's because now the only delimiter is a +, and not white space? I would like both of them to be skipped over or find a way to type in like "3x2" and have it be read as 3 x squared instead of having to type it in as "3 x 2"
• 05-05-2009, 09:21 PM
xcallmejudasx
I'm sure it can. You just have to set the pattern up properly or attach multiple delimiters(if possible)
Java 2 Platform SE 5.0
check the useDelimiter method of Scanner
Pattern (Java 2 Platform SE 5.0)
how to put together your delimiting pattern