• Post Reply Bookmark Topic Watch Topic
  • New Topic

Repeating Output within a JOptionPane  RSS feed

 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently taking a Java class. I have aproject that wants me to repeat the output within a JOptionPane.
here is my code so far

public double valueOfMoney(double amountOfMoney, int lengthOfTime)
{
double x = amountOfMoney;
int y = lengthOfTime;
int loopcount = 0;
String allString;
while (loopcount < y)
{

x = x * (1 + constant);
x = (int)(x * 100) / 100.0;
loopcount += 1;
allString = "The amount at end of year " + loopcount + " is $" + x;
JOptionPane.showConfirmDialog(null, allString + "\n" + x, "Investment Resutls",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);


}
return x;
}

This prints out the results in the pane but it goes to another pane what I want is something like this all withing the same pane.

The amount of year 1 is $x.xx
The amount of year 2 is $x.xx
etc.....

This is possible? Can it be done from the same method? Please help. I greatly appreicate aany help.

Thanks,
Bradley Jordan
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem you have right now is that your creation of the JOptionPane is inside the loop, so each time through the loop, it will create a new JOptionPane.
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take the Option pane out of the loop and all it shows is the last value of the loop....
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you'll need to collect all the information from the loop.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bradley Jordan:
I take the Option pane out of the loop and all it shows is the last value of the loop....


Well, of course... Your allString variable only holds the printline for one iteration. Moving the JOptionPane out of the loop doesn't change that. You will have to modify your program so that your allString variable is for all the iterations.

Henry
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easier said then done; at least for me it is.... any hints on how about to do that?
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public double valueOfMoney(double amountOfMoney, int lengthOfTime)
{
double x = amountOfMoney;
int y = lengthOfTime;
int loopcount = 0;
String allString;
while (loopcount < y)
{

x = x * (1 + constant);
x = (int)(x * 100) / 100.0;
loopcount += 1;
allString = "The amount at end of year " + loopcount + " is $" + x + "\n";
allString = allString + allString;

}

return x;
JOptionPane.showConfirmDialog(null, allString, "Investment Resutls",JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);

Will this code work?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will this code work?


When you try to compile it, does it compile?

Once you are able to compile it, when you try to run it, does it produce the value that you want?

If it doesn't, what do you think is the problem?


Anyway, you probably would get better responses, if the question is more specific -- an open ended question like "will this work?" or "give me a hint?", is not as good as, "I have a string X, how do I append Y to it?".

Henry
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it complied but I don't have a machine to run it. I am using a on-line complier;I am at work. Thanks for the advice. It did insturct me to initizlae the allSting which I did to "0". I am waiting to see if it gives me the results I want. Is there any websites that I could run it through? MY work computer won;t let me install anything without express written consent.
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that didn't work.

I can't figure out how to get allSting to show for each iteration through throuth the loop...

I need to know how to do that...
 
Bradley Jordan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actully. I just got it working, I swithed the allString in front of the statement:

allString = allString + "The amount at end of year " + loopcount + " is $" + x + "\n";

initiziaied allString to "";

and took the JOptionPane out of the loop and got the results I needed.

Thanks for the help all that posted!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out how to get allSting to show for each iteration through throuth the loop...


You could try something like this:


Or event better:


EDIT: Nevermind, glad you got it working!
[ February 20, 2007: Message edited by: Garrett Rowe ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!