Welcome to Dream.In.Code
Become a Java Expert!

Join 150,356 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,903 people online right now. Registration is fast and FREE... Join Now!




Question about Arrays

 
Reply to this topicStart new topic

Question about Arrays

blake11
20 Feb, 2008 - 11:01 AM
Post #1

D.I.C Head
**

Joined: 20 Feb, 2008
Posts: 52


My Contributions
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:

CODE

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.

User is offlineProfile CardPM
+Quote Post

Mavirick
RE: Question About Arrays
20 Feb, 2008 - 11:56 AM
Post #2

D.I.C Head
**

Joined: 18 Feb, 2008
Posts: 59



Thanked: 7 times
My Contributions
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.:

CODE
//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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month