Welcome to Dream.In.Code
Become a Java Expert!

Join 150,138 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,280 people online right now. Registration is fast and FREE... Join Now!




print Array

 
Reply to this topicStart new topic

print Array

j0e
2 Aug, 2008 - 09:41 AM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 32


My Contributions
Hi again i have the following array and i want to print its context
CODE

public Trans trans =new Trans();{
       trans.addTransaction(new Transactions("deposit",100));
       trans.addTransaction(new Transactions("deposit",100));
       trans.addTransaction(new Transactions("deposit",100));
       trans.addTransaction(new Transactions("deposit",100));
     }


These are the classes
CODE

public class Transactions {
    
public Transactions(String transactionType,int withdrawAmount)
{  
     transType = transactionType;
     transAmount = withdrawAmount;
  
  }


   private String transType;
   private int transAmount;
}


and

CODE


public class Trans {


  public Trans()
  {
    
     transactions = new ArrayList<Transactions>();
  }
    
   public ArrayList<Transactions> lastTransaction()
    {
        return transactions;
    }
  
   public void addTransaction(Transactions e)
    {
        transactions.add(e);
    }
  
    private static ArrayList<Transactions> transactions;
}


i tried loops or iteration but i cant do it.
Any idea?
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Print Array
2 Aug, 2008 - 09:45 AM
Post #2

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
QUOTE(j0e @ 2 Aug, 2008 - 10:41 AM) *

Hi again i have the following array and i want to print its context
CODE

public Trans trans =new Trans();{
       trans.addTransaction(new Transactions("deposit",100));
       trans.addTransaction(new Transactions("deposit",100));
       trans.addTransaction(new Transactions("deposit",100));
       trans.addTransaction(new Transactions("deposit",100));
     }


These are the classes
CODE

public class Transactions {
    
public Transactions(String transactionType,int withdrawAmount)
{  
     transType = transactionType;
     transAmount = withdrawAmount;
  
  }


   private String transType;
   private int transAmount;
}


and

CODE


public class Trans {


  public Trans()
  {
    
     transactions = new ArrayList<Transactions>();
  }
    
   public ArrayList<Transactions> lastTransaction()
    {
        return transactions;
    }
  
   public void addTransaction(Transactions e)
    {
        transactions.add(e);
    }
  
    private static ArrayList<Transactions> transactions;
}


i tried loops or iteration but i cant do it.
Any idea?


Well, do you want to 'print' the transactions, as in, on the console? Or do you want them in a GUI...or?

Well, either of those are going to need a method in the Trans class.

java
public void printTransactions()
{
for (int x = 0; x < transactions.size(); x++)
{
System.out.println(transactions.get(x).getType());
System.out.println(transactions.get(x).getAmount());
}
}


And once again...I see a semicolon in your first piece of code that shouldn't be there...

I also don't see that you can access the "Transactions" variables, there are no methods to access the private data. You can make your printing a little more friendly. I added a getType() method in that snippet.

java
// add this to your Transactions class

public String getType()
{
return this.transType;
}

public int getAmount()
{
return this.transAmount;
}


This post has been edited by Locke37: 2 Aug, 2008 - 09:53 AM
User is offlineProfile CardPM
+Quote Post

j0e
RE: Print Array
2 Aug, 2008 - 09:49 AM
Post #3

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 32


My Contributions
QUOTE(Locke37 @ 2 Aug, 2008 - 10:45 AM) *

Well, do you want to 'print' the transactions, as in, on the console? Or do you want them in a GUI...or?


well actually i want to print them in a gui in a textarea called jTextArea1 and in console too and later in a file.But even in console would be a great help to understand.
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Print Array
2 Aug, 2008 - 09:57 AM
Post #4

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
Well, then in my first snippet, printTransactions(), I'll edit it a bit here.

java
public String printTransactions()
{
String ret = "";

for (int x = 0; x < transactions.size(); x++)
{
ret += transactions.get(x).getType() + "\n";
ret += transactions.get(x).getAmount() + "\n\n";
}

return ret;
}


Then set the TextArea text to that method and it should work.

smile.gif

I think I edited my earlier post after you posted, I made 2 more methods, check those if the ones in this one aren't familiar. smile.gif

This post has been edited by Locke37: 2 Aug, 2008 - 10:06 AM
User is offlineProfile CardPM
+Quote Post

j0e
RE: Print Array
2 Aug, 2008 - 10:17 AM
Post #5

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 32


My Contributions
QUOTE(Locke37 @ 2 Aug, 2008 - 10:57 AM) *

Well, then in my first snippet, printTransactions(), I'll edit it a bit here.

java
public String printTransactions()
{
String ret = "";

for (int x = 0; x < transactions.size(); x++)
{
ret += transactions.get(x).getType() + "\n";
ret += transactions.get(x).getAmount() + "\n\n";
}

return ret;
}


Then set the TextArea text to that method and it should work.

smile.gif

I think I edited my earlier post after you posted, I made 2 more methods, check those if the ones in this one aren't familiar. smile.gif


Ok i got it.Thanks a lot again!

But the headache keeps going.Its the final thing in my Assignment.Actually i need these function because i want to keep in this array all the transaction of the Atm i design and then with a button print the 10 last.Your help unstuck me now i have to apply all the changes in my code.

As far as the semicolons in the arrays without the semicolons gives me an error.I have put them in the implementation file
CODE

package mypackage;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import mypackage.BankAccount;

/**
*
* @author  j0e
*/
public class Atm extends javax.swing.JDialog {
  
   //Create the initial array of the accounts
public Bank bank = new  Bank();{
        bank.addAccount(new BankAccount(100, 1000, "Karapanos" ,"Andreas Fotiou","Checking"));
        bank.addAccount(new BankAccount(200, 2000, "City" , "Maria Glastra", "Saving"));
        bank.addAccount(new BankAccount(300, 3000, "Karapanos", "Malvina Karali", "Checking"));
}      



i guess i might have them in wrong place but its work well i could post all the code of the project but is more than 2l lines.

Thanks Again.i might need you again on this.


User is offlineProfile CardPM
+Quote Post

Locke37
RE: Print Array
2 Aug, 2008 - 10:30 AM
Post #6

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
Oh wait, ignore everything I said about the semicolons...I was just looking at it wrong.

unsure.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:08AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month