Results 1 to 4 of 4
Thread: how to grab info from array
- 12-08-2010, 08:27 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
how to grab info from array
So im supposed to select the cigar and county, then put in how many boxes I want to purchase. The applet then calculates the sub total, sales tax and the total price. The code below runs so you can see how the applet is supposed to be setup minus the calculations code which is what I dont know how to do. Do I need to use 'if' statements to grab the info from the arrays ( the cigar name/price and county/tax rate)
Java Code:/* filename: cigars author: walker date: December.2010 */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class cigars extends JApplet implements ActionListener { // colors Color black = new Color(0,0,0); Color white = new Color(255,255,255); //components JComboBox cigar; JLabel cigars; JComboBox counties; JLabel county; JLabel boxes; JTextField box; JLabel subTotal; JTextField subText; JLabel tax; JTextField taxTf; JLabel total; JTextField totalTf; JButton clear; JButton enter; //variables int cigarIndex; int countyIndex; int boxess; double perBoxx; double taxRatee; String selectedCigar; String selectedCounty; String[] cigarArray = {"Europa", "Pariodi", "DeNobili", "Honduas"}; Double[] perBox = {30.75, 38.50, 45.35, 29.75}; String[] countyArray = {"Allegeny", "Butler"}; Double[] taxRate = {.07, .06}; public void init() { setLayout(null); setSize(400,400); /* initialize*/ cigars = new JLabel(); cigars.setBounds(50, 50, 100, 20); cigars.setFont(new Font("Default", Font.PLAIN, 12)); cigars.setText("Select Cigar"); cigars.setForeground(black); cigars.setHorizontalAlignment(JLabel.LEFT); add(cigars); cigar = new JComboBox(cigarArray); cigar.setBounds(150, 50, 100, 20); cigar.setForeground(black); cigar.setBackground(white); cigar.setMaximumRowCount(4); add(cigar); county = new JLabel(); county.setBounds(50, 110, 100, 20); county.setFont(new Font("Default", Font.PLAIN, 12)); county.setText("Select County"); county.setForeground(black); county.setHorizontalAlignment(JLabel.LEFT); add(county); counties = new JComboBox(countyArray); counties.setBounds(150, 110, 100, 20); counties.setForeground(black); counties.setBackground(white); counties.setMaximumRowCount(2); add(counties); boxes = new JLabel(); boxes.setBounds(50, 140, 100, 20); boxes.setFont(new Font("Default", Font.PLAIN, 12)); boxes.setText("Number of boxes"); boxes.setForeground(black); boxes.setHorizontalAlignment(JLabel.LEFT); add(boxes); box = new JTextField(); box.setBounds(150, 140, 100, 20); box.setFont(new Font("Default", Font.PLAIN, 12)); box.setHorizontalAlignment(JTextField.CENTER); box.setForeground(black); box.setBackground(white); box.setEditable(true); add(box); subTotal = new JLabel(); subTotal.setBounds(50, 180, 100, 20); subTotal.setFont(new Font("Default", Font.PLAIN, 12)); subTotal.setText("Sub Total"); subTotal.setForeground(black); subTotal.setHorizontalAlignment(JLabel.LEFT); add(subTotal); subText = new JTextField(); subText.setBounds(150, 180, 100, 20); subText.setFont(new Font("Default", Font.PLAIN, 12)); subText.setHorizontalAlignment(JTextField.CENTER); subText.setForeground(black); subText.setBackground(white); subText.setEditable(false); add(subText); tax = new JLabel(); tax.setBounds(50, 200, 100, 20); tax.setFont(new Font("Default", Font.PLAIN, 12)); tax.setText("Sales Tax"); tax.setForeground(black); tax.setHorizontalAlignment(JLabel.LEFT); add(tax); taxTf = new JTextField(); taxTf.setBounds(150, 200, 100, 20); taxTf.setFont(new Font("Default", Font.PLAIN, 12)); taxTf.setHorizontalAlignment(JTextField.CENTER); taxTf.setForeground(black); taxTf.setBackground(white); taxTf.setEditable(false); add(taxTf); total = new JLabel(); total.setBounds(50, 220, 100, 20); total.setFont(new Font("Default", Font.PLAIN, 12)); total.setText("Total sales"); total.setForeground(black); total.setHorizontalAlignment(JLabel.LEFT); add(total); totalTf = new JTextField(); totalTf.setBounds(150,220,100,20); totalTf.setFont(new Font("Default", Font.PLAIN, 12)); totalTf.setHorizontalAlignment(JTextField.CENTER); totalTf.setForeground(black); totalTf.setBackground(white); totalTf.setEditable(false); add(totalTf); enter = new JButton(); enter.setBounds(50, 250, 100, 20); enter.setFont(new Font("Default", Font.PLAIN, 12)); enter.setText("Enter"); enter.setForeground(black); enter.setBackground(white); add(enter); enter.addActionListener(this); clear = new JButton(); clear.setBounds(150, 250, 80, 20); clear.setFont(new Font("Default", Font.PLAIN, 12)); clear.setText("Clear"); clear.setForeground(black); clear.setBackground(white); add(clear); clear.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); if(obj == enter) { getSelectedCigar(); } else if(obj == clear) { clearAll(); } } public void getSelectedCigar() { cigarIndex = cigar.getSelectedIndex(); selectedCigar = cigarArray[cigarIndex]; getCounty(); } public void getCounty() { countyIndex = counties.getSelectedIndex(); selectedCounty = countyArray[countyIndex]; getBoxes(); } public void getBoxes() { try { boxess = Integer.parseInt(box.getText()); box.setText("" + boxess); calculateSub(); } catch(NumberFormatException exception) { JOptionPane.showMessageDialog(this, "Please enter first integer!", "Number Format Error", JOptionPane.ERROR_MESSAGE); getBoxes(); } } public void calculateSub() { } public void clearAll() { cigar.setSelectedIndex(0); /* SETS JCOMBOBOX BACK TO FIRST ITEM */ box.setText(""); counties.setSelectedIndex(0); subText.setText(""); taxTf.setText(""); totalTf.setText(""); } }
- 12-08-2010, 09:04 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
I have since addes this code
But I get an errorJava Code:public void calculateSub() { perBoxx = perBox * boxess; displayResults(); }
cigars.java:236: operator * cannot be applied to java.lang.Double[],double
perBoxx = perBox * boxess;
^
what do I have to do to be able to get the price of cigar in the array to * the number of boxes purchased?
- 12-09-2010, 09:47 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
No one? How do you even go about multiplying an index of an array to a user input?
- 12-09-2010, 01:26 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Trying to login to a website then grab another page
By DaveTheAve in forum New To JavaReplies: 6Last Post: 04-14-2010, 09:44 PM -
grab the textfile
By Sticks_ll in forum New To JavaReplies: 3Last Post: 03-31-2009, 01:23 PM -
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09-11-2008, 02:51 AM -
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
Traversing through a stack of objects, and puttin them info in an array
By szimme101 in forum New To JavaReplies: 1Last Post: 03-25-2008, 05:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks