Reputation: 539
I would like my print statement to be outside the loop so the statement doesn't print the same thing over and over. The for loop below simply checks a number from one array against another to find out how many matches have been found. Defining the variables above and printing the statements below results in a "variable not initialised error" which is understandable.
for (int i = 0; i < 6; i++)
{
int chkNum = myArray[i];
int lottMtch = count(chkNum, rndNum);
if (lottMtch > 0)
{
System.out.println(lottMtch + "matches found");
System.out.print(chkNum);
}
else {
System.out.print("no matches found");
}
}
Upvotes: 2
Views: 3048
Reputation: 5810
this would not really make sense .. if you want than Try this ...
int lottMtch[]=new int[myArray.length];
Arrays.fill(lottMtch, 0);
for (int i = 0; i < 6; i++)
{
int chkNum = myArray[i];
lottMtch[i] = count(chkNum, rndNum);
}
for (int i = 0; i < 6; i++)
{
if (lottMtch[i] > 0)
System.out.println(lottMtch[i] + " matches found "+ myArray[i]);
}
If you wan to found just how many match of rndNum
in myArray
Than try this
Here i assume rndNm
is global
int lottMtch=0;
for (int i = 0; i < 6; i++)
{
lottMtch += count(myArray[i], rndNum);
}
if (lottMtch> 0)
System.out.println(lottMtch + " matches found "+ rndNum);
As per discussed in comment Try this ..
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int i = 0; i < 6; i++)
{
Integer chkNum = myArray[i];
Integer cnt = (Integer)count(myArray[i], rndNum);
if(cnt>0)
{
if(map.get(chkNum)==null)
map.put(chkNum,1);
else
map.put(chkNum, map.get(chkNum)+1);
}
}
for (Object key : map.keySet())
System.out.println(map.get(key) + " matches found "+key.toString());
Upvotes: 2
Reputation: 669
You need to initialise variables outside the loop. Try this:
int chkNum = 0;
int lottMtch = 0;
for (int i = 0; i < 6; i++)
{
chkNum = myArray[i];
lottMtch = count(chkNum, rndNum);
}
if (lottMtch > 0)
{
System.out.println(lottMtch + "matches found");
System.out.print(chkNum);
}
else {
System.out.print("no matches found");
}
Upvotes: -2
Reputation: 14709
This is because if you only declare the variables above the loop and only initialize said variables in the loop, when you attempt to print them outside of the loop, there's no guarantee that they would have been initialized.
So, perhaps you want something like this:
int lottMtch = 0;
for (int i = 0; i < 6; i++)
{
int chkNum = myArray[i];
lottMtch += count(chkNum, rndNum);
//System.out.print(chkNum); this would not really make sense outside of the loop
}
if (lottMtch > 0)
{
System.out.println(lottMtch + "matches found");
}
else
{
System.out.print("no matches found");
}
Upvotes: 2
Reputation:
Declare the variable before the loop and then do your stuff in the loops, like adding one to the variable if it is found, then print it out afterwards if it is more than 0. Something like this...
int var = 0;
for(...) {
if(found)
var++;
}
if(var > 0)
sysout(var);
Of course this code won't work but it is a start. For your learning experience I will let you implement this idea with your code.
Upvotes: 3
Reputation:
You need to declare a variable outside the loop if you plan to access it after the loop exits.
Upvotes: 0