#int[] cannot be converted to Iterator

1 messages · Page 1 of 1 (latest)

silk fossil
#

The error I get is: FClass.java:53: error: incompatible types: int[] cannot be converted to Iterator Iterator i = sObj.get_Iterator(); => 1 error
I will provide the code for better context, and all I can add is that I need a way to make an Iterator Object and return it.
import java.util.*;

interface Iterator{
public boolean has_next();
public Object get_next();
}

class Sequence{
private final int maxLimit = 80;
private SeqIterator _iter = null;
int[] iArr;
int size;
Sequence(int size){
this.size = size;
}
//implement addTo(elem) to add an int elem to the sequence
public void addTo(int elem){
iArr[iArr.length - 1] = elem;
}
//implement get_Iterator() to return Iterator object
public int[] get_Iterator(){
return iArr;
}
private class SeqIterator implements Iterator{
int indx;
public SeqIterator(){
indx = -1;
}
//implement has_next()
public boolean has_next(){
indx = indx + 1;
if(indx+1 <= size-1){
return true;
}
else{
return false;
}
}
//implement get_next()
public Object get_next(){
return iArr[indx];
}
}
}

class FClass{
public static void main(String[] args) {
Sequence sObj = new Sequence(5);
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
sObj.addTo(sc.nextInt());
}
Iterator i = sObj.get_Iterator();
while(i.has_next())
System.out.print(i.get_next() + ", ");
}
}

mellow fableBOT
#

<@&987246399047479336> please have a look, thanks.

mellow fableBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

mellow fableBOT
#

int[] cannot be converted to Iterator

#

Changed the title to int[] cannot be converted to Iterator.

mellow fableBOT
# silk fossil The error I get is: FClass.java:53: error: incompatible types: int[] cannot be ...

Detected code, here are some useful tools:

Formatted code
The error I get is : FClass.java : 53 : error : incompatible types : int [] cannot be converted to Iterator Iterator i = sObj.get_Iterator();
 =  > 1error I will provide the codefor better context, and all I can add is that I need a way to make an Iterator Object andreturn it.import java.util. * ; 
interface Iterator {
  public boolean has_next(); public Object get_next();
}
class Sequence {
  private final int maxLimit = 80;
  private SeqIterator_iter = null ;
  int [] iArr;
  int size;
  Sequence(int size) {
    this .size = size;
  }
  //implement addTo(elem) to add an int elem to the sequence 
  public void addTo(int elem) {
    iArr[iArr.length - 1]  = elem;
  }
  //implement get_Iterator() to return Iterator object
  public int [] get_Iterator() {
    return iArr;
  }
  private class SeqIterator implements Iterator {
    int indx;
    public SeqIterator() {
      indx =  - 1;
    }
    //implement has_next()
    public boolean has_next() {
      indx = indx + 1;
      if (indx + 1 <= size - 1) {
        return true;
      }
      else {
        return false;
      }
    }
    //implement get_next()
    public Object get_next() {
      return iArr[indx] ;
    }
  }
}
class FClass {
  public static void main(String[] args) {
    Sequence sObj = new Sequence(5);
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < 5; i++) {
      sObj.addTo(sc.nextInt());
    }
    Iterator i = sObj.get_Iterator();
    while (i.has_next()) System.out.print(i.get_next() + ", ");
  }
}
copper orbit
#

//implement get_Iterator() to return Iterator object
public int [] get_Iterator() {
return iArr;
}

#

it literally says to return an iterator

#

but u return int[]

#

which is not an iterator

#

hence the error

silk fossil
#

it does not work either

copper orbit
#

yeah bc of what i just said

silk fossil
#

I tried earlier

copper orbit
#

there are actually multiple issues in ur code and it feels like u dont really know what ur doing there. seems a bit wild west and trial&error

#

u should probably step back and learn more about some basics

#

anyways. ur get iterator method is supposed to return a sequence iterator instance

#

not the array

silk fossil
#

yeah sorry. I dont know java, I do my work with python. This is the only time I have to do something with Java. Anyways thanks

fossil plinth
#

also is this an assignment ?

#

I don't know what you are doing

#

but you created an interface called Iterator, but you also imported java.util.* which means that you know have two Iterator clashing

#

this cannot work

#

also hash_next shouldn't increemnt, but get_next should

#

because has_next shouldn't have a side effect

#

and instead of putting comments, please add the override annotation

silk fossil
fossil plinth
#

and the naming is wrong, it should be camelCase, not snake_case

#

indx+1 <= size-1
and this condition is wrong

#

and you shouldn't return Object

fossil plinth
silk fossil
#

till int size and none of Fclass

fossil plinth
#

...ok

#

well let's first fix the compilation error

#

as zabu said, you are supposed to return an iterator, an array is not an iterator

#

do you understand that ?

#

@silk fossil

silk fossil
#

Well yes, kind of, Im still learning

#

I get what I need to do

#

Im unaware of the syntax

fossil plinth
silk fossil
#

return an iterator object and make it work

fossil plinth
silk fossil
#

there isn;t one

fossil plinth
#

there is

#

you created its class

silk fossil
#

Seqiterator

fossil plinth
#

yes

silk fossil
#

Iterator get_Iterator() {
if (_iter == null) {
_iter = new SeqIterator();
}
return _iter;
}

fossil plinth
#

but

#

why assigning the iterator to itr 🤔

silk fossil
#

what do you mean

fossil plinth
keen crater
#
import java.util.*;

interface Iterator{
    public boolean has_next();
    public Object get_next();
}

class Sequence{
    private final int maxLimit = 80;
    private SeqIterator _iter = null;
    int[] iArr; 
    int size;
    Sequence(int size){
        this.size = size;
    }
//implement addTo(elem) to add an int elem to the sequence 
    public void addTo(int elem){
        iArr[iArr.length - 1] = elem;
    }
//implement get_Iterator() to return Iterator object
    public int[] get_Iterator(){
        return iArr;
    }
    private class SeqIterator implements Iterator{
        int indx;
        public SeqIterator(){
            indx = -1;
        }
        //implement has_next()
        public boolean has_next(){
            indx = indx + 1;
            if(indx+1 <= size-1){
                return true;
            }
            else{
                return false;
            }
        }
        //implement get_next()
        public Object get_next(){
            return iArr[indx];
        }
    }
}

class FClass{
    public static void main(String[] args) {
        Sequence sObj = new Sequence(5);
        Scanner sc = new Scanner(System.in); 
        for(int i = 0; i < 5; i++) {
            sObj.addTo(sc.nextInt());
        }
        Iterator i = sObj.get_Iterator();
        while(i.has_next())
            System.out.print(i.get_next() + ", ");
    }
}
#

sorry just highlighting so its easier to read