Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Counting occurences of ints in an ArrayList

Posted on 2011-03-21
Medium Priority
537 Views
I have an arraylist of 6000 ints.  Each int is a random number from 1-25.  I want to output the numbers 1-25 in order, from the number that occured the most to the one that occured the least.

I have been working on this for days, and I keep getting duplicates in my output.  I have gotten so frustrated that I decided that I would start from scratch.

I am probably making this process way more complicated than it has to be though.

this is example output I am going for:

1   2   3   4   5
6   7   8   9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

these numbers, again would start at the top left if they were the most frequently occurring.

I keep getting these two error riddled outputs for example:
duplicated numbers:
1   1   2   3   4
5   6   7   8   9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

and ommissions

1   1   2   3   4
5   6   7   8   9
10 11 12 13 14
15 16 17 18 19
20      22 23 24

Any help would be appreciated.  I'm sorry I had to write in the code short hand style, but the computer that I am working on cannot get internet where I am at.
``````//assume I have an arrayList called randomList with 6000#'s 1-25
//and I have declared ints to count each time they pop up in
//this for loop
for(int i=0;i<randList.size();i++)
{
if(randList.get(i)==1)
count1++;
}
for(int i=0;i<randList.size();i++)
{
if(randList.get(i)==2)
count1++;
}
//and so on for each number, 1-25.
int[] sortCounter = new int[25];
for(int i=0;i<sortCounter.length;i++)
{
for(int j=0;j<randList.size();j++)
{
if(randList.get(i)<=randList.get(j))
sortCounter[i]++;
}
//I forgot to mention that I have an arrayList of JLabels
//to store and display the results to the user in a 5*5 grid
//we will call it labelList
int labelPos=i;
labelPos++;//this is to display the right number to the usr
int previous=i;
previous--;//I use this to check the previous value
int counterIndex=count[i]
counterIndex--;
/*this is so the array index can't go out of bounds if the counter was incremented 25 times*/
if(i!=0)
{
if(count[i]!=count[previous])
labelList.get(counterIndex).setText(Integer.toString(labelPos));
else //this section takes care of ties in counters
{
counterIndex--;
labelList.get(counterIndex).setText(Integer.toString(labelPos));
}
else//if i==0
labelList.get(counterIndex).setText(Integer.toString(labelPos));
}
}
``````
0
Question by:prototrumpet
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 86

Expert Comment

ID: 35184512
You can print them out with this:
``````static void showFreq(List<Integer> ints) {
int[] freqs = new int[26];
for(Integer i : ints) {
freqs[i]++;
}
for(int i = 1;i < freqs.length;i++) {
System.out.printf("%4d ", freqs[i]);
if(i % 5 == 0) System.out.println();
}
}
``````
0

LVL 5

Expert Comment

ID: 35184595
and if you would like to sort your data ascending or descending you have two options that come to my mind. use two arrays, sort one and check at which position the values initially were to get the number (this has an issue: if e.g. number 14 and 23 occured both equally...)
or you could have a small class - have a look at the posting: http://www.coderanch.com/t/451158/java/java/Helping-Sorting-Array-Doubles-But
0

LVL 47

Expert Comment

ID: 35184603

This iis my long way of doing it,
but this should print them in decreasing order

If you post the list, I'll also test it

``````HashMap h = new HashMap();
ArrayList uniq = new ArrayList();

for(int j=0; j<list.size(); j++){
Integer i = (Integer)list.get(j);
if(h.contains(i)){
} else
Integer ii = (Integer)h.get(i);
int inum = ii.intValue();
h.put(i,new Integer(inum+1));
{
h.put(i, new Integer(1));
}
}

HashMap nh = new HashMap();
ArrayList nu = new ArrayList();

for(int j=0; j<uniq.size(); j++){
Integer i = (Integer)uniq.get(j);

Integer ii = (Integer) h.get(i);

if(nh.get(ii) != null){
ArrayList al = (ArrayList)nh.get(ii);
}else
{
ArrayList al = new ArrayLiat();
}

}

nu = Collections.sortReverse(nu);

int count = 0;
for(int jj=0; jj<nu.size(); jj++){
Integer i = (Integer) nu.get(jj);
ArrayList al = (ArayList) nh.get(i);

for(int j0=0; j0<al.size(); j0++){
System.out.println(al.get(j0) + " " + i);
count++;
}

if(count>25) break;

}
``````
0

LVL 47

Expert Comment

ID: 35184634

Corrected version:
``````list contains integerrs

HashMap h = new HashMap();
ArrayList uniq = new ArrayList();

