I'm trying to write a program that displays all prime numbers below the one entered by the user. The only requirement is that it must be multi threaded. This is my first time using Java and multiple threads. Can you help? It compiles, but the output is strange. Maybe it's an address?
import java.util.Scanner;
import java.util.Arrays;
public class prime {
public static void main(String[] args){
// get number from user
System.out.println("Enter a number: ");
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
RunPrime runprime1 = new RunPrime (num);
runprime1.start();
Thread.yield();
runprime1.SmallerPrimeNumbers();
}
}
class RunPrime extends Thread {
private int given_number;
RunPrime (int n) {
given_number = n;
}
public void SmallerPrimeNumbers() {
int count = 0;
for (int i = 0; i <= given_number; i++) {
if (CheckPrime(i)) {
count++;
}
}
for (int i = 0; i < count; i++) {
for (int j = 2; j <= given_number; j++) {
if (CheckPrime(j)) {
number[i] = j;
}
}
}
System.out.println(number);
}
public static boolean CheckPrime (int n) {
for (int i=2 ; i<n ; i++) {
if (n%i == 0)
return false;
}
return true;
}
}
Your SmallerPrimeNumbers() function has a second for-loop that doesn't look necessary. You're also assigning and printing the variable number that wasn't declared anywhere (this is probably what's causing you trouble). Since you're only printing them and not saving them, you can simplify the function like this:
public void SmallerPrimeNumbers() {
int count = 0;
for (int i = 0; i <= given_number; i++) {
if (CheckPrime(i)) {
System.out.println(i);
}
}
}
Related
I am trying to do the following:
I want to create an array and use methods executed by a thread. all in one class "embedded class"
A method to randomly find largest number which is 0 and then call it max. A method to find 0 and increment it max + 1.
After that calculate the sum of all array. Check the picture it is more clear.
That's what I've done so far but not working as intended
package javaapplication7;
import java.util.*;
import java.util.concurrent.*;
public class JavaApplication7 {
private static Array array = new Array();
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
Scanner input = new Scanner(System.in);
System.out.println("Enter number");
int num = input.nextInt();
int array[] = new int[num];
for (int i = 0; i < array.length; i++) {
array[i] = 0;
}
for (int i = 0; i < array.length; i++) {
System.out.println("array["+i+"]: "+array[i]);
}
for (int i = 0; i < num; i++) {
executor.execute(new MyThread());
}
executor.shutdown();
while(!executor.isTerminated()){
}
System.out.println("What is balance? ");
}
private static class MyThread implements Runnable{
#Override
public void run(){
array.insert();
}
}
private static class Array{
private int [] array;
private int maximum;
public void setArrayNumbers(){
for (int i = 0; i < array.length; i++) {
array[i] = 0;
}
}
public int getMax(){
return maximum;
}
public synchronized void insert(){
try{
for (int i = 0; i < array.length; i++) {
if(array[i] == 0){
array[i] = array[i] + 1;
}else if(array[i] == 1){
array[i] = array[i] + 1;
}else if(array[i] == 2){
array [i] = array[i] + 1;
}
}
Thread.sleep(100);
}
catch(Exception ex) {
System.out.println(ex);
}
}
}
}
Help me correcting my mistakes so i can understand multithreading better
Thank you!
First of all, stop to extend Thread. This is actually a very inconvenient way to handle multithreading in Java. There are much better things like the interfaces Runnable and Callable.
I just implemented this the way the exercise is written:
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Fun implements Runnable {
int[] numbers;
public Fun(int[] numbers)
{
this.numbers = numbers;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter the amount of threads: ");
final int qtyThreads = in.nextInt();
int[] numbers = new int[qtyThreads];
for(int i = 0; i<numbers.length; i++)
{
numbers[i] = 0;
}
ExecutorService exs = Executors.newFixedThreadPool(qtyThreads);
for(int i = 0; i<qtyThreads; i++)
{
exs.submit(new Fun(numbers));
}
// already awaits termination
exs.shutdown();
int sum = 0;
for(int number : numbers)
{
sum += number;
}
System.out.println("ArraySum: "+sum);
}
#Override
public void run() {
int max = -1;
for(int i = 0; i<numbers.length; i++)
{
if(numbers[i] > max)
{
max = numbers[i];
}
}
for(int i = 0; i<numbers.length; i++)
{
if(numbers[i] == 0)
{
numbers[i] = ++max;
break;
}
}
}
}
And here the part about thread-safety:
To implement this in this case, you'd have to lock the array each time you do the writing, so no thread get's concurrent modifications.
So in the run method we'd have a this.lock.lock(); before the second loop and a this.lock.unlock(); after the second loop.
The lock has the Type ReentrantLock and is passed to the constructor of Fun, so every Thread has the same lock instance.
The lock() method actually waits until the lock is available. If you do something wrong here and don't unlock the lock, you'll cause a deadlock situation.
Basically, when it comes to validate the sum, the value of the sum should always be sum(n) = sum(n-1)+n - you can find this out by either doing some maths stuff, or as I was never particular good in maths, I just did the following:
1: 1
2: 3 (+2)
3: 6 (+5)
4: 10 (+9)
5: 15 (+14)
Which then lead to the conclusion that the sum is always the previous sum plus the user input. This however is not how you do a mathematical proof.
If you want to check if your multithreaded solution is working properly, you can always write a singlethreaded one and compare these values - this is a lot easier than doing it on paper, which you wouldn't do for high numbers anyway:
public static int validateWithSingleThread(int n)
{
int[] numbers = new int[n];
for(int itt = 0; itt<n; itt++)
{
int max = -1;
for(int i = 0; i<numbers.length; i++)
{
if(numbers[i] > max)
{
max = numbers[i];
}
}
for(int i = 0; i<numbers.length; i++)
{
if(numbers[i] == 0)
{
numbers[i] = ++max;
}
}
}
int sum = 0;
for(int number : numbers)
{
sum += number;
}
return sum;
}
I am very new to Java and I am trying to iterate over an array of integers and get all multiples of 10. What I get with my code is the elements in the array printed 100 times since that is the length of the array. I know it is very basic but I just can't figure the problem out. This is what I have:
import java.util.Arrays;
public class ArrayThings {
public static void main(String[] args) {
int[] myFirstArray = new int[100];
for (int i = 0; i < myFirstArray.length; i++) {
myFirstArray[i] = i;
}
for (int i : myFirstArray) {
if (i % 10 == 0) {
myFirstArray[i] = i;
} else {
i++;
}
System.out.println(Arrays.toString(myFirstArray));
}
}
}
I think that is what you want to do :
public class ArrayThings {
public static void main(String[] args) {
int[] myFirstArray = new int[100];
// array generation
for (int i = 0; i < myFirstArray.length; i++) {
myFirstArray[i] = i;
}
// printing multiples of 10
for (int i = 0; i < myFirstArray.length; i++) {
if (i % 10 == 0 && i != 0) {
System.out.println(myFirstArray[i]);
}
}
}
}
In Java-8 you can do it like below:
int result[] = IntStream.range(1, 100).filter(e -> e%10==0).toArray();
System.out.println(Arrays.toString(result));
Why do you need an array for printing multiples of 10? You could simply do:
public class ArrayThings{
public static void main(String[]args){
for(int i=0; i<101; i++) {
if(i%10==0 && i != 0){
System.out.println(i);
}
}
}}
P.S. you are printing whole array and not that particular element, that's why you are getting a wrong output.
You should move your print statement outside of the for loop, that is what is causing it to print 100 times.
Also your current code seems to do absolutely nothing. You are checking the modulo of i, then setting the value of myFirstArray to that value of i. The current value of myFirstArray at i, is already equal to i, as initialized in the first loop.
This should work
import java.util.Arrays;
public class ArrayThings {
public static void main(String[] args) {
int[] myFirstArray = new int[100];
int[] myMultiplesArray = new int[9];
for (int i = 0; i < myFirstArray.length; i++) {
myFirstArray[i] = i;
}
int j = 0;
for (int i : myFirstArray) {
if (i % 10 == 0) {
myMultiplesArray[j] = i;
j++;
}
}
System.out.println(Arrays.toString(myMultiplesArray));
}
}
This program is supposed to print all the prime numbers up to an int that you enter, for example:
Enter a Number:
20
2
3
5
7
11
13
17
19
I just cannot get my program to work, I really don't know what to do, so if someone could review it and try to fix it, that would be greatly appreciated, thanks.
import java.util.Scanner;
public class PrimeGenerator {
public static void main(String args[]) {
Scanner k = new Scanner(System.in);
System.out.println("Enter an integer");
int number = k.nextInt();
PrimeGenerator matt = new PrimeGenerator();
System.out.println(matt.nextPrime(number));
}
private int number;
public PrimeGenerator(int n) {
number = n;
}
public int nextPrime(int number) {
for (int i = 1; i <= number; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
}
}
if (prime){
return i;
}
}
}
}
You're already there actually. You've just got a mistake in the program-flow.
for (int i = 1; i <= number; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
}
}
if (prime){
return i; //<-- this return will terminate nextPrim
}
}
Things to fix/improve:
nextPrim would need to return a value within every possible program-branch. This means: consider the case where nextPrim doesn't find any number in the given range and steps out of the loop. Now the program would be stuck without any return-value.
Instead of returning the first prim-number found, you could print that found prim-number and keep the generator running. Nice, easy and solves the hassle with returning anything, since you now can simply declare nextPrim as void. I'd recommend renaming it to printPrims or something like that to make this change clear.
Passing number: You can save a bit of effort by only passing number once to the prim-generator. The simplest solution would be to pass it to nextPrim/printPrims. Now you can remove the instance-variable number and the constructor, which solves the issue with th e signature of the constructor.
1 is not a prim-number to be pedantic. So let's be pedantic and start the outer loop in printPrims with 2, so that 2 will be the first number that is checked for being a prim.
So let's put this into code:
import java.util.Scanner;
public class PrimeGenerator {
public static void main(String args[]) {
Scanner k = new Scanner(System.in);
System.out.println("Enter an integer");
int number = k.nextInt();
PrimeGenerator matt = new PrimeGenerator();
matt.printPrims(number);
}
public void printPrime(int number) {
for (int i = 2; i <= number; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
}
}
if (prime){
System.out.println(i);
}
}
}
A few general hints:
Work through compiler errors. They tell you precisely where and what errors occur within your code.
Think about the flow of your program before even implementing it.
Break the task down into smaller tasks and implement these one after another. As an example: for this problem first print out all numbers in the range 2, number. Afterwards go a step further and add functionality to filter out prim-numbers. Now you've got two components that you can easily test independent of each other.
You were nearly there but your nextPrimes function was terminating prematurely when you returned i, try something like this:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
System.out.print("Enter an integer:");
int number = k.nextInt();
printPrimesUptoN(number);
}
public static void printPrimesUptoN(int n){
for(int i=2;i<n;i++){
boolean isPrime = true;
for(int j=2;j<i;j++){
if(i % j == 0){
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(i);
}
}
}
Try it here!
There's a few issues with your code, maybe we can fix it together. First things first, you're missing a return statement in nextPrime and there's no empty default constructor PrimeGenerator() because you created a single-arg constructor. Try this:
public class PrimeGenerator {
public static void main(String args[]) {
Scanner k = new Scanner(System.in);
System.out.println("Enter an integer");
int number = k.nextInt();
// you probably want to pass your maximum value to the constructor
PrimeGenerator matt = new PrimeGenerator(number);
// without a loop of some sort this will only print a single prime number, e.g.
// Enter a Number:
// 20
// 2
System.out.println(matt.nextPrime(number));
}
private int number;
public PrimeGenerator(int n) {
this.number = n;
}
// you're using the argument as upper boundary for your prime detection while not increasing your lower boundary
// also you're checking if i is a prime here which you always start at 1. this should always return the same value because once you find a prime number you return
// you should consider using an algorithm like Sieve of Eratosthenes (or advanced verions thereof) to determine if a given number is prime
public int nextPrime(int number) {
for (int i = 1; i <= number; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
}
}
if (prime){
return i;
}
}
// you need to return something at the end of this method or throw an exception
throw new IllegalStateException("no more prime numbers available!");
}
}
This is not a solution which will yield the results you're expecting but it will compile at least. From that point you can move forward and fix the algorithmic issues.
Your original error was in.
PrimeGenerator matt = new PrimeGenerator();
Error:
PrimeGenerator.java:7: error: constructor PrimeGenerator in class PrimeGenerator cannot be applied to given types;
PrimeGenerator matt = new PrimeGenerator();
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Notice that you had a method with the same name as you class, but I don't think you were using it as a constructor and if you were it took an int which you didn't give it. Your method on line 13 was:
public PrimeGenerator(int n) {
number = n;
}
Try doing
new PrimeGenerator().nextPrime(number);
Instead
import java.util.Scanner;
public class PrimeGenerator {
public static void main(String args[]) {
Scanner k = new Scanner(System.in);
System.out.println("Enter an integer");
int number = k.nextInt();
new PrimeGenerator().nextPrime(number);
}
public void nextPrime(int number) {
for (int i = 1; i <= number; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
}
}
if (prime){
System.out.println(i);
}
}
}
}
Optionally, to use your original constructor, you could separate this out.
import java.util.Scanner;
public class PrimeGenerator {
private int number;
public static void main(String args[]) {
Scanner k = new Scanner(System.in);
System.out.println("Enter an integer");
int number = k.nextInt();
// Initialize with a number.
PrimeGenerator pg = new PrimeGenerator(number);
pg.printPrimes();
}
// This is the constructer you were misusing.
public PrimeGenerator(int n) {
number = n;
}
public void printPrimes() {
// Actually use your private number variable.
for (int i = 1; i <= number; i++) {
boolean prime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
}
}
if (prime){
System.out.println(i);
}
}
}
}
There are several issues:
Are you using an IDE? If so, which is it, else Why not?. If you consider yourself a beginner, be advised to used one, like Eclipse IDE or NetBeans IDE.
Regarding to your algorithm, at first glance, there is a compilation and a logic problems.
Compilation problem is when the execution of your class not work. In this case the method nextPrime(int number) MUST return an int. Although you put return clause, the method is not returning any value when prime is false. You could easily spot this problem with an IDE.
Another compilation problem, the sentinel or variable i is INSIDE the for loop, and you are printing it's value OUTSIDE of it. Once again, the IDE would help you with this problem.
Logic problem is, you are returning a value if and only if the flag prime is true, so, when calling the method (supposing is working) you only obtain ONE value when you call System.out.println(matt.nextPrime(number));
The correct implementation would have these considerations:
The method doesn't return, i.e public void nextPrime(number) { ..., which means you don't need print in the main method, just call it.
The method actually prints the numbers.
In this way, you can check if i % j is different of zero, then you don't need to process more dividers, breaking the for loop.
Print it.
That's it.
public static void main(String [] args) {
.
.
.
PrimeGenerator matt = new PrimeGenerator();
matt.nextPrime(number);
}
public void printPrime(int number) {
boolean prime = true;
for (int i = 2; i <= number; i++) {
prime = true;
for (int j = 2; j < i; j++) {
if (i % j != 0) {
prime = false;
break;
}
}
if (prime) {
System.out.println(i);
}
}
}
my intend is to use simplest java (array and loops) to generate random numbers without duplicate...but the output turns out to be 10 repeating numbers, and I cannot figure out why.
Here is my code:
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
do {
for (int i=0; i<number.length; i++) {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
}
} while (!repeat);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
How about you use a Set instead? If you also want to keep track of the order of insertion you can use a LinkedHashSet.
Random r = new Random();
Set<Integer> uniqueNumbers = new HashSet<>();
while (uniqueNumbers.size()<10){
uniqueNumbers.add(r.nextInt(21));
}
for (Integer i : uniqueNumbers){
System.out.print(i+" ");
}
A Set in java is like an Array or an ArrayList except it handles duplicates for you. It will only add the Integer to the set if it doesn't already exist in the set. The class Set has similar methods to the Array that you can utilize. For example Set.size() is equivalent to the Array.length and Set.add(Integer) is semi-equivalent to Array[index] = value. Sets do not keep track of insertion order so they do not have an index. It is a very powerful tool in Java once you learn about it. ;)
Hope this helps!
You need to break out of the for loop if either of the conditions are met.
int[] number = new int[10];
int count=0;
int num;
Random r = new Random();
while(count<number.length){
num = r.nextInt(21);
boolean repeat=false;
do{
for(int i=0; i<number.length; i++){
if(num==number[i]){
repeat=true;
break;
}
else if(i==count){
number[count]=num;
count++;
repeat=true;
break;
}
}
}while(!repeat);
}
for(int j=0;j<number.length;j++){
System.out.print(number[j]+" ");
}
This will make YOUR code work but #gonzo proposed a better solution.
Your code will break the while loop under the condition: num == number[i].
This means that if the pseudo-generated number is equal to that positions value (the default int in java is 0), then the code will end execution.
On the second conditional, the expression num != number[i] is always true (otherwise the code would have entered the previous if), but, on the first run, when i == count (or i=0, and count=0) the repeat=true breaks the loop, and nothing else would happen, rendering the output something such as
0 0 0 0 0 0...
Try this:
int[] number = new int[10];
java.util.Random r = new java.util.Random();
for(int i=0; i<number.length; i++){
boolean repeat=false;
do{
repeat=false;
int num = r.nextInt(21);
for(int j=0; j<number.length; j++){
if(number[j]==num){
repeat=true;
}
}
if(!repeat) number[i]=num;
}while(repeat);
}
for (int k = 0; k < number.length; k++) {
System.out.print(number[k] + " ");
}
System.out.println();
Test it here.
I believe the problem is much easier to solve. You could use a List to check if the number has been generated or not (uniqueness). Here is a working block of code.
int count=0;
int num;
Random r = new Random();
List<Integer> numbers = new ArrayList<Integer>();
while (count<10) {
num = r.nextInt(21);
if(!numbers.contains(num) ) {
numbers.add(num);
count++;
}
}
for(int j=0;j<10;j++){
System.out.print(numbers.get(j)+" ");
}
}
Let's start with the most simple approach, putting 10 random - potentially duplicated - numbers into an array:
public class NonUniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
number[count++] = ThreadLocalRandom.current().nextInt(21);
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
So that gets you most of the way there, the only thing you know have to do is pick a number and check your array:
public class UniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
int candidate = ThreadLocalRandom.current().nextInt(21);
// Is candidate in our array already?
boolean exists = false;
for (int i = 0; i < count; i++) {
if (number[i] == candidate) {
exists = true;
break;
}
}
// We didn't find it, so we're good to add it to the array
if (!exists) {
number[count++] = candidate;
}
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
The problem is with your inner 'for' loop. Once the program finds a unique integer, it adds the integer to the array and then increments the count. On the next loop iteration, the new integer will be added again because (num != number[i] && i == count), eventually filling up the array with the same integer. The for loop needs to exit after adding the unique integer the first time.
But if we look at the construction more deeply, we see that the inner for loop is entirely unnecessary.
See the code below.
import java.util.*;
public class RandomDemo {
public static void main( String args[] ){
// create random object
Random r = new Random();
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
int i=0;
do {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
i++;
} while (!repeat && i < number.length);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
}
}
This would be my approach.
import java.util.Random;
public class uniquerandom {
public static void main(String[] args) {
Random rnd = new Random();
int qask[]=new int[10];
int it,i,t=0,in,flag;
for(it=0;;it++)
{
i=rnd.nextInt(11);
flag=0;
for(in=0;in<qask.length;in++)
{
if(i==qask[in])
{
flag=1;
break;
}
}
if(flag!=1)
{
qask[t++]=i;
}
if(t==10)
break;
}
for(it=0;it<qask.length;it++)
System.out.println(qask[it]);
}}
public String pickStringElement(ArrayList list, int... howMany) {
int counter = howMany.length > 0 ? howMany[0] : 1;
String returnString = "";
ArrayList previousVal = new ArrayList()
for (int i = 1; i <= counter; i++) {
Random rand = new Random()
for(int j=1; j <=list.size(); j++){
int newRand = rand.nextInt(list.size())
if (!previousVal.contains(newRand)){
previousVal.add(newRand)
returnString = returnString + (i>1 ? ", " + list.get(newRand) :list.get(newRand))
break
}
}
}
return returnString;
}
Create simple method and call it where you require-
private List<Integer> q_list = new ArrayList<>(); //declare list integer type
private void checkList(int size)
{
position = getRandom(list.size()); //generating random value less than size
if(q_list.contains(position)) { // check if list contains position
checkList(size); /// if it contains call checkList method again
}
else
{
q_list.add(position); // else add the position in the list
playAnimation(tv_questions, 0, list.get(position).getQuestion()); // task you want to perform after getting value
}
}
for getting random value this method is being called-
public static int getRandom(int max){
return (int) (Math.random()*max);
}
Trying to write a program that "rolls" dice and displays the results of the players and computer's rolls, as well as find how many of each number was rolled. Say, player rolls 3 4 3 5 6, then the player has a match composed of 2 3's. Haven't wrote the code to display the matching yet.
My problem is that I am trying to record the rolls to an ArrayList, then compare each number for the players and computers rolls from the ArrayList, and count up the number of each number's occurrence, but it I keep getting the error of
error: incomparable types: DieClass and int
Whenever I try to compare from the ArrayList
The program in question uses methods from the class DieClass
import java.util.ArrayList;
public class DieTester
{
private static ArrayList<DieClass> player = new ArrayList<DieClass>();
private static ArrayList<DieClass> computer = new ArrayList<DieClass>();
public static void main(String[] args)
{
for(int a = 1; a <= 5; a++)
{
DieClass roller = new DieClass();
player.add(roller);
}
for(int a = 1; a <= 5; a++)
{
DieClass roller = new DieClass();
computer.add(roller);
}
System.out.println("The user rolls: "+player);
System.out.println("The computer rolls: "+computer);
}
public String findMatching()
{
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
for(int i=1; i<player.size(); i++)
{
if(player.get(i)==1)
{
count1++;
}
else if(player.get(i)==2)
{
count2++;
}
else if(player.get(i)==3)
{
count3++;
}
else if(player.get(i)==4)
{
count4++;
}
else if(player.get(i)==5)
{
count5++;
}
}
for(int i=1; i<player.size(); i++)
{
if(computer.get(i)==1)
{
count1++;
}
else if(computer.get(i)==2)
{
count2++;
}
else if(computer.get(i)==3)
{
count3++;
}
else if(computer.get(i)==4)
{
count4++;
}
else if(computer.get(i)==5)
{
count5++;
}
}
}
}
Your problem is that you are comparing DieClass with Integers
if(player.get(i)==1)
For the counters, why don't you use an array of int? like
int counters [] = new int[6];
counters[2]++;
Please post DieClass, however, I think that your code should be like this.
for (DieClass dieClass : player) {
counters[dieClass.getNumber()-1]++; //Supose that DieClass has a getNumber method and set minus one because counters goes from 0 to 5
}