Note that there are some explanatory texts on larger screens.

plurals
  1. POcould some help me find why my code couldn't run
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/13445021/java-code-with-tests-infinite-loop">Java code with tests - infinite loop?</a> </p> </blockquote> <p>Here is my code that I want to get the relationship between people, however, when I run unit test, the test ran forever and couldn't get the result, and my cpu using was high. Here is my code. Could someone see what's wrong with it? the string relations are multiple line inputs of string with a format "A , B" +\n" + "C , D" where A is the parent of B and C is the parent of D.</p> <p>this is the default constructor for the code and is the input format of string, we don't need to check if the format is correct</p> <pre><code> public SeeRelations(String relations){ this.relations = relations; } </code></pre> <p>//helper function to get each line of the string</p> <pre><code> private ArrayList&lt;String&gt; lineRelations(){ int i; ArrayList&lt;String&gt; lineRelations = new ArrayList&lt;String&gt;(); String[] lines = relations.split("\n"); for(i = 0; i &lt; lines.length; i++){ lineRelations.add(lines[i]); } return lineRelations; } </code></pre> <p>//helper function to put each of the relationship in arraylists </p> <pre><code> private ArrayList&lt;ArrayList&lt;String&gt;&gt; allRelations(){ int i; ArrayList&lt;ArrayList&lt;String&gt;&gt; allRelations = new ArrayList&lt;ArrayList&lt;String&gt;&gt;(); ArrayList&lt;String&gt; lineRelations = lineRelations(); for(i = 0; i &lt; lineRelations.size(); i++){ ArrayList&lt;String&gt; eachLine = new ArrayList&lt;String&gt;(Arrays.asList(lineRelations.get(i).split("\\s*,\\s*"))); allRelations.add(eachLine); } return allRelations; } </code></pre> <p>this is the method to check if the input name is existent //helper function to see if the name exist for seeRelations() </p> <pre><code> private boolean hasThisName(String name){ ArrayList&lt;ArrayList&lt;String&gt;&gt; allRelations = allRelations(); int i; int j; for(i = 0; i &lt; allRelations.size(); i++){ for(j = 0; j &lt; allRelations.get(i).size(); j++){ if(name.equals(allRelations.get(i).get(j))){ return true; } } } return false; } </code></pre> <p>this is the function to get the generation number between two people //helper function to get Generation number of seeRelations()</p> <pre><code> private int getGenerationNum(String person, String ancestor){ ArrayList&lt;ArrayList&lt;String&gt;&gt; allRelations = allRelations(); String name; int i; int j; int generationNum = 0; for(i = 0, j = 0, name = ancestor; i &lt; allRelations.size(); i++){ if(name.equals(allRelations.get(i).get(0)) &amp;&amp; !person.equals(allRelations.get(i).get(1))){ generationNum++; ancestor = allRelations.get(i).get(1); i = 0; j = 1; } else if(ancestor.equals(allRelations.get(i).get(0)) &amp;&amp; person.equals(allRelations.get(i).get(1))){ generationNum++; j = 1; break; } } if(j == 0){ return 0; } else{ return generationNum; } } </code></pre> <p>this is the method to get multiple of "great" for the final output</p> <pre><code> private String great(int num){ int i; String great = ""; for(i = 0; i &lt; num; i++){ great += "great"; } return great; } </code></pre> <p>this is my final method to check the relationship between two people </p> <pre><code> public String seeRelations(String person, String ancestor){ int generationNum = getGenerationNum(person, ancestor); String great = great(generationNum - 2); if(!(hasThisName(person) &amp;&amp; hasThisName(ancestor))){ return null; } else{ if(generationNum == 0){ return null; } else if(generationNum == 1){ return ancestor + " is the parent of " + person; } else if(generationNum == 2){ return ancestor + " is the grandparent of " + person; } else{ return ancestor + " is the" + " " + great +"grandparent of " + person; } } } </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.
 

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