for(int j=0; j<list.size(); j++){
Integer i = (Integer)list.get(j);
if(h.get(i) != null){

Integer ii = (Integer)h.get(i);
int inum = ii.intValue();
h.put(i,new Integer(inum+1));
} else
{
h.put(i, new Integer(1));
}
}

HashMap nh = new HashMap();
ArrayList nu = new ArrayList();

for(int j=0; j<uniq.size(); j++){
Integer i = (Integer)uniq.get(j);

Integer ii = (Integer) h.get(i);

if(nh.get(ii) != null){
ArrayList al = (ArrayList)nh.get(ii);
nh.put(ii,al);
}else
{
ArrayList al = new ArrayLiat();
nh.put(ii,al);
}

}

nu = Collections.sortReverse(nu);

int count = 0;
for(int jj=0; jj<nu.size(); jj++){
Integer i = (Integer) nu.get(jj);
ArrayList al = (ArayList) nh.get(i);

for(int j0=0; j0<al.size(); j0++){
System.out.println(al.get(j0) + " " + i);
count++;
}

if(count>25) break;

}
``````
0

Author Comment

ID: 35185055
I managed to get Internet for the computer I was originally working on the project with.

Here attached is the actual project file in its current state.

The method in question is on line 192, makeNumberList().

I am still learning, and I have no knowledge of hashing or using trees or things like that, so I'm not sure how to implement some of your suggestions and I'm sorry about that.  If that is the only way it could be done, then I will just have to hit the books for a while.

I created this using netbeans, so if you want to see what's happening just plug it in there and run it.  First hit generate new draws, then you will see what's going on.
lines 380 to 411 are where I am trying to set the labels based on the amount of times the numbers occurred so jumping there will save you a lot of sifting through the code.  Thanks again for your help, and I apologize for my ignorance
``````import java.awt.BorderLayout;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JLabel;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Cash25GUI.java
*
* Created on Mar 20, 2011, 1:08:24 PM
*/

