Results 1 to 8 of 8
Thread: Method return type problem
 05052009, 05:11 AM #1Member
 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[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 } }
"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?
 05052009, 05:14 AM #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; 05052009 at 05:21 AM.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way

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 }
Also note that the derivative variable is already a String array. Thus you can simply call
return derivative;
not
return derivative[];
 05052009, 06:40 AM #4Member
 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[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; } }
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.
 05052009, 04:39 PM #5Member
 Join Date
 Feb 2009
 Posts
 47
 Rep Power
 0
to the top
 05052009, 06:17 PM #6
you can just add a delimiter to your scanner that checks for + or  and it will automatically skip them.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
 05052009, 09:12 PM #7Member
 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+");
 05052009, 09:21 PM #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 patternLiberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Similar Threads

Error: invalid method declaration; return type required
By silvia in forum AWT / SwingReplies: 3Last Post: 06052010, 08:05 PM 
Help: how can get method, arg, return type... from a file .class
By ykzforever in forum New To JavaReplies: 4Last Post: 11252008, 06:53 AM 
return type determines override/overload?
By hedefalk in forum Advanced JavaReplies: 4Last Post: 07112007, 01:48 PM 
The return type
By Marcus in forum New To JavaReplies: 1Last Post: 07052007, 06:28 AM
Bookmarks