The Artima Developer Community
Sponsored Link

Java Answers Forum
help with simple method code.

1 reply on 1 page. Most recent reply: Apr 10, 2003 2:41 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

help with simple method code. Posted: Apr 9, 2003 9:42 PM
Reply to this message Reply
Advertisement
Why am I getting an error that says:
C:\Program Files\Xinox Software\JCreator LE\MyProjects\Assignment#1cs260\Bag.java:65: cannot resolve symbol
symbol : method next ()
location: interface Iterator
add( iterator.next());


import java.util.*;

/*********************************************************
Class Name: Bag
Functions: All of the functions in the Collection Interface
Purpose: Implement the Collection Interface.
*********************************************************/
class Bag implements Collection
{

//Class variables.
int size;
int index;
Object[] wallet;

/*****************************************************
Function name: Bag();
Parameters: Collection c
Declared variables: none
Return value: none
Purpose: Create a new array + Bag object.
*****************************************************/
public Bag()
{
size=10;
index=0;
wallet=new Object[size];
};

/*****************************************************
Function name: add();
Parameters: Object
Declared variables: none
Return value: boolean
Purpose: Add an Object to the Collection.
*****************************************************/
public boolean add(Object o)
{

if(o==null)
return false;

while(wallet[index]==null)
{
wallet[index]=o;
}
++index;

return true;

};

public boolean addAll(Collection c)
{
Iterator iterator = c.iterator();
while( iterator.hasNext())
{
add( iterator.next());
}
return true;
};

public void clear()
{

};

/*public boolean contains(Object o){};
public boolean containsAll(Collection c){};
public boolean equals(Object o){};*/
public Iterator iterator()
{

};
/*public boolean remove(Object o){};
public boolean removeAll(Collection c){};
public boolean retainAll(Collection c){};
public int size(){};
public Object[] toArray(){};*/

//Inner class that is implementing the Iterator interface.
class Iterd implements Iterator
{

public boolean hasNext()
{
index=0;//resets the index so it starts at the beginning.
if(wallet[++index]!=null)
return true;
};

public Object next()
{
index=0;
++index;
return wallet[index];
};
public void remove(){};
};


};


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: help with simple method code. Posted: Apr 10, 2003 2:41 AM
Reply to this message Reply
> Why am I getting an error that says:
> C:\Program Files\Xinox Software\JCreator
> LE\MyProjects\Assignment#1cs260\Bag.java:65: cannot
> resolve symbol
> symbol : method next ()
> location: interface Iterator
> add( iterator.next());
>

I didn't get that error but I got plenty of others. To start it is not necessary to close methods with a semi-colon. For example, this is not necessary

public void myMethod()
{
  // do something here 
}; // <-- see semi-colon


Try this
public void myMethod()
{
  // do something here
} // note no semi-colon


Secondly, a lot of the Collection methods had not been implemented, for example,
public int size()
 
public boolean isEmpty()
 
public boolean contains( Object o )
 
public Object[] toArray()
 
public Object[] toArray( Object o[] )
 
public boolean remove( Object o )
 
public boolean containsAll( Collection c )
 
public boolean removeAll( Collection c )
 
public boolean retainAll( Collection c )


Finally, your hasNext() method in class Iterd should return a value in the event of not finding another element.

public boolean hasNext()
{
  index=0;//resets the index so it starts at the beginning.
  if(wallet[++index]!=null)
    return true;
  else
    return false;
}


I suspect the error you got is misleading. Make the changes I suggested and let us know if you're still having problems.

Adam

Flat View: This topic has 1 reply on 1 page
Topic: null pointer exception in JBuilder Previous Topic   Next Topic Topic: Prime Number Sources/Example Request

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use