remembering the for loop initializer
Code :
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 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.
Re: remembering the for loop initializer
just don't initialize i again:
Code :
for(;i<count1;i++)
{
...
}
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
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.
Code :
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;
}
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
Re: remembering the for loop initializer
Re: remembering the for loop initializer
my needs int and double types to store the amount of money input, so i cant use aaraylist
Re: remembering the for loop initializer
Yes you can, use the wrapper classes called Integer and Double, capital I and D :D
Code :
final List<Integer> myIntegers = new ArrayList<Integer>();
final List<Double> myDoubles = new ArrayList<Double>();
// Json
Re: remembering the for loop initializer
lol, oops. i forgot to update counter.
Code :
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;
}
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.
Re: remembering the for loop initializer
what exactly do you want to do?
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
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.
Re: remembering the for loop initializer
i solved it thanks for help helloworld