Why can't I print out the whole ArrayList? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why can't I print out the whole ArrayList? [closed]



I have a main class where I create objects of the class Account. I can add several accounts (objects) to my arrayList, and with my for loop I can print out all of the objects so I know that that part works (I will delete it later since the user shouldn't see all other accounts, its only so I can see where my code works and not).



In my third class, Transaction, I am supposed to iterate through that list in order to find one specific account, based on accountNr and make transactions from/to that specific account. This is where I need help, I thought I could make a for loop and printing out all the objects just so I can see that the code actually have access to all objects.



The problem is that it only prints out the first object, as if the other objects weren't there? I don't have to get the printing part right, but I need to know how I can see IF I have "access" to the whole list, so I know how to move on to the part where I can choose a specific object to work with. Again, the printing of accounts is just for myself so I can see if the code works.



This is the part in the main class (called AccountTest) where I create an object of Account and add the variables since I have a constructor. And then I add that object to the list, and print it out so I can see that they are there, which they all are.


Account account = new Account(accountNr,
balance,credit);
accountList.add(account);
System.out.println("Created account for:"+
accountNr);

for (int i=0; i<accountList.size(); i++)
System.out.println(accountList.get(i));



The part of the transaction class where I want to print out the accountList and later on use some code in order to find the specific object I am looking for...


import static accountpackage.AccountTest.
accountList;

public class Transaction
AccountTest main = new AccountTest();

public void showAccountTransaction()
for (int i=0; i<accountList.size(); i++)
System.out.println(accountList.get(i) );






I have all the set & get methods for all variables.



I saw that I got a "-1" vote on this question, so I may add that I have indeed searched for this a lot, and tried different ways. Both from stackoverflow, Youtube, watched old lessons from teachers. It would be difficult to read my text if I wrote all the research effort...And since I find this difficult, I find it hard to be extremely clear about it.



Edit... AccountTest code


public class AccountTest {
static List <Account> accountList = new ArrayList <>();

public static void main(String args)
Scanner input = new Scanner (System.in);
Random randGen = new Random ();
Transaction t = new Transaction();

menu();
boolean choice = true;
String myChoice;
do
myChoice = input.next();
input.nextLine();
switch (myChoice.charAt(0))
case '1':
menu();
case '2':
createAccount();
meny();
break;
case '3' :
t.showAccountTransaction();
meny();
break;
case '4' :
val = false;
System.out.println("Program ended.");
System.exit(0);

while (choice);
//end of main

public static void menu ()
System.out.println ("nChoose one of the alternatives! n"
+ "1 Menun"
+ "2 Create accountn"
+ "3 Show and make transactionn"
+ "4 End" );


public static void createAccount()
Scanner inread = new Scanner (System.in);
System.out.println("AccountNr: ");
int accountNr = inread.nextInt();
System.out.println("Balance: ");
int balance = inread.nextInt();
System.out.println("Credit: ");
int credit = inread.nextInt();

Account account = new Account (accountNr, balance, credit);
accountList.add(account);
System.out.println("Created account for: " + accountNr);

System.out.println("The accountList size in main: "
+kontoLista.size());

for (int i=0; i<accountList.size(); i++)
System.out.println(accountList.get(i));



//and get & set methods for the list



This question appears to be off-topic. The users who voted to close gave this specific reason:





if you only add one object to the list, you can only print one
– Lino
Aug 10 at 17:30





To test what @Lino has suggested, print the size of the list prior to your loop: System.out.println(accountList.size())
– John Stark
Aug 10 at 17:35



System.out.println(accountList.size())





@JohnStark I think OP refers to down vote this question received. Not entirely sure why this question gets down voted.
– Rohit
Aug 10 at 17:42






I am able to run your code without any issues, the method showAccountTransaction prints out the number of accounts depending on the output
– Nicholas K
Aug 10 at 18:37


showAccountTransaction





Yes i created 2 accounts based via your switch case '2'. I think you have missed observing the output. As you can clearly see the hashcode of the objects printed are different. The accountList size in main: 3 org.test.com.Account@2503dbd3 org.test.com.Account@4b67cf4d org.test.com.Account@7ea987ac
– Nicholas K
Aug 10 at 18:41





1 Answer
1



So I had to make some edits to your code to get it to run properly, but nothing that substantially changed the flow/control of the program. Once it was able to compile properly, the code ran fine and returned all elements of the list in both the main() and Transaction classes (I had to create an Account object, which I just made as containing the fields that you were setting).


main()


Transaction



Here's the code as I ran it, with a few comments inline about a couple of best practice things:


package accountpackage;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class AccountTest
// This List should be parameterized with the Account class instead of a raw-type List<>.
static List<Account> accountList = new ArrayList<>();

public static void main(String args)
Scanner input = new Scanner(System.in);
Random randGen = new Random();
Transaction t = new Transaction();

menu();
boolean choice = true;
String myChoice;
do
myChoice = input.next();
input.nextLine();
switch (myChoice.charAt(0))
case '1':
menu();
case '2':
createAccount(input);
menu();
break;
case '3':
t.showAccountTransaction();
menu();
break;
case '4':
choice = false;
System.out.println("Program ended.");
System.exit(0);

while (choice);

// Close the scanner to avoid a memory leak!
input.close();
// end of main

public static void menu()
System.out.println("nChoose one of the alternatives! n" + "1 Menun" + "2 Create accountn"
+ "3 Show and make transactionn" + "4 End");


// You can pass your existing scanner in to this method instead of creating a new scanner.
public static void createAccount(Scanner input)
System.out.println("AccountNr: ");
int accountNr = input.nextInt();
System.out.println("Balance: ");
int balance = input.nextInt();
System.out.println("Credit: ");
int credit = input.nextInt();

Account account = new Account(accountNr, balance, credit);
accountList.add(account);
System.out.println("Created account for: " + accountNr);

System.out.println("The accountList size in main: " + accountList.size());

for (int i = 0; i < accountList.size(); i++)
System.out.println("Main Output" + accountList.get(i));


// and get & set methods for the list



Below is the output that I got in the console, so you can see the values I entered, and what was printed back to me:



Let me know if you're still having trouble and I'll take a second look, but for now anyway, the code appears to be functioning properly!





Thank you, I get the same output as you got, so it works now! You wouldn't happen to know what my next step should be in order to replace the existing balance, with a new balance in one specific object based on accountNr (perhaps after user input who prints its accountNr once more)?
– Anna
Aug 10 at 19:45





@Anna Since you're storing the Account objects in a list, you would have to iterate over all Accounts in the list, checking whether each one contained the accountNr that the user entered. Once you find the matching account object, you would update its balance accordingly. That being said, it's a costly operation to look through potentially the entire list to find the account; you may be better off converting the List into a Map so that you can have constant-time lookup based on accountNr: static Map<Integer, Account> accountMap = new HashMap<>();
– John Stark
Aug 10 at 20:07


static Map<Integer, Account> accountMap = new HashMap<>();





Great, I am now using HashMap and it works perfectly. I can now print out the specific object based on accountNr. Next step will be to be able to extract balance or something, so I can perform math on it, and then replace it with the new balance (which I have found code for). If you want you could give me some pointer there as well, otherwise, thank you so much for the help!
– Anna
Aug 10 at 21:34





Awesome, glad to hear that the HashMap is working out! Updating the balance once you have the Account Object should just be altering the internal state of the balance using the setter you have. The steps in pseudocode would be: 1) Get Account from map 2) Get balance from Account 3) Perform math operations 4) Set result as balance within the Account. In code it would be similar to this: int oldBalance = accountMap.get(accountNr).getBalance(); int newBalance = 0; // perform math newBalance = // result of math accountMap.get(accountNr).setBalance(newBalance);
– John Stark
Aug 10 at 22:02



int oldBalance = accountMap.get(accountNr).getBalance(); int newBalance = 0; // perform math newBalance = // result of math accountMap.get(accountNr).setBalance(newBalance);





Thank you, that worked out perfectly, your help was much appreciated!
– Anna
Aug 10 at 22:17

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard