1 Replies - 1017 Views - Last Post: 20 February 2008 - 12:56 PM Rate Topic: -----

#1 blake11   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 20-February 08

Question about Arrays

Post icon  Posted 20 February 2008 - 12:01 PM

Well, lets get this party started. I am currently working on a loan calculator. I understand how to create it using JOptionPane.showInputDialog and only allowing for one loan at a time. What I really want to do is create a single content pane that uses arrays to store up to 5 different loans. I envision a JMenu with “New Loan,” “Existing Loans,” and “Delete Loan.” Under the “New Loan” menu, I would have:

JMenu newLoan = new JMenu(“New Loan”);
JMenuItem nl;

nl = new JMenuItem(“Preferred Customer”);
nl.addActionListener(this);
newLoan.add(nl);

nl = new JMenuItem(“Regular Customer”);
nl.addActionListener(this);
newLoan.add(nl);


I would have two different selections, seeing as which one you pick determines the interest rate you are going to be stuck with. Now, I am still very new to Java, but I think I am getting a handle on it pretty quick. I am guessing I first want to create a class, something along the lines as NewLoan to handle this part of the program. Up until this point, I have been using just command line and JOptionPane; so, the contentPane is still very new to me. One of my many questions is how do I make this all take place within the same screen? I mean, when I click “Preferred Customer” and it sets off the if (actionCommand.equals(“Preferred Customer”)), how is this going to effect my contentPane. Because I would like pane to be “over written” I guess for lack of better words, when I select new selections from the menu bar. Does this have to do with setting things visible?

But here is my real question, how do I get this to work with arrays. I understand to this point how to send data to an array using for loops, but how do I get the data to go any of the available slots that might be vacant. I mean what if I have entered 5 loans, but then I delete loan 4 using the delete function, how do I then make the next “New Loan” go to the available slot. I am probably making this so much harder than it really is, and the answer I am staring at it in the book.

I have so many other questions; such as how do I turn automatically turn the objects that I am going to have populated by the array in the “Existing Loans” list into working buttons. But I am sure that is way over my head at this point. Any help/advice would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about Arrays

#2 Mavirick   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 72
  • Joined: 18-February 08

Re: Question about Arrays

Posted 20 February 2008 - 12:56 PM

If you're actually looking to delete and move around array data, you'll want to use the arrayList class. However to make things easier I would probably just set the loan at whatever index to zero and then use a simple if statement to fill that spot next, i.e.:

//lets say five loans have been filled in the loan[] array,
//but loan 4 must be deleted...

loan[3] = 0;

//... and then replaced

double newloanvalue = 50.0;

for(int i=0; i<loan.length; i++)
{
	 if(loan[i] == 0)
		  loan[i] = newloanvalue;
}


Of course it would have to be modified a bit if more than one loan was deleted before a spot was filled again, but you get the basic idea.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1