-3

This is my code so far:

public class BankAccountService {

    public static void main(String[] args) {


        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println("!                 Welcome To San Andreas Bank               !");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

        System.out.println("Enter one of the following numbers to select: ");
        System.out.println("1.Select bank account ");
        System.out.println("2.Check account balance ");
        System.out.println("3.Deposit ");
        System.out.println("4.Withdraw money ");
        System.out.println("5.Transfer money ");
        System.out.println("6.Exit ");

        String bankaccountsstring = "BE68 5390 0754 7034, BE68 5320 0744 7534, BE68 5570 0723 7547";
        String[] Bankaccounts = bankaccountsstring.split(",");

        Scanner inputin1 = new Scanner(System.in);
        int inputin = inputin1.nextInt();

        if (inputin <= 0 || inputin > 6 ) {
            System.out.println("Please try again, choose a number between 1 and 6");
        }
        else if (inputin == 1 ) {
            for (int y = 0; y < Bankaccounts.length; y++ )


            System.out.println("Your bank accounts: ");
            System.out.println(Arrays.toString(Bankaccounts));


        }


    }

This is what I get in console

image

But I want it to be like this:

Your Bank accounts:
1. bank account ID 1
2. bank account ID 2
3. bank account ID 3

another example:

1. BE68 5390 0754 7034
2. BE68 5320 0744 7534
3. BE68 5570 0723 7547

How can I achieve this? I googled around but have not found a good solution for this.

Thank you in advance

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 2
    Have another look at your `y` loop, and explain to yourself why that loop is there, and what it does. Then fix the code, using appropriate indentations and `{ }` braces. – Andreas Apr 05 '21 at 15:13
  • What did you put into the search bar when you "googled around"? This an honest question - knowing how to search to find answers is half the battle. Also, it would be helpful if you really want answers from this site, as it would prove that you actually tried something to fix the problem on your own. – tucuxi Apr 05 '21 at 15:19
  • Are you just looking for a way to print this information or you need an index in the map to execute further logic? – Richard Marto Apr 05 '21 at 15:26
  • I don't think Java has a zipWithIndex on the List collection, but you can use this post for reference if you need to add the index on a map. https://stackoverflow.com/questions/18552005/is-there-a-concise-way-to-iterate-over-a-stream-with-indices-in-java-8 – Richard Marto Apr 05 '21 at 15:27

3 Answers3

1

replace

System.out.println("Your bank accounts: ");
System.out.println(Arrays.toString(Bankaccounts));

with

{
  System.out.println((y+1)+"."+ bank account ID: "+Bankaccounts[y]);
}
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24
0

You are printing the whole array in form of a string.to get the output in the format you have asked simply print using array index in your loop like

System.out.println(y + ". "+Bankaccounts[y]);
Sharan L
  • 1
  • 2
0

Thank you for all the answers I figured it out, I know I am a big noob, and looking for problem solutions is part of the struggle but I am actually a noob in that too. It eezz what it eezz

    } else if (inputin == 1) {
        for (int y = 0; y < Bankaccounts.length; y++ ) {
            System.out.printf("%d %s%n", y + 1, Bankaccounts[y]);
            //System.out.println(y + ". "+Bankaccounts[y]);

Also, prinln does not work but printf does? wtf