Results 1 to 4 of 4
  1. #1
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default 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("");
    		}
    	}

  2. #2
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I have since addes this code

    Java Code:
    public void calculateSub()
    		{
    		perBoxx = perBox * boxess;
    		displayResults();
    		}
    But I get an error
    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?

  3. #3
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    No one? How do you even go about multiplying an index of an array to a user input?

  4. #4
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    This error is: perBox is array, boxess is int, you can't multiple an array with an int.
    You have 4 cigar's type and 4 price for each box of cigar's type, is it right?
    Then,boxess and perBoxx are what?

Similar Threads

  1. Trying to login to a website then grab another page
    By DaveTheAve in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 10:44 PM
  2. grab the textfile
    By Sticks_ll in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 02:23 PM
  3. Replies: 9
    Last Post: 09-11-2008, 03:51 AM
  4. Replies: 5
    Last Post: 07-30-2008, 10:30 AM
  5. Replies: 1
    Last Post: 03-25-2008, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •