0

This seems really simple but all I'm trying to do is return items from an ArrayList as Strings I used the following code to return them, but it only returns the first item in the list, I would like it to return each item individually. Not sure what I'm doing I had it working but deleted it accidentally.

public String getEachEmployeeInstance() {


        for (Employee e : employees)
        {  

             return e.getFirstName() +"\t" + e.getLastName() +"\t"+ e.getEmployeeIDString()+"\t" + e.getPunchIn() +"\t"+ e.getPunchOut() +"\t"+ e.getDailyHours() +"\t"+ e.getWeeklyHours();  
        }         
        return null;            
    }
PM 77-1
  • 12,933
  • 21
  • 68
  • 111

3 Answers3

2

It looks like you are looking for a Java equivalent to Python's yield keyword. Java does not have this functionality built-in.

When you say return, you are telling Java to exit the function and return the current element. The rest of the loop does not run.

If you really want a generator functionality in Java, you might want to consider looking at the libraries mentioned in this answer.

For this specific use case, it should be sufficient to just maintain an Iterator or an index into the collection and reference this object each time the function is called.

Iterator<Employee> myEmployeeIterator = null;

public String getEachEmployeeInstance() {
    if (myEmployeeIterator == null) 
        myEmployeeIterator = employees.iterator();

    if (myEmployeeIterator.hasNext()) {
        Employee e = myEmployeeIterator.next();
        return e.getFirstName() +"\t" + e.getLastName() +"\t"+ e.getEmployeeIDString()+"\t" + e.getPunchIn() +"\t"+ e.getPunchOut() +"\t"+ e.getDailyHours() +"\t"+ e.getWeeklyHours();  
    }

    return null;            
}

Another possibility is that the OP actually just wants the concatenated output from the loop, delimited by newlines, to display in his GUI component. This can be achieved most easily with StringBuilder.

public String getEachEmployeeInstance() {

    StringBuilder sb = new StringBuilder();
    for (Employee e : employees)
    {  
        sb.append(e.getFirstName() +"\t" + e.getLastName() +"\t"+ e.getEmployeeIDString()+"\t" + e.getPunchIn() +"\t"+ e.getPunchOut() +"\t"+ e.getDailyHours() +"\t"+ e.getWeeklyHours());  
        sb.append("\n");
    }         
    return sb.toString();            
}
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • how do I get it to continue rather than exit? – user3682269 Jul 12 '14 at 01:17
  • 1
    @user3682269, Wrap it up in a class, and store an index in the class. Then increment the index each time you return, so that the next call will get the next index. – merlin2011 Jul 12 '14 at 01:25
  • @merlin2011 is entirely right. Take a look at [the implementation of ArrayList's Iterator](http://www.docjar.com/html/api/java/util/ArrayList.java.html) (the inner class is named `Itr`). It does just that. – David Ehrmann Jul 12 '14 at 01:33
  • I think i have done that, with the ID, but I need some way to return the values to jTextPane, I was using System.out.println to the console but need it to now go to jTextPane. – user3682269 Jul 12 '14 at 01:36
  • @user3682269 please accept the answer if it resolved your question. Also it will be helpful to people in the future if you indicate which half of the answer helped you. – merlin2011 Jul 12 '14 at 16:12
0

It only returns the first element as string since you put return as the first statement in the for loop.

If you want to return a list of string. You need to do the following:

public List<String> getEachEmployeeInstance() {
    List<String> employeeList = new ArrayList<>;

    for (Employee e : employees)
    {  
         employeeList.add( e.getFirstName() +"\t" + e.getLastName() +"\t"+ e.getEmployeeIDString()+"\t" + e.getPunchIn() +"\t"+ e.getPunchOut() +"\t"+ e.getDailyHours() +"\t"+ e.getWeeklyHours());  
    }         
    return employeeList;            
}

On the other hand, if you want to do a yield return you can use YieldUtils from this project:

https://code.google.com/p/java-yield/

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • Do you think your approach make any sense? – PM 77-1 Jul 12 '14 at 01:11
  • @PM77-1 sorry my friend, can you give me more details? – Federico Piazza Jul 12 '14 at 01:13
  • I think he means that you could replace the entire function with `return employees`, and change the interface to return a Collection. – merlin2011 Jul 12 '14 at 01:14
  • Yes. Why would someone want to take the list of objects and return the list of strings. Can you think of a reasonable use case? – PM 77-1 Jul 12 '14 at 01:14
  • @merlin2011 I don't understand that from the question. OP said `return items from an ArrayList as Strings ` and ` I would like it to return each item individually`. I think OP is new in java and as he said he wants to do a really simple thing. – Federico Piazza Jul 12 '14 at 01:17
  • @PM77-1 OP asked for that, he has a list of objects, wants to do some process on the object and return a string for each object. What's the problem? It is a normal and simple thing. What am I missing here? – Federico Piazza Jul 12 '14 at 01:19
  • You are changing his interface. He wants a function that returns *one string at a time*, and a different string each time it is called. – merlin2011 Jul 12 '14 at 01:23
  • Sorry guys, I'm trying to return them to jTextPaqe and it only seems to do one and that's it. when I use System.out.println to the console it prints all of them. I don't know how to get them to print out to jTextPane – user3682269 Jul 12 '14 at 01:40
  • @user3682269 Can you confirm if the answer I posted is wrong? I don't want to post a wrong answer. – Federico Piazza Jul 12 '14 at 01:57
  • No it's not wrong, just not what I'm looking for, sorry! I think I need to do some more research. – user3682269 Jul 12 '14 at 02:18
0

It seems that you would like to pass the returned string to another method which accepts a single string (as opposed to an array or list of strings). If so, you might not need the getEachEmployeeInstance() method:

for(Employee employee : employees) {
    anotherMethod(employee.getFirstname() /* ... */);
}
Jae Heon Lee
  • 1,101
  • 6
  • 10
  • OK, that makes sense. I'll try to create another method. Basically I think I need another method for jTextPane to do this. Thanks – user3682269 Jul 12 '14 at 04:04