Java: Displaying Array elements from a LinkedList

Soldato
Joined
5 Dec 2003
Posts
2,716
Location
Glasgow
Hi, just trying to learn Java and do a little project in it and i've come up against a brick wall, wondering if someone here can help me out.

I've managed to create a method to insert an array into a LinkedList.

Now i'm trying to make a second method to write it out to the screen again but i'm having trouble accessing the array elements.

Here is the code:

Code:
public class Transaction
{

    List transactions = new LinkedList();

    public boolean setTransaction(String transactionType, Double transactionAmount)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        NumberFormat numberFormatter;
        numberFormatter = NumberFormat.getCurrencyInstance();
        
        Date now = new Date();
        String nowFormatted = formatter.format(now);
        String transactionAmountString = numberFormatter.format(transactionAmount);

	    String[] transactionArray = new String[3];
	    transactionArray[0] = nowFormatted;
	    transactionArray[1] = transactionType;
	    transactionArray[2] = transactionAmountString;

        transactions.add(0, transactionArray);
        return true;
    }

    public void getTransactions()
    {
        for(int i=0; i<transactions.size(); i++){
           System.out.println(transactions.get(i));
        }
    }
}

This runs okay but the output is rubbish and I expect that but when I try to change this line:
Code:
System.out.println(transactions.get(i));
to something like
Code:
System.out.println(transactions.get(i).nowFormatted);
it gives the error cannot resolve Symbol.
 
Your better off doing it something like this - Couldn't be bothered to add in proper formatting sure you can do that. :p At least make your linked list type safe. Not to mention there is no method .nowFormatted defined on your object... so thats why your getting the symbol error.

Transaction.java
Code:
public class Transaction
{

    private String transactionType;
    private double transactionAmouunt;

    public Transaction(String type, double amount)
    {
        this.transactionType = type;
        this.transactionAmouunt = amount;
    }

    public String toString()
    {
        return "Type: " + this.transactionType + " Amount: " + this.transactionAmouunt + " ";
    }
}

TransactionTest.java
Code:
import java.util.LinkedList;

public class TransactionTest
{
    private static LinkedList<Transaction> transactions = new LinkedList<Transaction>();

    public static void main(String[] args)
    {
        for (Transaction t : transactions)
        {
            System.out.println(t);
        }
    }
}
 
Last edited:
Back
Top Bottom