10 Replies - 380 Views - Last Post: 29 November 2012 - 12:23 PM Rate Topic: -----

#1 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

For loop

Posted 29 November 2012 - 03:13 AM

for (Bank_AC a : accountList)
		 { 
	        System.out.println(toString(a));
	     } 

I understand this is what is called an enhanced for loop: and it basiccaly iterates through the whole array.
What change would i make to the code just to iterate the through the first 3 elements?

This post has been edited by jon.kiparsky: 29 November 2012 - 12:19 PM

Is This A Good Question/Topic? 0
  • +

Replies To: For loop

#2 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: For loop

Posted 29 November 2012 - 03:29 AM

Hi mattylynch1
for-in or for-each determine the number of iterations from the size of the collection.
So in order to control the number of iterations you will have to go for the traditional for loop.

Regards
Was This Post Helpful? 0
  • +
  • -

#3 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: For loop

Posted 29 November 2012 - 03:32 AM

how exactly would i phrase that for the object of type Bank_Ac. IM new to this obviously and ive only used simple for loops for int arrays.
Was This Post Helpful? 0
  • +
  • -

#4 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: For loop

Posted 29 November 2012 - 03:58 AM

Basically instead of saying "for every account in account list" i need to say "for the accounts in element 0 of array to element3 of array"
For....
Was This Post Helpful? 0
  • +
  • -

#5 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: For loop

Posted 29 November 2012 - 04:10 AM

for(int i=0;i<3;i++)
System.out.println(accountList[i].toString());

Quote

I want the program to try and print details but if array is not full then i want it to print the first 3 accounts instead.....


You will need a flag.As soon you input any account value set the flag to one.In this way you will be able to figure out whether an account holds a valid entry or not.
Now you can use a for loop to check if all the accounts have flag value 1(using array.length as the loop counter) and check if all accounts are valid.
If you find an an empty account break out of the loop and check if the value of the loop variable is equal to array.length.
If no then print the first three accounts again using the flag(if flag=1 print the account details and increment the loop variable else reiterate).The counter would be 3.

Hope I am clear enough.

This post has been edited by rnty: 29 November 2012 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 mattylynch1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 27-November 12

Re: For loop

Posted 29 November 2012 - 04:34 AM

Below see relevant code snippets. I cant do this:
System.out.println(toString(i));
because it needs to accept a bank account object rather than an int.
I want the program to try and print details but if array is not full then i want it to print the first 3 accounts instead.....
private  String toString(Bank_AC thisacc)
  		{
	
      return "Account Name: " + thisacc.getName()+"\t "
                     +"A/c no:"+thisacc.getaccNO()+"\t "
                     +"Balance in A/c: "+thisacc.getbal();
      
  		}

public void printDetToConsole (Bank_AC[] accountList)	
  {
	  try
	  {
	  System.out.println("Bank Accounts currently in Account  Array: " );
		 for (Bank_AC a : accountList)
		 { 
	        System.out.println(toString(a));
	     } 
	  }
	  catch(NullPointerException NPE)
	  {
	  System.out.println("Bank Accounts currently in Account Array: " );
	  
	  for(int i=0;i<3;i++)
	
		 { 
	        System.out.println(toString(i));
	     }
	  }
  }[code]
[/code]
Was This Post Helpful? 0
  • +
  • -

#7 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: For loop

Posted 29 November 2012 - 06:06 AM

for(int i=0;i<3;i++)
{ 
  System.out.println(toString(i));
}



You should know what that prints by just looking... 0,1,2.

I don't get why you are only going through 3 accounts if there is a null pointer but you should loop through the accounts like you would any other array.

for (int i =0; i < 3; ++i) // 3 is a magic number
{ 
    System.out.println(accoutList[i].ToString());
    //Could also do accountList[i].(some BANK_AC property) ex: accountList[i].getbal();
} 



EDIT: Two spelling mistakes. Getting coffee.

This post has been edited by ipushmycar: 29 November 2012 - 06:10 AM

Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: For loop

Posted 29 November 2012 - 08:51 AM

If Bank_AC has a toString() method, and it is there that it should be, the class you are in shouldn't need a change if Bank_AC changes

    for(int i = 0; i < accountList.length; ++i) {
       if(accountList[i] != null)
            System.out.println(accoutList[i]);
    }


should to the trick
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,082
  • Joined: 19-March 11

Re: For loop

Posted 29 November 2012 - 08:56 AM

This should do it:

for (Bank_AC a : accountList.subList(0,3))
{ 
System.out.println(toString(a));
} 

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: For loop

Posted 29 November 2012 - 12:17 PM


   for (Bank_AC a : accountList.subList(0,3))  



Jon, accountList is an array :)
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,082
  • Joined: 19-March 11

Re: For loop

Posted 29 November 2012 - 12:23 PM

Well I'll be dipped, so it is.
Why is it called list if it's not a List?

Okay, never mind that. Go ahead and use the indexes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1