/**
*
* @author Cliff
*/
public class Cash25GUI extends javax.swing.JFrame
{

/** Creates new form Cash25GUI */
protected static String hotCombo1="";
protected static String hotCombo2="";
protected static String hotCombo3="";

protected static String hotComboPercent1="";
protected static String hotComboPercent2="";
protected static String hotComboPercent3="";
static ArrayList<JLabel> hotNumLabels = new ArrayList<JLabel>();

static DecimalFormat fmt = new DecimalFormat("0.##");
public Cash25GUI()
{
initComponents();

}
public static void makeComboPercentFile()
{
try
{
}
catch(IOException exception){};
Scanner scan = new Scanner(fin);
FileOutputStream fout=null;
try
{
fout = new FileOutputStream("myComboPercents.txt");
}
catch(IOException exception) {};
PrintStream printStream = new PrintStream(fout);

ArrayList<String> myList = new ArrayList<String>();

while(scan.hasNextLine())

String a1="";
String b1="";
String c1="";
String d1="";
String e1="";
String f1="";
for(int a=1;a<=4;a++)
{
for(int b=1;b<=4;b++)
{
for(int c=1;c<=4;c++)
{
for(int d=1;d<=4;d++)
{
for(int e=1;e<=4;e++)
{
for(int f=1;f<=4;f++)
{
if(f>=e)
if(e>=d)
if(d>=c)
if(c>=b)
if(b>=a)
{

if(a==1)
a1="A";
else if(a==2)
a1="B";
else if(a==3)
a1="C";
else if(a==4)
a1="D";
if(b==1)
b1="A";
else if(b == 2)
b1="B";
else if(b == 3)
b1="C";
else if(b == 4)
b1="D";
if(c==1)
c1="A";
else if(c == 2)
c1="B";
else if(c == 3)
c1="C";
else if(c == 4)
c1="D";
if(d==1)
d1="A";
else if(d==2)
d1="B";
else if(d==3)
d1="C";
else if(d==4)
d1="D";
if(e==1)
e1="A";
else if(e==2)
e1="B";
else if(e==3)
e1="C";
else if(e==4)
e1="D";
if(f==1)
f1="A";
else if(f==2)
f1="B";
else if(f==3)
f1="C";
else if(f==4)
f1="D";

String toCheck = a1+" "+b1+" "+c1+" "+d1+" "+e1+" "+f1;
double occurences=0;
double elements=0;

for(elements=0;elements<myList.size();elements++)
{
if(myList.get((int) elements).equals(toCheck))
occurences++;
}
double frequency = (occurences/elements)*100.0;
printStream.println(toCheck+"\n"+frequency);

}
}
}
}
}
}
}
}
public static void makeNumberList()
{
try
{
}
catch(IOException exception){};
Scanner scan = new Scanner(fin);

int a=0;int b=0; int c=0; int d=0; int e=0; int f=0;

int one=0;int two=0;int three=0;int four=0;int five=0;
int six=0;int seven=0;int eight=0;int nine=0;int ten=0;
int eleven=0;int twelve=0;int thirteen=0;int fourteen=0; int fifteen=0;
int sixteen=0;int seventeen=0;int eighteen=0; int nineteen=0;int twenty=0;
int twentyOne=0;int twentyTwo=0;int twentyThree=0;int twentyFour=0;int twentyFive=0;

ArrayList<Integer> tempList = new ArrayList<Integer>();

for(int i=0;scan.hasNextLine();i++)
{
String line = scan.nextLine();
int count =0;
String tempA="";
String tempB="";
String tempC="";
String tempD="";
String tempE="";
String tempF="";

for(int z=0;z<=line.length();z++)
{
if(z==line.length())
{
f=Integer.parseInt(tempF);
}
else
{

String temp = Character.toString(line.charAt(z));
if(temp.equals(" ") == false && count == 0)
{
tempA=tempA+temp;

}
else if(temp.equals(" ")&&count==0)
{
a=Integer.parseInt(tempA);
count++;
}
else if(temp.equals(" ") == false && count == 1)
{
tempB=tempB+temp;
}
else if(temp.equals(" ")&&count==1)
{
b=Integer.parseInt(tempB);
count++;
}
else if(temp.equals(" ") == false && count == 2)
{
tempC=tempC+temp;
}
else if(temp.equals(" ")&&count==2)
{
c=Integer.parseInt(tempC);
count++;
}
else if(temp.equals(" ") == false && count == 3)
{
tempD=tempD+temp;
}
else if(temp.equals(" ")&&count==3)
{
d = Integer.parseInt(tempD);
count++;
}
else if(temp.equals(" ") == false && count == 4)
{
tempE=tempE+temp;
}
else if(temp.equals(" ")&&count==4)
{
e = Integer.parseInt(tempE);
count++;
}
else if(temp.equals(" ") == false && count == 5)
{
tempF=tempF+temp;
}
}
}

}
for(int i=0;i<tempList.size();i++)
{
if(tempList.get(i)==1)
one++;
if(tempList.get(i)==2)
two++;
if(tempList.get(i)==3)
three++;
if(tempList.get(i)==4)
four++;
if(tempList.get(i)==5)
five++;
if(tempList.get(i)==6)
six++;
if(tempList.get(i)==7)
seven++;
if(tempList.get(i)==8)
eight++;
if(tempList.get(i)==9)
nine++;
if(tempList.get(i)==10)
ten++;
if(tempList.get(i)==11)
eleven++;
if(tempList.get(i)==12)
twelve++;
if(tempList.get(i)==13)
thirteen++;
if(tempList.get(i)==14)
fourteen++;
if(tempList.get(i)==15)
fifteen++;
if(tempList.get(i)==16)
sixteen++;
if(tempList.get(i)==17)
seventeen++;
if(tempList.get(i)==18)
eighteen++;
if(tempList.get(i)==19)
nineteen++;
if(tempList.get(i)==20)
twenty++;
if(tempList.get(i)==21)
twentyOne++;
if(tempList.get(i)==22)
twentyTwo++;
if(tempList.get(i)==23)
twentyThree++;
if(tempList.get(i)==24)
twentyFour++;
if(tempList.get(i)==25)
twentyFive++;

//sort tempList into sorted List, then make a loop to assign values to hotNumList
}

int[] count = new int[25];

ArrayList<Integer> tempListCounter = new ArrayList<Integer>();

for(int i=0;i<count.length;i++)
{

for(int j=0;j<tempListCounter.size();j++)
{
if(tempListCounter.get(i)<=tempListCounter.get(j))
{

count[i]++;

}
}
int labelI = i;
labelI++;

int checkI=i;
checkI--;
int counterIndex=count[i];
counterIndex--;
if(i!=0)
{
if(count[i]!=count[checkI])
hotNumLabels.get(counterIndex).setText(Integer.toString(labelI));
else if(count[i]==count[checkI])
{
counterIndex--;
hotNumLabels.get(counterIndex).setText(Integer.toString(labelI));
}
}
else if(i == 0)
hotNumLabels.get(counterIndex).setText(Integer.toString(labelI));
}
/*
int[] biggestToSmallest= new int[25];

for(int i=0;i<tempListCounter.size();i++)
{
biggestToSmallest[i]=tempListCounter.get(i);
}
Arrays.sort(biggestToSmallest);

for(int i=biggestToSmallest.length-1;i>=0;i--)
{
if(biggestToSmallest[i]==one)
hotNumLabels.get(i).setText("1");
if(biggestToSmallest[i]==two)
hotNumLabels.get(i).setText("2");
if(biggestToSmallest[i]==three)
hotNumLabels.get(i).setText("3");
if(biggestToSmallest[i]==four)
hotNumLabels.get(i).setText("4");
if(biggestToSmallest[i]==five)
hotNumLabels.get(i).setText("5");
if(biggestToSmallest[i]==six)
hotNumLabels.get(i).setText("6");
if(biggestToSmallest[i]==seven)
hotNumLabels.get(i).setText("7");
if(biggestToSmallest[i]==eight)
hotNumLabels.get(i).setText("8");
if(biggestToSmallest[i]==nine)
hotNumLabels.get(i).setText("9");
if(biggestToSmallest[i]==ten)
hotNumLabels.get(i).setText("10");
if(biggestToSmallest[i]==eleven)
hotNumLabels.get(i).setText("11");
if(biggestToSmallest[i]==twelve)
hotNumLabels.get(i).setText("12");
if(biggestToSmallest[i]==thirteen)
hotNumLabels.get(i).setText("13");
if(biggestToSmallest[i]==fourteen)
hotNumLabels.get(i).setText("14");
if(biggestToSmallest[i]==fifteen)
hotNumLabels.get(i).setText("15");
if(biggestToSmallest[i]==sixteen)
hotNumLabels.get(i).setText("16");
if(biggestToSmallest[i]==seventeen)
hotNumLabels.get(i).setText("17");
if(biggestToSmallest[i]==eighteen)
hotNumLabels.get(i).setText("18");
if(biggestToSmallest[i]==nineteen)
hotNumLabels.get(i).setText("19");
if(biggestToSmallest[i]==twenty)
hotNumLabels.get(i).setText("20");
if(biggestToSmallest[i]==twentyOne)
hotNumLabels.get(i).setText("21");
if(biggestToSmallest[i]==twentyTwo)
hotNumLabels.get(i).setText("22");
if(biggestToSmallest[i]==twentyThree)
hotNumLabels.get(i).setText("23");
if(biggestToSmallest[i]==twentyFour)
hotNumLabels.get(i).setText("24");
if(biggestToSmallest[i]==twentyFive)
hotNumLabels.get(i).setText("25");
}*/
}

public static void makeLetterComboFile()
{
try
{
}
catch(IOException exception){};
Scanner scan = new Scanner(fin);
FileOutputStream fout=null;
try
{
fout = new FileOutputStream("myLetterCombos.text");
}
catch(IOException exception) {};
PrintStream printStream = new PrintStream(fout);

int a=0;int b=0; int c=0; int d=0; int e=0; int f=0;
String first=null;
String second=null;
String third=null;
String fourth=null;
String fifth=null;
String sixth=null;

for(int i=0;scan.hasNextLine();i++)
{
String line = scan.nextLine();
int count =0;
String tempA="";
String tempB="";
String tempC="";
String tempD="";
String tempE="";
String tempF="";

for(int z=0;z<=line.length();z++)
{
if(z==line.length())
{
f=Integer.parseInt(tempF);
}
else
{

String temp = Character.toString(line.charAt(z));
if(temp.equals(" ") == false && count == 0)
{
tempA=tempA+temp;

}
else if(temp.equals(" ")&&count==0)
{
a=Integer.parseInt(tempA);
count++;
}
else if(temp.equals(" ") == false && count == 1)
{
tempB=tempB+temp;
}
else if(temp.equals(" ")&&count==1)
{
b=Integer.parseInt(tempB);
count++;
}
else if(temp.equals(" ") == false && count == 2)
{
tempC=tempC+temp;
}
else if(temp.equals(" ")&&count==2)
{
c=Integer.parseInt(tempC);
count++;
}
else if(temp.equals(" ") == false && count == 3)
{
tempD=tempD+temp;
}
else if(temp.equals(" ")&&count==3)
{
d = Integer.parseInt(tempD);
count++;
}
else if(temp.equals(" ") == false && count == 4)
{
tempE=tempE+temp;
}
else if(temp.equals(" ")&&count==4)
{
e = Integer.parseInt(tempE);
count++;
}
else if(temp.equals(" ") == false && count == 5)
{
tempF=tempF+temp;
}
}
}

if(a<7)
first="A";
else if(a<13)
first="B";
else if(a<19)
first="C";
else
first="D";

if(b<7)
second="A";
else if(b<13)
second="B";
else if(b<19)
second="C";
else
second="D";

if(c<7)
third="A";
else if(c<13)
third="B";
else if(c<19)
third="C";
else
third="D";

if(d<7)
fourth="A";
else if(d<13)
fourth="B";
else if(d<19)
fourth="C";
else
fourth="D";

if(e<7)
fifth="A";
else if(e<13)
fifth="B";
else if(e<19)
fifth="C";
else
fifth="D";

if(f<7)
sixth="A";
else if(f<13)
sixth="B";
else if(f<19)
sixth="C";
else
sixth="D";

printStream.println(first+" "+second+" "+third+" "+fourth+" "+fifth+" "+sixth);
}
}
public static void makeHotCombos()
{
try
{
}
catch(IOException exception){};
Scanner scan = new Scanner(fin);
ArrayList<String> myList = new ArrayList<String>();

while(scan.hasNextLine())

String biggest1="";
String biggest2="";
String biggest3="";

double biggestPercent1=0;
double biggestPercent2=0;
double biggestPercent3=0;

for(int i=0;i<myList.size()-1;i++)
{
String combo = myList.get(i);
double comboPercent = Double.parseDouble(myList.get(++i));

if(comboPercent>=biggestPercent1)
{
biggest3=biggest2;
biggestPercent3=biggestPercent2;

biggest2=biggest1;
biggestPercent2=biggestPercent1;

biggest1=combo;
biggestPercent1=comboPercent;
}
else if(comboPercent>=biggestPercent2)
{
biggest3=biggest2;
biggestPercent3=biggestPercent2;

biggest2=combo;
biggestPercent2=comboPercent;
}
else if(comboPercent>=biggestPercent3)
{
biggest3=combo;
biggestPercent3=comboPercent;
}
}
hotCombo1=biggest1;
hotCombo2=biggest2;
hotCombo3=biggest3;
hotComboPercent1 = fmt.format(biggestPercent1);
hotComboPercent2 = fmt.format(biggestPercent2);
hotComboPercent3 = fmt.format(biggestPercent3);
}

public static void setHotCombos()
{
makeHotCombos();
hotComboLabel1.setText(hotCombo1);
hotComboLabel2.setText(hotCombo2);
hotComboLabel3.setText(hotCombo3);

hotComboPercentLabel1.setText(hotComboPercent1+" %");
hotComboPercentLabel2.setText(hotComboPercent2+" %");
hotComboPercentLabel3.setText(hotComboPercent3+" %");

}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

generateNewNumbers = new javax.swing.JButton();
checkNumbers = new javax.swing.JButton();
num1 = new javax.swing.JTextField();
num2 = new javax.swing.JTextField();
num4 = new javax.swing.JTextField();
num3 = new javax.swing.JTextField();
num5 = new javax.swing.JTextField();
num6 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
hitLabel1 = new javax.swing.JLabel();
hitLabel2 = new javax.swing.JLabel();
hitLabel3 = new javax.swing.JLabel();
hitLabel4 = new javax.swing.JLabel();
hitLabel5 = new javax.swing.JLabel();
hitLabel6 = new javax.swing.JLabel();
tipLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
hotComboLabel1 = new javax.swing.JLabel();
hotComboPercentLabel1 = new javax.swing.JLabel();
hotComboLabel2 = new javax.swing.JLabel();
hotComboPercentLabel2 = new javax.swing.JLabel();
hotComboLabel3 = new javax.swing.JLabel();
hotComboPercentLabel3 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
n1 = new javax.swing.JLabel();
n5 = new javax.swing.JLabel();
n4 = new javax.swing.JLabel();
n3 = new javax.swing.JLabel();
n2 = new javax.swing.JLabel();
n10 = new javax.swing.JLabel();
n6 = new javax.swing.JLabel();
n8 = new javax.swing.JLabel();
n7 = new javax.swing.JLabel();
n9 = new javax.swing.JLabel();
n14 = new javax.swing.JLabel();
n11 = new javax.swing.JLabel();
n12 = new javax.swing.JLabel();
n15 = new javax.swing.JLabel();
n13 = new javax.swing.JLabel();
n20 = new javax.swing.JLabel();
n16 = new javax.swing.JLabel();
n18 = new javax.swing.JLabel();
n19 = new javax.swing.JLabel();
n17 = new javax.swing.JLabel();
n21 = new javax.swing.JLabel();
n22 = new javax.swing.JLabel();
n24 = new javax.swing.JLabel();
n23 = new javax.swing.JLabel();
n25 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cash 25 Simulator");

generateNewNumbers.setFont(new java.awt.Font("Lucida Grande", 0, 18));
generateNewNumbers.setText("Generate New Draws");
public void actionPerformed(java.awt.event.ActionEvent evt) {
generateNewNumbersActionPerformed(evt);
}
});

