# Thread: Method return type problem

1. Member Join Date
Feb 2009
Posts
47
Rep Power
0

## Method return type problem

Here is my program. It is supposed to take the derivative of a given function and display it.

Java 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 + " is\n");
System.out.println(d.getDerivative(args));
}

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?  Reply With Quote

2. ## 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)
Last edited by OrangeDog; 05-05-2009 at 05:21 AM.  Reply With Quote

3. ## This is your method:
Java 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[];  Reply With Quote

4. Member Join Date
Feb 2009
Posts
47
Rep Power
0

## oh okay, I get it thanks. I have a question after changing it up.
Java 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 + " is\n");
System.out.println(d.getDerivative(args));
}

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.  Reply With Quote

5. Member Join Date
Feb 2009
Posts
47
Rep Power
0

## to the top  Reply With Quote

6. ## you can just add a delimiter to your scanner that checks for + or - and it will automatically skip them.  Reply With Quote

7. Member Join Date
Feb 2009
Posts
47
Rep Power
0

## Am I able to have two delimiters for a scanner? I put in the code
Java 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"  Reply With Quote

8. ## 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•