Home > Back-end >  how do I print the list for this
how do I print the list for this

Time:11-22

System.out.println("Enter the Account no :");//in my main class
int accNo=sc.nextInt();
switch(n)
{
    case 1 ->{
            
                    
        Account ac=nbank.searchAccount(accNo);//account is my other class
        if(ac!=null)
        {
                    
        System.out.println("Enter the Amount to Deposite :");
        double amt=sc.nextDouble();
                        
        List<Trans> tr=nbank.deposite(accNo,amt);//Trans is also class and I created the list for this class and Trans contains the following below methods
                                            
System.out.println("AccNo :" tr.getAccNo() "  TransID :" tr.getTransId() "  type: " tr.getType() " Amount :" tr.getWithAmt() "  Status :" tr.getStatus() " Date :" tr.getDate());

                    

`from the withdraw method i'm getting a list but somehow I can't able to print the list contents using LinkedList

and for every method it shows method not found, class I made are as Trans(getter and setter),bank as logic

how do I be able to print the list for the above programs using LinkedList`

List<Trans> withdraw(int accNo,double amt)//in my bank class where i'm implementing the logic and trying to store to other classes. 

here my intension is to view the withdraw the transaction done by the customer in my main()

{
    List<Trans> tr=new LinkedList<Trans>();
    Account ac=searchAccount(accNo);
    if(ac instanceof FDAccount)
        tr=((FDAccount)ac).withdraw(amt);
    else if(ac instanceof Account)
        tr=ac.withdraw(amt);
    return tr;
}

this is my withdraw method that is called by List<Trans> tr=nbank.withdraw(accNo,amt);
`UserUI.java:189: error: cannot find symbol
                                                        System.out.println("AccNo :" tr.getAccNo() "  TransID :" tr.getTransId() "  type: " tr.getType() " Amount :" tr.getDepAmt() "  Status :" tr.getStatus() " Date :" tr.getDate());
                                                                                       ^
  symbol:   method getAccNo()
  location: variable tr of type List<Trans>
UserUI.java:189: error: cannot find symbol`

error like this etc

CodePudding user response:

As @xlm said, tr is a List, you only can access getter by index. You can use for loop or foreach, you can't use tr.getAcNo().

CodePudding user response:

tr is a List as returned by withdraw method. As such it does not have method getAccNo.

You probably mean reference to an instance of Trans in the list e.g. for the first Trans in the List:

tr.get(0).getAccNo()

Or for every Trans:

for (Trans tran : tr) {
  System.out.println("AccNo :" tran.getAccNo() "  TransID :" tran.getTransId() "  type: " tran.getType() " Amount :" tran.getWithAmt() "  Status :" tran.getStatus() " Date :" tran.getDate());
}

Hard to be more specific or sure without additional clarifications/code in your question.

CodePudding user response:

This is a list, in java you must loop or iterate through the list, for an example:

//indexed loop
for (int i = 0; i < tr.size(); i  ) {
    System.out.println("AccNo :" tr.get(i).getAccNo() "  TransID :" tr.get(i).getTransId() "  type: " tr.get(i).getType() " Amount :" tr.get(i).getWithAmt() "  Status :" tr.get(i).getStatus() " Date :" tr.get(i).getDate());
}

or

//foreach loop
for (Trans trans: tr) {
    System.out.println("AccNo :" trans.getAccNo() "  TransID :" trans.getTransId() "  type: " trans.getType() " Amount :" trans.getWithAmt() "  Status :" trans.getStatus() " Date :" trans.getDate());
}
  •  Tags:  
  • java
  • Related