Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: remembering the for loop initializer

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default remembering the for loop initializer

    	for( i = 0;i<count1;i++){
    			while(mod){
    			stockName[i] =	portFolioStockName();
    			stockSym[i]  =	portFolioStockSym();	
    			unitPrice[i] =	portFolioUnitPrice();
    			unitBought[i] = portFolioUnitBought();
    			portFolioSummary(i);
    			System.out.print("If Ok, enter 0 else enter 1 to modify :");
    			myInt = myScanner.nextInt();
                mod = (myInt !=0);
    			totalStock = unitPrice[i]*unitBought[i];
    			cashLeft = capital - totalStock;
    			}
    			mod=true;

    what can i do to remember the i = 0 initializer,because my for loop will be repeated for quite a number of times depending on the loop
    while(mod)
    and my arrays are created depending on the i initializer how can i remember the i ? because my current code overwrites the arrays[i] each time, my for loop is used again as i value starts at 0 again.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: remembering the for loop initializer

    just don't initialize i again:
    for(;i<count1;i++)
    {
    ...
    }

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: remembering the for loop initializer

    what about the termination condition, because count1 is a input by user of 1-99, if the user inputs 1 for a new for loop with the condition already at 5 wont it terminate immediately

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: remembering the for loop initializer

    ooh, you're adding stuff to an array from user input... in that case, i'd recommend the ArrayList class. It's simple and should fullfill all that you need. If you really want to use just an array, keep i the way it is in your code, but have a seperate counter for where next to put into the array.

    int counter = 0;
    for(int i = 0;i<count1;i++)
    {
    	while(mod)
    	{
    		stockName[counter] =	portFolioStockName();
    		stockSym[counter]  =	portFolioStockSym();	
    		unitPrice[counter] =	portFolioUnitPrice();
    		unitBought[counter] = portFolioUnitBought();
    		portFolioSummary(counter);
    		System.out.print("If Ok, enter 0 else enter 1 to modify :");
    		myInt = myScanner.nextInt();
    		mod = (myInt !=0);
    		totalStock = unitPrice[counter]*unitBought[counter];
    		cashLeft = capital - totalStock;
    	}
    	mod=true;
    }
    Last edited by helloworld922; July 15th, 2009 at 01:02 AM.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: remembering the for loop initializer

    i have not learnt ArrayList class and also i tried doing it your way but it is still the same the array got replaced when i enter a new 1

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: remembering the for loop initializer

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: remembering the for loop initializer

    my needs int and double types to store the amount of money input, so i cant use aaraylist

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: remembering the for loop initializer

    Yes you can, use the wrapper classes called Integer and Double, capital I and D

    final List<Integer> myIntegers = new ArrayList<Integer>();
     
    final List<Double> myDoubles = new ArrayList<Double>();

    // Json

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: remembering the for loop initializer

    lol, oops. i forgot to update counter.
    int counter = 0;
    for(int i = 0;i<count1;i++)
    {
    	while(mod)
    	{
    		stockName[counter] =	portFolioStockName();
    		stockSym[counter]  =	portFolioStockSym();	
    		unitPrice[counter] =	portFolioUnitPrice();
    		unitBought[counter] = portFolioUnitBought();
    		portFolioSummary(counter);
    		System.out.print("If Ok, enter 0 else enter 1 to modify :");
    		myInt = myScanner.nextInt();
    		mod = (myInt !=0);
    		totalStock = unitPrice[counter]*unitBought[counter];
    		cashLeft = capital - totalStock;
    		counter++;
    	}
    	mod=true;
    }

  10. #10
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: remembering the for loop initializer

    it still do not work, it gets replaced, my friend helped me do a 2dimensional array and it worked but i am having trouble learning how it works.

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: remembering the for loop initializer

    what exactly do you want to do?

  12. #12
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: remembering the for loop initializer

    if you take a look at my code(not yet completed but compilable)
    at Paste #128981 | LodgeIt!

    and run it, when you choose choice 1, it asks to create a stock portfolio, you create 1 stock and then it returns back to the menu,so you choose choice 1 again and create ANOTHER stock after filling in the stuffs it goes back to menu, then you choose choice 2 to show the stocks you have bought, however it only shows 1 stock when actually we have already created 2 stock.because the newly created stock overwrites the older stock

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: remembering the for loop initializer

    I believe this thread is related to

    http://www.javaprogrammingforums.com...ject-help.html

    Sorry for my lack of replies lotus. Swamped with work right now.. I'll hopefully pick this up tomorrow morning.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  14. #14
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: remembering the for loop initializer

    i solved it thanks for help helloworld

Similar Threads

  1. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  2. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM
  3. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  4. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 02:31 PM