0

I'm trying to implement an algorithm in one method that takes in two lists of type studentlist and I need to access the array from the constructor method, studentList to get the size and the student numbers to compare between lists.

e.g.

for (int i = 0; i < L1.studentID.length-1; i++) {
    for (int j = 0; j < L2.studentID.length-1; j++) {
        if (L1.studentID[i] = L2.studentID[j]) {
            num++; }}}

Most of the constructor, and not including the 2nd one for accessing lists in a file instead of randomly generated:

studentID=new int[size];
boolean[] usedID=new boolean[IDrange];
for (int i=0;i<IDrange;i++) usedID[i]=false;
for (int i=0;i<size;i++) {
    int t;
    do {
    t=(int)(Math.random()*IDrange);
    } while (usedID[t]);
    usedID[t]=true;
    studentID[i]=t; }

Size (list.studentID.length or I could have used list.numberOfStudents which is built in) seems fine, but I'm having trouble with getting the elements of the array themselves. I figure I could just do list.studentID[i] but I get a 'type mismatch: cannot convert from int to boolean'.

Any ideas?

1
  • 2
    why not L1.studentID[i] == L2.studentID[j]? //double '=' Commented Jan 25, 2013 at 15:11

3 Answers 3

2

Your issue is here:

if (L1.studentID[i] = L2.studentID[j]) 

You are making an attribution of L2.studentID[j] to L1.studentID[i] and then passing the value of L1.studentID[i] to the if

The if in java expects explicittly a boolean value.

You either want to do:

if (L1.studentID[i] == L2.studentID[j])

or

if ((L1.studentID[i] = L2.studentID[j])!=0)

The code you currently have is the same as doing:

L1.studentID[i] = L2.studentID[j];
if (L1.studentID[i]){  <-- this won't work because L1.studentID[i] is an int and the if expects a boolean
...
Sign up to request clarification or add additional context in comments.

1 Comment

Oh yea, completely forgot I had to do that! Thanks everyone, thought it had something to do with the boolean in the constructor or something.
2

You should compare them like this

if (L1.studentID[i] == L2.studentID[j])

Otherwise you're doing an attribution of L2 to L1.

Comments

2

'type mismatch: cannot convert from int to boolean'

You are getting an int because you are doing an assignment: = operator. To compare two integers you need to use == operator.

Try with

if (L1.studentID[i] == L2.studentID[j]) {

and you'll get a boolean.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.