Results 1 to 9 of 9
  1. #1
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Troubles with Vector

    Hello...

    I am working on a code with classmates. One of my classmates started this code, but instead of using Arrays, she used Vector.

    I am using TextPad. It will let me compile the code, but it won't let me run the code.

    Here is the error message I get.

    Charity.java: 173: warning: [unchecked] unchecked call to addElement (E) as a member of the raw type java.util.Vector
    charityListVector.addElement(donor);

    Here is the code. Please let me know if there is any way I can make this code work without having my classmate re-write the entire thing?

    Thanks for your help on this.

    -MP-

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.text.NumberFormat;
    import java.util.Vector;
    
    import javax.swing.*;
    
    
    public class Charity extends JFrame implements ActionListener,PropertyChangeListener
    {
    	// JLabels
    	private JLabel nameLabel;
    	private JLabel charityLabel;
    	private JLabel amountLabel;
    
    	// JTextFields
    	private JTextField nameField;
    	private JTextField charityField;
    	private JFormattedTextField amountField;
    
    	// JButtons
    	private JButton acceptButton;
    	private JButton viewCharityList;
    	private JButton newCharity;
    	private JButton exit;
    
    	// JPanel
    	private JPanel labelAndFieldPanel;
    	private JPanel buttonsPanel;
    	private JPanel textAreaPanel;
    	private JPanel mainPanel;
    
    	// JTextArea
    	private JTextArea viewCharityArea;
    
    	private NumberFormat amountFormat;
    	private static Vector charityListVector;
    	public Charity()
    	{
    		// setting frame properties
    		setSize(600, 500);
    		setLayout(new BorderLayout());
    		setTitle("Charity Details");
    		setResizable(false);
    
    		// instantiating JLabels
    		nameLabel = new JLabel("Enter Name");
    		charityLabel = new JLabel("Enter Charity Name");
    		amountLabel = new JLabel("Enter Amount");
    
    		// instantiating JTextFields
    		nameField = new JTextField(25);
    		charityField = new JTextField(25);
    		amountFormat =  NumberFormat.getInstance();
    		amountField = new JFormattedTextField(amountFormat);
    		amountField.setColumns(25);
    
    		labelAndFieldPanel = new JPanel(new GridLayout(3,2,0,10));
    
    		// adding labels and fields
    		labelAndFieldPanel.add(nameLabel);
    		labelAndFieldPanel.add(nameField);
    		labelAndFieldPanel.add(charityLabel);
    		labelAndFieldPanel.add(charityField);
    		labelAndFieldPanel.add(amountLabel);
    		labelAndFieldPanel.add(amountField);
    
    		// instantiating button objects
    		acceptButton = new JButton("Accept Charity");
    		acceptButton.addActionListener(this);
    
    		newCharity = new JButton("New Charity");
    		newCharity.addActionListener(this);
    
    		viewCharityList = new JButton("View Charity List");
    		viewCharityList.addActionListener(this);
    
    		exit = new JButton("Exit");
    		exit.addActionListener(new exitBtnListener());
    
    
    		// adding buttons to buttons panel
    		buttonsPanel = new JPanel();
    		buttonsPanel.add(acceptButton);
    		buttonsPanel.add(newCharity);
    		buttonsPanel.add(viewCharityList);
    		buttonsPanel.add(exit);
    
    		// instantiating JTextarea
    		viewCharityArea = new JTextArea();
    		viewCharityArea.setEditable(false);
    		viewCharityArea.setLineWrap(true);
    		viewCharityArea.setWrapStyleWord(true);
    
    		// setting scroll for text area
    		JScrollPane scrollPane = new JScrollPane(viewCharityArea);
    		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		// setting size of scroll pane
    		scrollPane.setPreferredSize(new Dimension(400, 200));
    		// adding components to main panel
    		mainPanel = new JPanel();
    		mainPanel.add(labelAndFieldPanel);
    		mainPanel.add(buttonsPanel);
    		mainPanel.add(scrollPane);
    
    		// adding main panel
    		add(mainPanel);
    
    }
    class exitBtnListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				System.exit(0);
    			}
    		}
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Object eventSource = event.getSource();
    		String donorName;
    		String charityName;
    		String amountText;
    		double amount;
    		Donor donor;
    
    		boolean isValid = true;
    		// if accept charity button is pressed
    		if(eventSource == acceptButton)
    		{
    			// getting values
    			donorName = nameField.getText().trim();
    			charityName = charityField.getText().trim();
    			amountText = amountField.getText().trim();
    			// if any value is empty, a message is displayed
    			if(donorName.equals(""))
    			{
    				JOptionPane.showMessageDialog(this, "Please enter donor's name.");
    				isValid = false;
    			}
    			if(charityName.equals(""))
    			{
    				JOptionPane.showMessageDialog(this, "Please enter charity name.");
    				isValid = false;
    			}
    			if(amountText.equals(""))
    			{
    				JOptionPane.showMessageDialog(this, "Please enter charity amount.");
    				isValid = false;
    			}
    			// if all the values are valid, charity is saved
    			if(isValid)
    			{
    				amount = ((Number)(amountField.getValue())).doubleValue();
    				if(amount<0.0)
    				{
    					JOptionPane.showMessageDialog(this, "Please enter right amount.");
    					return;
    				}
    				donor = new Donor(donorName, charityName, amount);
    				// storing donor objects in vector
    				charityListVector.addElement(donor);
    			}
    		}
    		// if new charity button is pressed, all the data is cleared
    		else if(eventSource == newCharity)
    		{
    			nameField.setText("");
    			charityField.setText("");
    			amountField.setText("");
    		}
    		// if view charity button is pressed, the list is displayed in the JTextArea
    		else if(eventSource == viewCharityList)
    		{
    			String charityDetails = "";
    			int numberOfCharities = charityListVector.size();
    			for(int i=0;i<numberOfCharities;i++)
    			{
    				donor = (Donor)charityListVector.elementAt(i);
    				charityDetails += "Donor Name : " + donor.getNameOfDonor();
    				charityDetails += "\nCharity Name : " + donor.getNameOfCharity();
    				charityDetails += "\nAmount : $" + donor.getAmount();
    				charityDetails += "\n\n";
    			}
    			viewCharityArea.setText(charityDetails);
    
    			}
    	}
    
    	public void propertyChange(PropertyChangeEvent arg0)
    	{
    
    	}
    
    	public static void main(String[] args)
    	{
    		Charity charity = new Charity();
    		charity.setVisible(true);
    		charityListVector = new Vector();
    	}
    
    	// Donor Class
    	class Donor
    	{
    		private String nameOfDonor;
    		private String nameOfCharity;
    		private double amount;
    
    		public Donor(String nameOfDonor, String nameOfCharity, double amount) {
    			super();
    			this.nameOfDonor = nameOfDonor;
    			this.nameOfCharity = nameOfCharity;
    			this.amount = amount;
    		}
    
    		public String getNameOfDonor() {
    			return nameOfDonor;
    		}
    
    		public void setNameOfDonor(String nameOfDonor) {
    			this.nameOfDonor = nameOfDonor;
    		}
    
    		public String getNameOfCharity() {
    			return nameOfCharity;
    		}
    
    		public void setNameOfCharity(String nameOfCharity) {
    			this.nameOfCharity = nameOfCharity;
    		}
    
    		public double getAmount() {
    			return amount;
    		}
    
    		public void setAmount(double amount) {
    			this.amount = amount;
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Troubles with Vector

    I am using TextPad. It will let me compile the code, but it won't let me run the code.

    Here is the error message I get.

    Charity.java: 173: warning: [unchecked] unchecked call to addElement (E) as a member of the raw type java.util.Vector
    charityListVector.addElement(donor);
    That's a warning, not an error. It does not prevent the code form executing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Troubles with Vector

    That's what every one says :)

    The only problem is...the program won't run. I get a box that looks like this...

    Troubles with Vector-error-message.jpg

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Troubles with Vector

    What's the exact command that resulted in that screen? Copy and paste it, don't post another screenshot.

    To copy from the command prompt, click the small black C:\ icon at the top left and look around the menu options.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Troubles with Vector

    Okay...here it is...

    Usage: java [-options] class [args...]
    (to execute a class)
    or java [-options] -jar jarfile [args...]
    (to execute a jar file)
    where options include:
    -d32 use a 32-bit data model if available
    -d64 use a 64-bit data model if available
    -server to select the "server" VM
    -hotspot is a synonym for the "server" VM [deprecated]
    The default VM is server.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    A ; separated list of directories, JAR archives,
    and ZIP archives to search for class files.
    -D<name>=<value>
    set a system property
    -verbose[:class|gc|jni]
    enable verbose output
    -version print product version and exit
    -version:<value>
    require the specified version to run
    -showversion print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
    include/exclude user private JREs in the version search
    -? -help print this help message
    -X print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
    enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
    disable assertions with specified granularity
    -esa | -enablesystemassertions
    enable system assertions
    -dsa | -disablesystemassertions
    disable system assertions
    -agentlib:<libname>[=<options>]
    load native agent library <libname>, e.g. -agentlib:hprof
    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
    load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
    load Java programming language agent, see java.lang.instrument

    -splash:<imagepath>
    show splash screen with specified image
    See Java SE Documentation at a Glance for m
    ore details.
    Press any key to continue . . .

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Troubles with Vector

    Where's the command you entered?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Outsider418 is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Troubles with Vector

    I'm working from TextPad...so when I run the code, I just go to "run java application".

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Troubles with Vector

    Oh well. I've never used TextPad so maybe someone else can help.

    Or maybe you can try to run that code from the command line and post back.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default Re: Troubles with Vector

    Hi....

    your code is working with command prompt.. try it better than textpad..
    there is no any warning or error while running this code in CMD.

    ------------------------------------------
    mcajavaprogramer

Similar Threads

  1. String to int troubles
    By Chuckling_atom in forum New To Java
    Replies: 13
    Last Post: 11-27-2011, 06:38 PM
  2. synchronizing troubles
    By AedonetLIRA in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-12-2010, 11:38 PM
  3. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  4. Char troubles
    By diggdude in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 03:55 PM
  5. JOptionPane Troubles
    By Tb0h in forum New To Java
    Replies: 4
    Last Post: 08-26-2009, 01:12 PM

Posting Permissions

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