Results 1 to 14 of 14
- 11-27-2010, 06:41 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Java Inventory Program Part 5 Help
Hello again.
I am really stumped here, and I feel like I am missing something basic.
How can I call one string item from an arrayList?
I have an ArrayList with 5 string items. I want to be able to navigate through the list sequentially, but only display one item at a time.
-
Use a for loop and get the item with the ArrayList's get method, or you could use a for-each loop.
- 11-27-2010, 06:57 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
A for-each statement? Could you explain a for-each statement? I can't find it in my Java book.
EDIT: Actually, I looked it up. I can't use the for-each, because I also have to be able to navigate the arrayList backwards and forwards.Last edited by ladykrimson; 11-27-2010 at 07:03 PM.
- 11-27-2010, 08:13 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
OK, so I made a Gui component, but how do I call elements of the ArrayList from a different method? Am I going to have to declare the list in each method?
This is the code I have so far:
The three variables that I highlighted are in the main method. How do I get it to recognize bluList in the main method from within the inventoryPreviousButtonActionPerformed function?Java Code:import java.util.*; import javax.swing.*; public class InventoryGui extends javax.swing.JFrame { /** Creates new form JF1 */ public InventoryGui() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { inventoryGuiPanel = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); inventoryListWindow = new javax.swing.JTextArea(); jScrollPane2 = new javax.swing.JScrollPane(); inventoryTotalWindow = new javax.swing.JTextArea(); jScrollPane3 = new javax.swing.JScrollPane(); inventoryNavigationWindow = new javax.swing.JTextArea(); inventoryTotalLabel = new javax.swing.JLabel(); inventoryPreviousButton = new javax.swing.JButton(); inventoryNextButton = new javax.swing.JButton(); String sOutPut = ""; BlurayDVD[] blu = new BlurayDVD[6]; blu[0] = new BlurayDVD("Independence Day", 6, 5.23, 1, "Standard DVD"); System.out.println(blu); blu[1] = new BlurayDVD("X-Men", 8, 4.73, 2, "Standard DVD"); System.out.println(blu); blu[2] = new BlurayDVD("Jurassic Park", 3, 6.01, 3, "Standard DVD"); System.out.println(blu); blu[3] = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD"); System.out.println(blu); blu[4] = new BlurayDVD ("The Mist", 3, 6.58, 5, "BluRay DVD"); System.out.println(blu); blu[5] = new BlurayDVD ("Independence DaySE", 4, 7.81, 6, "BluRay DVD"); System.out.println(blu); ArrayList<BlurayDVD> bluList = new ArrayList<BlurayDVD>(); BlurayDVD b0 = new BlurayDVD ("The Mist", 3, 6.58, 5, "Standard DVD"); BlurayDVD b1 = new BlurayDVD ("Independence Day", 6, 5.23, 1, "Standard DVD"); BlurayDVD b2 = new BlurayDVD ("X-Men", 8, 4.73, 2,"BluRay DVD"); BlurayDVD b3 = new BlurayDVD ("Jurassic Park", 3, 6.01, 3,"BluRay DVD"); BlurayDVD b4 = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD"); BlurayDVD b5 = new BlurayDVD("Independence DaySE", 4, 7.81, 6, "BluRay DVD"); bluList.add(b0); bluList.add(b1); bluList.add(b2); bluList.add(b3); bluList.add(b4); bluList.add(b5); Collections.sort(bluList, new Comparator<BlurayDVD>() { public int compare(BlurayDVD o1, BlurayDVD o2) { BlurayDVD p1 = (BlurayDVD) o1; BlurayDVD p2 = (BlurayDVD) o2; return p1.getDvdTitle().compareToIgnoreCase(p2.getDvdTitle()); } }); for (int i = 0; i < bluList.size(); i++) { sOutPut += String.format(" %d\t%18s\t%d\t$%.2f\t$%.2f\t$%.2f\t%s\n", bluList.get(i).dvdItem, bluList.get(i).getDvdTitle(), bluList.get(i).getDvdStock(), bluList.get(i).getDvdPrice(), bluList.get(i).value(), bluList.get(i).getRestockFee(), bluList.get(i).getCategory()); } setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("DVD Inventory"); setBackground(new java.awt.Color(255, 255, 255)); setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS)); inventoryListWindow.setColumns(20); inventoryListWindow.setFont(new java.awt.Font("Verdana", 1, 13)); inventoryListWindow.setRows(5); jScrollPane1.setViewportView(inventoryListWindow); jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); inventoryTotalWindow.setColumns(20); inventoryTotalWindow.setFont(new java.awt.Font("Verdana", 1, 13)); inventoryTotalWindow.setRows(5); jScrollPane2.setViewportView(inventoryTotalWindow); jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); inventoryNavigationWindow.setColumns(20); inventoryNavigationWindow.setFont(new java.awt.Font("Verdana", 1, 13)); inventoryNavigationWindow.setRows(5); jScrollPane3.setViewportView(inventoryNavigationWindow); inventoryTotalLabel.setFont(new java.awt.Font("Verdana", 1, 13)); inventoryTotalLabel.setText("The total value of the inventory is"); inventoryPreviousButton.setText("Previous"); inventoryPreviousButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inventoryPreviousButtonActionPerformed(evt); } }); inventoryNextButton.setText("Next"); inventoryNextButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inventoryNextButtonActionPerformed(evt); } }); javax.swing.GroupLayout inventoryGuiPanelLayout = new javax.swing.GroupLayout(inventoryGuiPanel); inventoryGuiPanel.setLayout(inventoryGuiPanelLayout); inventoryGuiPanelLayout.setHorizontalGroup( inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(inventoryGuiPanelLayout.createSequentialGroup() .addGroup(inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(inventoryGuiPanelLayout.createSequentialGroup() .addGap(251, 251, 251) .addComponent(inventoryPreviousButton) .addGap(150, 150, 150) .addComponent(inventoryNextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(inventoryGuiPanelLayout.createSequentialGroup() .addGap(21, 21, 21) .addGroup(inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inventoryGuiPanelLayout.createSequentialGroup() .addComponent(inventoryTotalLabel) .addGap(18, 18, 18) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 759, Short.MAX_VALUE)))) .addContainerGap(18, Short.MAX_VALUE)) ); inventoryGuiPanelLayout.setVerticalGroup( inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(inventoryGuiPanelLayout.createSequentialGroup() .addGap(25, 25, 25) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(46, 46, 46) .addGroup(inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(inventoryGuiPanelLayout.createSequentialGroup() .addComponent(inventoryTotalLabel) .addGap(64, 64, 64)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, inventoryGuiPanelLayout.createSequentialGroup() .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(55, 55, 55))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(inventoryPreviousButton) .addComponent(inventoryNextButton)) .addContainerGap(44, Short.MAX_VALUE)) ); getContentPane().add(inventoryGuiPanel); pack(); }// </editor-fold> public void inventoryPreviousButtonActionPerformed(java.awt.event.ActionEvent evt) { for (int i =0; i < [B][I]bluList[/I][/B].indexOf(0); i++) { [B][I]bluList[/I][/B].listIterator(0); [B][I]sOutPut[/I][/B] += String.format(" %d\t%18s\t%d\t$%.2f\t$%.2f\t$%.2f\t%s\n", bluList.get(i).dvdItem, bluList.get(i).getDvdTitle(), bluList.get(i).getDvdStock(), bluList.get(i).getDvdPrice(), bluList.get(i).value(), bluList.get(i).getRestockFee(), bluList.get(i).getCategory()); } } public void inventoryNextButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JF2().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JPanel inventoryGuiPanel; private javax.swing.JTextArea inventoryListWindow; private javax.swing.JTextArea inventoryNavigationWindow; private javax.swing.JButton inventoryNextButton; private javax.swing.JButton inventoryPreviousButton; private javax.swing.JLabel inventoryTotalLabel; private javax.swing.JTextArea inventoryTotalWindow; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; // End of variables declaration } class DVD { String dvdTitle; int dvdStock; double dvdPrice; int dvdItem; double titleValue; double totalValue; String dvdCategory; double initialTitleValue; public DVD(String title, int stock, double price, int item, String category) { dvdTitle = title; dvdStock = stock; dvdPrice = price; dvdItem = item; dvdCategory = category; } //end five-argument constructor // set DVD title public void setDvdTitle(String title) { dvdTitle = title; } //end method setDvdTitle //return DVD title public String getDvdTitle() { return dvdTitle; } //end method getDvdTitle //set number if DVDs in stock public void setDvdStock(int stock) { dvdStock = stock; } //end method setDvdStock //retrieve the number of DVD's in stock public int getDvdStock() { return dvdStock; } //end method getDvdStock // set the price of the title public void setDvdPrice(double price) { dvdPrice = price; } //end method setDvdPrice //retrieve the price of this title public double getDvdPrice() { return dvdPrice; } //end method getDvdPrice // set the DVD item number public void setDvdItem(int item) { dvdItem = item; } //end method setDvdItem //retrieve the DVD item number public int getDvdItem() { return dvdItem; } //end method getDvdItem public double getTotal(BlurayDVD[] blu) { for (int i = 0; i < blu.length; i++) { totalValue += blu[i].value(); } return totalValue; } // end method getTotal } //end class DVD // subclass BluRayDVD class BlurayDVD extends DVD { // adds one field double restockFee; // constructor for BlurayDVD public BlurayDVD(String title, int stock, double price, int item, String category) { // add these from superclass DVD super(title, stock, price, item, category); } // method to set the category public void setCategory(String category) { dvdCategory = category; } // end method setCategory // method to retrieve the category public String getCategory() { return dvdCategory; } // end method getCategory public void setRestockFee() { restockFee = initialTitleValue *.05; } // retrieve the restocking fee public double getRestockFee() { return restockFee; } // end method getRestockFee // calculate inventory value for each title and cumulatively calculate the total value of the entire inventory public double value() { initialTitleValue = (dvdPrice * dvdStock); // initial value restockFee = initialTitleValue *.05; titleValue = initialTitleValue + restockFee; // add restock fee to the initial value return titleValue; } // end method value } // end subclass BlurayDVD
-
- 11-27-2010, 10:55 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Um...I wasn't supposed to post the code?
:confused:
-
Posting code is fine, but if the program is very large, most won't read it. Myself, I have too much work to catch up on to be able to go through a large bit of code like that, and most here are in the same shoes. My experience has been that the quickest help comes if one posts a small compilable program that runs and demonstrates the error, an SSCCE. In other words you'll usually get more to read your code and help if you don't post your program but rather a small compilable test program
- 11-27-2010, 11:02 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
OK......
sorry.
-
No problem. Your problem is that you need to declare the ArrayList in the class, not in a method. Right now it is only visible in the class it was declared in, and to be visible in the whole program, to have "class scope" it needs to be declared as a class field.
- 11-27-2010, 11:06 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Thank you.
- 11-28-2010, 12:38 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
-
It has nothing to do with the variable being an ArrayList whatsoever. Any variable declared within a method is visible only within that method, and any variable declared within a block (say within an if block) is only visible within that block. These are part of Java's scoping rules.
- 11-28-2010, 02:05 AM #13
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
-
If this is confusing, try reading what someone else says about this. Please Google "Java scoping rules" and search this forum on this topic, and it may become clearer.
A (hopefully) simple example:
Java Code:public class ScopeTest { // variable below is visible throughout the entire class private int classScopeVariable = 3; public void myMethod() { // variable below is only visible within this method. int methodScopeVariable = 4; } public void anotherMethod() { // statement below works because classScopeVariable is "visible" within this method System.out.println("class scope variable is: " + classScopeVariable); // statement below causes error because methodScopeVariable is not visible here System.out.println("method scope variable is: " + methodScopeVariable); } }
Similar Threads
-
Help with inventory program part 5 PLEASE!
By Exether in forum New To JavaReplies: 2Last Post: 08-09-2010, 06:25 AM -
Inventory program part 3 (again prob yes)
By Exether in forum New To JavaReplies: 16Last Post: 07-26-2010, 05:06 AM -
Inventory Program Part 2 of 6
By tlouvierre in forum New To JavaReplies: 2Last Post: 05-28-2009, 01:30 AM -
Inventory Program Part 3 ~ please help!
By marMcD in forum New To JavaReplies: 13Last Post: 02-25-2009, 05:57 AM -
Java Inventory Program Part 3
By ljk8950 in forum New To JavaReplies: 18Last Post: 07-28-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks