2015-01-15 62 views
2

我正在恢復的錯誤是「void type不允許在這裏」,我將如何解決這個錯誤以打印出我的數組列表。嘗試打印陣列列表時出錯

import java.util.ArrayList; 



public class Library 
{ 


    private ArrayList<Member>listOfMembers; 
    public Library() 
    { 
     listOfMembers = new ArrayList<Member>(); 
    } 
    public void storeMember(Member Member) 
    { 
     listOfMembers.add(Member); 
    } 
    public int numberOfMembers() 
    { 
     return listOfMembers.size(); 
    } 

    public void listMembers() 
    { 
    for (int item=0; item<listOfMembers.size(); item++) { 
    Member m = listOfMembers.get (item); 
    System.out.println(m.GetWholeName()); 
     } 
    } 


} 

,這裏是我的會員類,以防萬一你需要它

class Member 
{ 
    // The fields. 
    private String firstname; 
    private String lastname; 
    private Integer number; 
    private Integer id; 

    /** 

    hehe 
    */ 
    public Member(String firstName, String lastName, Integer telNumber, Integer memberId) 
    { 
     firstname = firstName; 
     lastname = lastName; 
     number = telNumber; 
     id = memberId; 

    } 


    // Add the methods here ... 
    public void GetWholeName(){ 

    System.out.println(firstname + (" ") + lastname); 

} 
} 

我試圖通過使用一個數組列表,打印我的成員類的姓和名我的圖書館類

回答

3

您正在嘗試打印出m.GetWholeName()的結果,該結果返回void。將GetWholeName更改爲返回String,或者僅調用GetWholeName,因爲它已將名稱打印到標準輸出,儘管該行爲與名稱暗示的行爲不完全匹配。

public String GetWholeName() { 

    return firstname + " " + lastname; 

} 
+0

感謝您這解決了它 – Hmed 2015-01-15 19:00:23

1

在會員級的變化是:

System.out.println(firstname + (" ") + lastname); 

這樣:

return firstname + " " + lastname;