Note that there are some explanatory texts on larger screens.

plurals
  1. POCode continues on for a long time - Recursion Issues - How can I fix?
    primarykey
    data
    text
    <p>I used the following code in my class with the intention of doing one round of recursion (specifically creating an object within an object of the same type). Well, that one round of recursion is now like 200 rounds of recursion... So that messes a lot of stuff up. The following code is where I call the recursion:</p> <pre><code>//Find Solute try{if(iterations == 0){ //RECONDITION::: iterations is equal to zero at start of program and is static! remaining = Whitespace.removePreceding(remaining); String unused = remaining.substring(0); InterpretInput solute = new InterpretInput(remaining); solute.begin(); solute.fixSoluteAmount(); soluteAmount = solute.getSolventAmount(); isSolution = true; ++iterations; }}catch(Exception ex){ } finally{ System.out.println("Debugging point D"); findNumber(); fixSolventAmount(); fixSoluteAmount(); } </code></pre> <p>You'll find "Debugging point D" above, this is printed a ton of times so it apparently is going after the recursion with a bunch of objects, and the rest of the code is screwed up because of this. I just need someone experienced to point out how this is flawed as one iteration of recursion.</p> <p>If you need the entire class, I'll also copy / paste that bellow, but it's almost 200 lines... so yeah... (I know I shouldn't make classes that long but this object needed a lot of stuff in it).</p> <pre><code>import java.util.ArrayList; </code></pre> <p>public class InterpretInput {</p> <pre><code>/** * @param remaining - The string that was input, what's left to analyze */ /** Variables */ private String remaining; //The string input by the user, containing what's left to analyze private static int iterations = 0; //Solvent Info private double solventAmount; //The amount of the solvent expressed as final in MOLES private M solventAmountMeas; //The measurement used in solventAmount private double solventConc; //The concentration of the solvent private M solventConcMeas; //The measurement used in solventConc private E[] solventCompound; //The compound of the solvent private E[] water = {E.H, E.H, E.O}; //Solute Info private double soluteAmount; //The amount of solute in the solution //Type of Data private boolean isElement = false; //Determines if the information input is only an element private boolean hasAmount = false; //Determines if the information input has an amount of solvent private boolean isSolution = false; //determines if the information input is a solution private int identificationNumber; /** Constructor */ public InterpretInput (String remain){ remaining = remain; } /** Mutator Methods * @throws Exception */ public void begin() throws Exception{ //Find Measurement FindMeasurements measureObject = new FindMeasurements(remaining); while (measureObject.exists() == true){ measureObject.determineNumber(); measureObject.determineMeasurement(); double solventAmountTemp = measureObject.getAmount(); M solventAmountMeasTemp = measureObject.getMeasurement(); if( (solventAmountMeasTemp.getType()) == 3 ){ isSolution = true; solventConc = solventAmountTemp; solventConcMeas = solventAmountMeasTemp; }else{ hasAmount = true; solventAmount = solventAmountTemp; solventAmountMeas = solventAmountMeasTemp; } remaining = measureObject.getRemaining(); } //Find Compound FindCompound comp = new FindCompound(remaining); comp.getCompound(); solventCompound = comp.getValue(); remaining = comp.getRemaining(); if (solventCompound.length == 1) isElement = true; //Find Solute try{if(iterations == 0){ remaining = Whitespace.removePreceding(remaining); String unused = remaining.substring(0); InterpretInput solute = new InterpretInput(remaining); solute.begin(); solute.fixSoluteAmount(); soluteAmount = solute.getSolventAmount(); isSolution = true; ++iterations; }}catch(Exception ex){ } finally{ System.out.println("Debugging point D"); findNumber(); fixSolventAmount(); fixSoluteAmount(); } } public void fixSoluteAmount() throws Exception { fixSolventAmount(); } public void fixSolventAmount() throws Exception { switch (identificationNumber){ //VIEW findNumber TO SEE INDEX OF THESE CASES case 1:{ //In this situation, there would be nothing to change to begin with break; } case 2:{ //In this situation, there would be nothing to change to begin with break; } case 3:{ solventAmount *= solventAmountMeas.ofBase(); switch (solventAmountMeas.getType()){ case 1:{ //volume if (!solventCompound.equals(water)) throw new Exception(); else{ solventAmount *= 1000; //Convert 1000g for every 1L double molarMass = 0; for (E e : solventCompound) molarMass += e.atomicMass(); solventAmount /= molarMass; //convert to moles } } case 2:{ //mass double molarMass = 0; for (E e : solventCompound) molarMass += e.atomicMass(); solventAmount /= molarMass; //convert to moles } } } case 4:{ if(solventAmountMeas.equals(M.m)){ throw new Exception(); //I AM TAKING OUT THIS FEATURE, IT WILL BE TOO DIFFICULT TO IMPLEMENT //BASICALLY, YOU CANNOT USE MOLALITY IN THIS PROGRAM ANYMORE } } case 5:{ if(solventAmountMeas.equals(M.m)) throw new Exception(); //I AM TAKING OUT THIS FEATURE, IT WILL BE TOO DIFFICULT TO IMPLEMENT //BASICALLY, YOU CANNOT USE MOLALITY IN THIS PROGRAM ANYMORE double molarMass = 0; for (E e : solventCompound) molarMass += e.atomicMass(); solventAmount /= molarMass; //convert to moles } } } public void findNumber(){ /** * 1 = Element * 2 = Compound * 3 = measured amount of compound * 4 = specific concentration of solution * 5 = Measured amount of specific concentration of solution * */ if(isElement==true) identificationNumber = 1; else if(isSolution == false &amp;&amp; hasAmount == false) identificationNumber = 2; else if(isSolution == false &amp;&amp; hasAmount == true) identificationNumber = 3; else if(isSolution == true &amp;&amp; hasAmount == false) identificationNumber = 4; else identificationNumber = 5; } /** Accessory Methods */ public double getSolventAmount(){ return solventAmount; } public double getSoluteAmount(){ return soluteAmount; } public double getConcentration(){ return solventConc; } public E[] returnCompound(){ return solventCompound; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload