Results 1 to 9 of 9
Thread: [SOLVED] Loop(?) problem
 05062009, 12:22 AM #1Member
 Join Date
 Feb 2009
 Posts
 47
 Rep Power
 0
[SOLVED] Loop(?) problem
The program is not displaying the response I am looking for. The terms are getting added into the Arraylists, and are being differentiated then added into the other ArrayList properly. However, whenever I run the program it only displays the last term. For example, if I put in "3 x 3 + 2 x 2", it will display
The derivative of 3 x 3 + 2 x 2 is
The term 3.0x3.0was added to terms.
The term 9.0x ^2.0was added to derivative.
The term 2.0x2.0was added to terms.
The term 4.0x ^1.0was added to derivative.
4.0x ^1.0 +
I want it to display this 
The derivative of 3 x 3 + 2 x 2 is
The term 3.0x3.0was added to terms.
The term 9.0x ^2.0was added to derivative.
The term 2.0x2.0was added to terms.
The term 4.0x ^1.0was added to derivative.
9.0x ^2.0 + 4.0x ^1.0 +
Java Code:import java.util.Scanner; import java.util.ArrayList; import java.util.regex.Pattern; 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 = ""; for(String each: derivative) { answer = answer + each; //put each term in derivative into the answer String answer = answer + " + "; //add the + signs...adds an extra one right now until I find a better way. } return answer; } public void analyze(String function) { scanner = new Scanner(function); //pass the function to the scanner scanner.useDelimiter("\\W+"); terms = new ArrayList<String>(); int x = 0; 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(getCoefficient() + getVariable() + getPower()); //set the term into the arraylist System.out.println("The term " + getCoefficient() + getVariable() + getPower() + "was added to terms."); //just for testing purposes differentiate(terms.get(x)); x++; } } public ArrayList<String> differentiate(String analyzedFunction) { derivative = new ArrayList<String>(); double newCoefficient = getPower() * getCoefficient(); //exponent times coefficient double newPower = getPower()  1.0; //exponent power minus 1 derivative.add(newCoefficient + getVariable() + " ^" + newPower); System.out.println("The term " + newCoefficient + getVariable() + " ^" + newPower + "was added to derivative."); //just for testing purposes return derivative; } }
 05062009, 12:45 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
How big is the array list derivative when you call getDerivative()? How big did you expect it would be?
Where  and how often  do you set the value (not the contents) of derivative?Last edited by pbrockway2; 05062009 at 12:48 AM.
 05062009, 03:43 AM #3Member
 Join Date
 Nov 2008
 Posts
 30
 Rep Power
 0
Looking at your code I'm not real sure its a testing class or a class of information sense you have main but you also have a bunch of methods that are set up to do things.
Now the way i understand a test program inside main you have yoru veriables and the method calls. I know that Arrays only work with a designated size, sense they can't grow. So you to determine the size before you load/search make it do fancy things.
 05062009, 04:35 AM #4Member
 Join Date
 Feb 2009
 Posts
 47
 Rep Power
 0
Well when getDerivative() first gets called, derivative is null. But once analyze() gets called, and differentiate() gets called, it should add elements to the array list equal to how many terms the function has. Does using the add method count as setting the value or the contents? The add() method in differentiate() is the only time derivative is changed. Two elements are being added to derivative, but for some reason it's the same value. I changed a line in differentiate() to make sure it was the element was being added.
Java Code:public ArrayList<String> differentiate(String analyzedFunction) { derivative = new ArrayList<String>(); double newCoefficient = getPower() * getCoefficient(); //exponent times coefficient double newPower = getPower()  1.0; //exponent power minus 1 derivative.add(newCoefficient + getVariable() + " ^" + newPower); System.out.println("The term " + derivative.get(0) + "was added to derivative."); //******changed calling variables into calling the array list element!!!! x++; return derivative; }
The derivative of 3 x 3 + 2 x 2 is
The term 3.0x3.0was added to terms.
The term 9.0x ^2.0was added to derivative.
The term 2.0x2.0was added to terms.
The term 4.0x ^1.0was added to derivative.
4.0x ^1.0 +
So the values are being set but I don't see why the line that should be the whole derivative is just the last term.
 05062009, 04:37 AM #5Member
 Join Date
 Nov 2008
 Posts
 30
 Rep Power
 0
Mmm that looks like it shoudl be used with vectors or linked list as opposed to arrays, because an array isn't dynamic...it'd have to know how many elements you want to put in it before hand. like if theres.... 4 elements in it before it can load.
 05062009, 05:17 AM #6
ArrayLists are also dynamic
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
 05062009, 09:24 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Well when getDerivative() first gets called, derivative is null. But once analyze() gets called, and differentiate() gets called, it should add elements to the array list equal to how many terms the function has
The other half was supposed to push you see what the length of derivative really is.
Java Code:public String getDerivative(String function) { analyze(function); //analyze the function given String answer = ""; /* * This line tells you the *actual* size of derivative */ System.out.println("size=" + derivative.size()); for(String each: derivative) { answer = answer + each; answer = answer + " + "; } return answer; }
Java Code:The derivative of 3 x 3 + 2 x 2 is The term 3.0x3.0was added to terms. The term 9.0x ^2.0was added to derivative. The term 2.0x2.0was added to terms. The term 4.0x ^1.0was added to derivative. size=1 4.0x ^1.0 +
(Since the purpose of derivative is to "accumulate" the result it is most important that it be assigned to exactly once.)
 05062009, 03:48 PM #8Member
 Join Date
 Feb 2009
 Posts
 47
 Rep Power
 0
Oh...okay. I see now I had a blatantly obvious mistake there in differentiate. It reset the value of derivative each time it was called. Thanks. How do I marked this as solved?
 05062009, 06:03 PM #9Senior Member
 Join Date
 Mar 2009
 Posts
 552
 Rep Power
 6
Similar Threads

if else loop problem
By Ms.Ranjan in forum New To JavaReplies: 12Last Post: 04252009, 10:30 AM 
Some while loop problem need help
By shaggyoo7 in forum New To JavaReplies: 4Last Post: 01142009, 08:16 PM 
Loop Problem
By jralexander in forum New To JavaReplies: 4Last Post: 12022008, 08:08 AM 
Problem to use different for loop to add up
By matt_well in forum New To JavaReplies: 6Last Post: 08032008, 11:24 PM 
For loop problem
By mcal in forum New To JavaReplies: 32Last Post: 01252008, 04:51 PM
Bookmarks