Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're suffering from some design issues. Specifically, you're conflating the task of getting your numbers with the task of counting frequency.</p> <pre><code>import java.util.Scanner; public class Ex1partA { public static void main(String[] args) { Scanner kbin = new Scanner(System.in); //Get all inputs List&lt;Integer&gt; inputs = getInputs(10,19); //Count the frequency Map&lt;Integer, Integer&gt; freq = countFrequency(inputs); for (int key : freq.keySet()) { System.out.println(key + ": " + freq.get(key)); } } public static List&lt;Integer&gt; getInputs(int min, int max) { List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(); System.out.print("\n\tInput numbers from 10 to 19: \n"); int i=0; while (i &lt; 50) { Integer newNumber = kbin.nextInt(); if (newNumber &lt; max &amp;&amp; newNumber &gt; min) { list.add(newNumber); } else { System.out.println("Bad number. Ignoring.");//Note this simply skips, does not ask for re-entry of bad numbers } i++; } return list; } // Finds the frequency of the input list and out puts it as // a map of number =&gt; how many times that number appears public static Map&lt;Integer, Integer&gt; countFrequency(List&lt;Integer&gt; input) { Map&lt;Integer,Integer&gt; freq = new HashMap&lt;Integer,Integer&gt;(); for (Integer val : input) { Integer currentCount = freq.get(val); freq.put(val, currentCount + 1); } return freq; } } </code></pre> <p>Lets note a few key points:</p> <ul> <li>Each function should do exactly ONE thing, if at all possible. So here we have a function that gets all the inputs (within a certain range) and another that counts the frequency of a list of numbers.</li> <li>Using full objects rather than primitive arrays gives you a lot of flexibility and power without overhead of you having to manually massage data. </li> <li>In particular, the concept of a <code>Map</code> is important here. You are 'mapping' a key (which in this case is the input value provided by the user of a number between 10 and 19) with a value (which in this case is the number of times that number appears in the list of numbers you have).</li> <li>&amp;&amp; rather than &amp; to apply the boolean AND to two other boolean operators.</li> <li>Note that the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html" rel="nofollow">Collections API</a> provides frequency-counting methods that would allow you to avoid doing the mapping yourself. </li> </ul>
 

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