PDA

View Full Version : Sigh, Java HW Help


Pages : 1 [2]

rhink
01-26-07, 09:28 PM
Declare the string in a place it won't get overwritten every loop iteration.

Marvel_us
01-26-07, 09: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, 09:39 PM
Actually now that I try it, it won't work locally it should be an instance variable.

Marvel_us
01-26-07, 10:40 PM
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.

rhink
01-26-07, 11:34 PM
Might need a separator between the lines....or actually make them multiple lines.

Marvel_us
01-26-07, 11:36 PM
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-26-07, 11:38 PM
can you post your output?

Marvel_us
01-26-07, 11:48 PM
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.

rhink
01-26-07, 11:51 PM
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, 12: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, 07:52 PM
Program complete! Thanks to all for the responses.(nana2)

Especially Cinema4DXL.