checkNumbers.setFont(new java.awt.Font("Lucida Grande", 0, 18));
checkNumbers.setText("Check My Numbes");
public void actionPerformed(java.awt.event.ActionEvent evt) {
checkNumbersActionPerformed(evt);
}
});

num1.setFont(new java.awt.Font("Lucida Grande", 0, 24));
num1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
public void actionPerformed(java.awt.event.ActionEvent evt) {
num1ActionPerformed(evt);
}
});

num2.setFont(new java.awt.Font("Lucida Grande", 0, 24));
num2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
public void actionPerformed(java.awt.event.ActionEvent evt) {
num2ActionPerformed(evt);
}
});

num4.setFont(new java.awt.Font("Lucida Grande", 0, 24));
num4.setHorizontalAlignment(javax.swing.JTextField.CENTER);
public void actionPerformed(java.awt.event.ActionEvent evt) {
num4ActionPerformed(evt);
}
});

num3.setFont(new java.awt.Font("Lucida Grande", 0, 24));
num3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
public void actionPerformed(java.awt.event.ActionEvent evt) {
num3ActionPerformed(evt);
}
});

num5.setFont(new java.awt.Font("Lucida Grande", 0, 24));
num5.setHorizontalAlignment(javax.swing.JTextField.CENTER);
public void actionPerformed(java.awt.event.ActionEvent evt) {
num5ActionPerformed(evt);
}
});

