Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with ArrayList tables  RSS feed

 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList series beginner question?
Hello I really need some help on my assignment. I'm nearly done but am having trouble
I'm trying to print out consecutive tables with the updated arraylists from years 0 to 10 for my invest.accumulate method which just accumulates interest and returns it based on the years elapsed...


My output now looks something like this: (The table is correct but this messes up the alignment. so no problem with the table..)

run:
Principal Interest Maturity Compound Mode Accum.Value Tot.Interest
========= ======== ======== ============= =========== ============
5000 8.25 5 quarterly 5000 0
12000 10.8 10 daily 12000 0
2000 7.3 5 quarterly 2000 0
10000 5.55 3 monthly 10000 0
2000 4.5 1 daily 2000 0
5000 9.15 10 monthly 5000 0


I can't figure out how to print more than one table with the accumulated values updated 10 times.. the accumulated value automatically updates the tot.interest.

I'm not sure how i can modify an array after I have already entered it into the list so that i can print out an updated table for all the arrays... can someone please help?? Thanks a lot

[Added code tags - see UseCodeTags for details]
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have some very peculiar names for your variables. You don’t need two Scanners. You can read nextInt() etc directly from the file. If you are using nextLine() maybe you should use while(reader.hasNextLine())... as your loop test.
The reason the alignment is off is that (outside code tags) this website renders multiple spaces as a single space. You could try   instead of ordinary space.
You ought to override the toString() method in your CDlist class, rather than creating a toPrint() method. You can cheat by making toPrint private (or even not making it private) and writing this Then you simply print an instance of that class: System.out.println(minister);
You don’t any more than that in the main method; in my opinion the ideal length for a main method is 1 statement.
I am not quite sure about what you want, so I am guessing a bit, but let’s have a go: How about a matureAllInvestments(int years) method in CDlist. You can guess its intent from its name. Then you can loop, calling matureAllEtc several times and printing the CDlist object each time.
 
tom jenkins
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I think I resolved the problem:

I added your suggested method to the CDlist class:

public void matureAllInv(int years)
{
for (int i = 0; i < CDarrays.size(); i++)
{
CD pop = CDarrays.get(i);
pop.accumulate(years);

}
}

and this to the main:
for (int i = 0; i <= years; i++)
{

minister.matureAllInv(i);
System.out.println(minister.toString());

}

worked like a charm! big thanks!

p.s. i'd like to make the main method only 1 statement long also, but unfortunately my professor requires a different method for main..
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!