• Post Reply Bookmark Topic Watch Topic
  • New Topic

iterating over an array of elements  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner in Java and I am using EMF(Eclipse Modeling Framework) to create a plugin. I have the following program:


public class EnterLeaveHandler implements IOtfHandler {

Scanner reader = new Scanner(System.in);
public EnterLeaveHandler(String id, boolean print) {
this.whatever = id;
this.print = print;
}

private String whatever;
private boolean print;

@Override
public void handle(long time, int state, int cpuid, int token) {

Trace t = Basic_traceFactory.eINSTANCE.createTrace();
Events e = Basic_traceFactory.eINSTANCE.createEvents();

t.setIdentifier(token);
e.setTimestamp(time);
e.setCpuid(cpuid);
e.setState(state);
e.setData(this.whatever);

if (this.print) {

System.out.println("Enter timestamp information for filtering");
long b = reader.nextLong();
System.out.println("This is the start time " +b);
for(long x:e.getTimestamp)
{

}

}


Im trying to initialize an array with the elements from timestamp, which is basically 'time'. But I get an error stating that 'Can only iterate over an array or an instance of java.lang.Iterable'

Im not sure about what is wrong here..kindly help.
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preethi ,
Welcome to CodeRanch!

To use the for loop, you need a list. You only have one object. getTimestamp() would return a single long, not a list of longs. Which means you could do this without a loop.
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Jeanne! But the handle method reads from a file which has an array of data for timestamp. So should I create an array for the 'time' values passed through the method and then use it in for loop?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying you call handle() multiple times? (in which case, no for loop inside) or that you have some way of getting multiple times (in which case an array could make sense)?
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a way by which the handle(); is called multiple times as follows from main(). In that case, how should I declare and initialize an array for time?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. It would be easier to use an ArrayList than an array since you don't need to know the # elements in advance for an ArrayList.

The steps are to:
1) Declare an ArrayList instance variable
2) Instantiate the ArrayList to an empty list
3) Instead of a for loop in the handle method, add a single timestamp to the array.
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help Jeanne! It is working and I have understood the concept of arraylist now
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! I'm glad to hear you got it working. And thanks for being patient as we went back and forth!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!