num6.setFont(new java.awt.Font("Lucida Grande", 0, 24));
num6.setHorizontalAlignment(javax.swing.JTextField.CENTER);
public void actionPerformed(java.awt.event.ActionEvent evt) {
num6ActionPerformed(evt);
}
});

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Enter a number from 1 - 25 in each field, then hit the check numbers button to get");

tipLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
tipLabel1.setText("Not hitting much?  Tip: A = 1-6, B = 7-12, C = 13 - 18, and D = 19 - 25.");

jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Combinations in these formats are hot!");

jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("the percentage of times the numbers hit in 1000 different draws");

jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel4.setText("Here are all 25 numbers from hot to cold");

n1.setForeground(new java.awt.Color(255, 0, 51));
n1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n5.setForeground(new java.awt.Color(255, 0, 51));
n5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n4.setForeground(new java.awt.Color(255, 0, 51));
n4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n3.setForeground(new java.awt.Color(255, 0, 51));
n3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n2.setForeground(new java.awt.Color(255, 0, 51));
n2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n14.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n20.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n16.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n18.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n19.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n17.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n21.setForeground(new java.awt.Color(0, 51, 255));
n21.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n22.setForeground(new java.awt.Color(0, 51, 255));
n22.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n24.setForeground(new java.awt.Color(0, 51, 255));
n24.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n23.setForeground(new java.awt.Color(0, 51, 255));
n23.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

