View Full Version : Sigh, Java HW Help
Declare the string in a place it won't get overwritten every loop iteration.
Marvel_us
01-26-07, 11:35 PM
Got ya, I would only need it to be local to that method right? I don't see a need to have it declared as an instance variable for that class. So this is what I have:
public void DisplayList()
{
String r ;
for(int i = 0; i < accounts.size(); i++)
{
Customer c = accounts.get( i ) ;
String s = c.toStringCustomer() ;
r += s ;
}
JTextArea text = new JTextArea(r, 40, 40);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane, "Account Information",
JOptionPane.INFORMATION_MESSAGE);
}
}
Marvel_us
01-26-07, 11:39 PM
Actually now that I try it, it won't work locally it should be an instance variable.
Marvel_us
01-27-07, 12:40 AM
Gah, it still isn't working. It's displaying information multiple times, there is something wrong with the r += s ; line but I still haven't figured out how to get around that.
Might need a separator between the lines....or actually make them multiple lines.
Marvel_us
01-27-07, 01:36 AM
Might need a separator between the lines....or actually make them multiple lines.
Not sure I'm getting you on that one. What do you mean by make them multiple lines?
Cinema4DXL
01-27-07, 01:38 AM
can you post your output?
Marvel_us
01-27-07, 01:48 AM
can you post your output?
Sure, this is how it looks when after entering only two customers. What's up with the null on the first one as well?
nullAccount Number: 1000 John Smith
Address:
Street: 7669 NW
City: Miami
State:
FL
Zip Code: 33054
Account Balance: 15065.0
Account Number: 1000 John Smith
Address:
Street: 7669 NW
City: Miami
State:
FL
Zip Code: 33054
Account Balance: 15065.0
Account Number: 1001 Rico Suave
Address:
Street: 7899 SW
City: Newark
State:
NJ
Zip Code: 6549
Account Balance: 7800.0
Don't mind the overall layout. I plan making it layout better. Just trying to get it to display the objects correctly first.
Not sure I'm getting you on that one. What do you mean by make them multiple lines?
Ahh was making an assumption about what your problem was, looks like I was wrong.
About the null- you never initialized your "r" variable. Think about what r += s; is really doing, especially the first time through.
Marvel_us
01-27-07, 02:01 AM
Ahh was making an assumption about what your problem was, looks like I was wrong.
About the null- you never initialized your "r" variable. Think about what r += s; is really doing, especially the first time through.
Yea, I just figured realized that. I initialized it as an empty string now but the duplication problem is still there. I know the problem is the r +=s but I'm going in circles trying to figure out the solution.
Marvel_us
01-27-07, 09:52 PM
Program complete! Thanks to all for the responses.(nana2)
Especially Cinema4DXL.
vBulletin® v3.7.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.