n25.setForeground(new java.awt.Color(0, 51, 255));
n25.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
);
jPanel1Layout.setVerticalGroup(
);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
);
layout.setVerticalGroup(
);

pack();
}// </editor-fold>

private void generateNewNumbersActionPerformed(java.awt.event.ActionEvent evt) {
Cash25();
setHotCombos();
makeNumberList();
}

private void checkNumbersActionPerformed(java.awt.event.ActionEvent evt) {
String aIn = num1.getText();
String bIn = num2.getText();
String cIn = num3.getText();
String dIn = num4.getText();
String eIn = num5.getText();
String fIn = num6.getText();

try
{
}
catch(IOException exception){};
Scanner scan = new Scanner(fin);

int a=0;int b=0; int c=0; int d=0; int e=0; int f=0;

int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
int ecount = 0;
int fcount = 0;
ArrayList<Integer> myList = new ArrayList<Integer>();
for(int i=0;scan.hasNextLine();i++)
{
String line = scan.nextLine();
int count =0;
String tempA="";
String tempB="";
String tempC="";
String tempD="";
String tempE="";
String tempF="";

for(int z=0;z<=line.length();z++)
{
if(z==line.length())
{
f=Integer.parseInt(tempF);
}
else
{

String temp = Character.toString(line.charAt(z));
if(temp.equals(" ") == false && count == 0)
{
tempA=tempA+temp;

}
else if(temp.equals(" ")&&count==0)
{
a=Integer.parseInt(tempA);
count++;
}
else if(temp.equals(" ") == false && count == 1)
{
tempB=tempB+temp;
}
else if(temp.equals(" ")&&count==1)
{
b=Integer.parseInt(tempB);
count++;
}
else if(temp.equals(" ") == false && count == 2)
{
tempC=tempC+temp;
}
else if(temp.equals(" ")&&count==2)
{
c=Integer.parseInt(tempC);
count++;
}
else if(temp.equals(" ") == false && count == 3)
{
tempD=tempD+temp;
}
else if(temp.equals(" ")&&count==3)
{
d = Integer.parseInt(tempD);
count++;
}
else if(temp.equals(" ") == false && count == 4)
{
tempE=tempE+temp;
}
else if(temp.equals(" ")&&count==4)
{
e = Integer.parseInt(tempE);
count++;
}
else if(temp.equals(" ") == false && count == 5)
{
tempF=tempF+temp;
}
}
}

}

for(int i=0;i<myList.size();i++)
{
System.out.println(myList.get(i));
if(myList.get(i)==Integer.parseInt(aIn))
acount++;
if(myList.get(i)==Integer.parseInt(bIn))
bcount++;
if(myList.get(i)==Integer.parseInt(cIn))
ccount++;
if(myList.get(i)==Integer.parseInt(dIn))
dcount++;
if(myList.get(i)==Integer.parseInt(eIn))
ecount++;
if(myList.get(i)==Integer.parseInt(fIn))
fcount++;
}

if(acount>0)
num1.setBackground(Color.green);
if(bcount>0)
num2.setBackground(Color.green);
if(ccount>0)
num3.setBackground(Color.green);
if(dcount>0)
num4.setBackground(Color.green);
if(ecount>0)
num5.setBackground(Color.green);
if(fcount>0)
num6.setBackground(Color.green);

hitLabel1.setText(fmt.format(acount/10)+" %");
hitLabel2.setText(fmt.format(bcount/10)+" %");
hitLabel3.setText(fmt.format(ccount/10)+" %");
hitLabel4.setText(fmt.format(dcount/10)+" %");
hitLabel5.setText(fmt.format(ecount/10)+" %");
hitLabel6.setText(fmt.format(fcount/10)+" %");
}

private void num1ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void num2ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void num4ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void num3ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void num5ActionPerformed(java.awt.event.ActionEvent evt) {
}

private void num6ActionPerformed(java.awt.event.ActionEvent evt) {
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Cash25GUI().setVisible(true);
}
});
}
public static void Cash25()
{

MakeRandomDraws myDraws = new MakeRandomDraws();
myDraws.makeRandomDrawsFile();
makeLetterComboFile();
makeComboPercentFile();
}

// Variables declaration - do not modify
private javax.swing.JButton checkNumbers;
private javax.swing.JButton generateNewNumbers;
private javax.swing.JLabel hitLabel1;
private javax.swing.JLabel hitLabel2;
private javax.swing.JLabel hitLabel3;
private javax.swing.JLabel hitLabel4;
private javax.swing.JLabel hitLabel5;
private javax.swing.JLabel hitLabel6;
private static javax.swing.JLabel hotComboLabel1;
private static javax.swing.JLabel hotComboLabel2;
private static javax.swing.JLabel hotComboLabel3;
private static javax.swing.JLabel hotComboPercentLabel1;
private static javax.swing.JLabel hotComboPercentLabel2;
private static javax.swing.JLabel hotComboPercentLabel3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel n1;
private javax.swing.JLabel n10;
private javax.swing.JLabel n11;
private javax.swing.JLabel n12;
private javax.swing.JLabel n13;
private javax.swing.JLabel n14;
private javax.swing.JLabel n15;
private javax.swing.JLabel n16;
private javax.swing.JLabel n17;
private javax.swing.JLabel n18;
private javax.swing.JLabel n19;
private javax.swing.JLabel n2;
private javax.swing.JLabel n20;
private javax.swing.JLabel n21;
private javax.swing.JLabel n22;
private javax.swing.JLabel n23;
private javax.swing.JLabel n24;
private javax.swing.JLabel n25;
private javax.swing.JLabel n3;
private javax.swing.JLabel n4;
private javax.swing.JLabel n5;
private javax.swing.JLabel n6;
private javax.swing.JLabel n7;
private javax.swing.JLabel n8;
private javax.swing.JLabel n9;
private javax.swing.JTextField num1;
private javax.swing.JTextField num2;
private javax.swing.JTextField num3;
private javax.swing.JTextField num4;
private javax.swing.JTextField num5;
private javax.swing.JTextField num6;
private javax.swing.JLabel tipLabel1;
// End of variables declaration

}
``````
0

LVL 47

Expert Comment

ID: 35185076
Do you have the list of numbers which you need to count ocurrences;
can you post just that list?
0

LVL 47

Assisted Solution

for_yan earned 2000 total points
ID: 35185120

This is the code which I tested:

``````import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class CountIntOccurences {

public CountIntOccurences(){

ArrayList list = null;
list = new ArrayList();

int [] myInt = {2,3,4,4,4,5,5,3};
for(int j=0; j<myInt.length; j++){
}

HashMap h = new HashMap();
ArrayList uniq = new ArrayList();

for(int j=0; j<list.size(); j++){
Integer i = (Integer)list.get(j);
if(h.get(i) != null){

Integer ii = (Integer)h.get(i);
int inum = ii.intValue();
h.put(i,new Integer(inum+1));
} else
{
h.put(i, new Integer(1));
}
}

HashMap nh = new HashMap();
ArrayList nu = new ArrayList();

for(int j=0; j<uniq.size(); j++){
Integer i = (Integer)uniq.get(j);

Integer ii = (Integer) h.get(i);

if(nh.get(ii) != null){
ArrayList al = (ArrayList)nh.get(ii);
nh.put(ii,al);
}else
{
ArrayList al = new ArrayList();
nh.put(ii,al);
}

}

Collections.sort(nu);

Collections.reverse(nu);

int count = 0;
for(int jj=0; jj<nu.size(); jj++){
Integer i = (Integer) nu.get(jj);
ArrayList al = (ArrayList) nh.get(i);

for(int j0=0; j0<al.size(); j0++){
System.out.println(al.get(j0) + " " + i);
count++;
}

if(count>25) break;

}

}

public static void main(String [] args){

new CountIntOccurences();
}

}
``````

This is the result:

``````4 3
3 2
5 2
2 1
``````

Paste your own list and check.
Let me know if you have any questions
0

Author Comment

ID: 35185143
Here is my List, its a text file that I read with the method.

by hitting the generate new draws button, you can make your own when you run the file though
myfile.txt
0

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 35185204

There you go:

``````import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;

public class CountIntOccurences {

public CountIntOccurences(){

ArrayList list = null;
list = new ArrayList();

try {
DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\test\\myIntList.txt"));

String buff = null;
StringTokenizer t = new StringTokenizer(buff);

}

}  catch(Exception ex){
System.out.println(ex.toString());
ex.printStackTrace();
}
/*
int [] myInt = {2,3,4,4,4,5,5,3};
for(int j=0; j<myInt.length; j++){
}
*/

HashMap h = new HashMap();
ArrayList uniq = new ArrayList();

for(int j=0; j<list.size(); j++){
Integer i = (Integer)list.get(j);
if(h.get(i) != null){

Integer ii = (Integer)h.get(i);
int inum = ii.intValue();
h.put(i,new Integer(inum+1));
} else
{
h.put(i, new Integer(1));
}
}

HashMap nh = new HashMap();
ArrayList nu = new ArrayList();

for(int j=0; j<uniq.size(); j++){
Integer i = (Integer)uniq.get(j);

Integer ii = (Integer) h.get(i);

if(nh.get(ii) != null){
ArrayList al = (ArrayList)nh.get(ii);
nh.put(ii,al);
}else
{
ArrayList al = new ArrayList();
nh.put(ii,al);
}

}

Collections.sort(nu);

Collections.reverse(nu);

int count = 0;
for(int jj=0; jj<nu.size(); jj++){
Integer i = (Integer) nu.get(jj);
ArrayList al = (ArrayList) nh.get(i);

for(int j0=0; j0<al.size(); j0++){
System.out.println(al.get(j0) + " " + i);
count++;
}

if(count>25) break;

}

}

public static void main(String [] args){

new CountIntOccurences();
}

}
``````

Result (even checked with the editor - indeed 816 occurences of 25):

``````25 816
24 814
23 740
22 618
21 482
20 412
19 264
18 235
17 182
16 175
15 144
14 119
13 117
12 105
11 96
9 90
10 89
8 79
5 74
6 69
7 63
4 63
1 54
3 52
2 48
``````
0

LVL 47

Assisted Solution

for_yan earned 2000 total points
ID: 35185331
Explanation of the above code:

First we read the file and make ArrayList list

Then we create HashMap called "h" -
it has your integers as keys and number of occurrences for each integer as values

Simulatneously we create ArrayList "uniq" whaich has all your integers but each one only one time - just dsitinct integers
In fact this is the list of keys of hashMap "h"

Then we go second time and make another HashMap "nh"
This one has frequencies as keys and ArrayList's conatining all integers which have such fequency as a value

Simulatebnously creating ArrayList of frequencies - the keys to HashMap nh

Then we sort list of frequencies - from low to high (Collections.sort)
Then we reverse this list Collections, reverse)

then we go therough the list of frequencies from hig to low
and for each frequency retrieve ArrayList of integers which have such frequency
and print the pairs - first integer - then frequecy

Count the numre of rows if after some frequency we have more than 25 rows - stop printing
You may happen to have more than 25 rows in case the same frequency applies to several integers

Let me know if you have any questions.
To understand this you just need to know what is ArrayList and what is HashMap - nothing else

0

Author Comment

ID: 35185458
It works!!!

Thanks Everyone!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to aâ€¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop: