6 Replies - 338 Views - Last Post: 18 May 2014 - 04:19 PM Rate Topic: -----

#1 program94  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-April 14

DATABASE

Posted 18 May 2014 - 02:23 PM

HI I AM WORKING ON MY FINAL PROJECT AND I AM MAKING A DATABASE


I HAVE THREE CLASSES
FCBARCELONA.JAVA, PLAYER.JAVA AND ROSTER.JAVA

I AM WORKING ON ADD A PLAYER METHOD AND DISPLAY ROSTER METHOD BUT IT IS GIVING ME RUN TIME ERROR PLEASE HELP IN FIXING THEM


FC BARECELONA.JAVA


  


import java.util.Scanner;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;

/**
 * Creates a ShopMenu object that creates a GUI environment which simulates shopping
 */
public class FCBarcelona
{
	public static void main(String[] args) throws IOException
    {
    	Player1 myPlayer1 = new Player1();
    }
}

/**
 * The ShopMenu class provides the GUI environment for testing methods of the
 * ShoppingCart and Item classes.  It uses MouseListener and ActionListener interfaces.
 */
class Player1 extends JFrame implements ActionListener, MouseListener
{
		// window dimensions
		private static final int FRAME_WIDTH       = 1400; // adjust to have a wider/slimmer window
 		private static final int FRAME_HEIGHT      = 500; // adjust to have a taller/shorter window
 		private static final int FRAME_X_ORIGIN    = 150;
 		private static final int FRAME_Y_ORIGIN    = 150;

		// database
		private Roster myRoster;

		// declare GUI elements
		private JLabel title, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve;
		private JPanel north;
		private JTextArea display;
		private JMenuBar menuBar;
		private JMenu menu;
		private JMenuItem open, save, quit, help;
		private JScrollPane scroller;

		/**
		 * The constructor instantiates all of the gui elements, adds them to the content pane,
		 * and adds the listeners to the GUI elements
		 */
		public Player1()
		{
			myRoster = new Roster();

			// set up the window
			setTitle("FC Barcelona");
 			setSize(FRAME_WIDTH, FRAME_HEIGHT);
 			setResizable(false);
 			setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

 			// set up the main display area and make it scrollable
 			display = new JTextArea();
 			display.setFont(new Font("Courier",Font.BOLD,12));
 			display.setEditable(false);
 			display.setBorder(BorderFactory.createLineBorder(Color.blue));
 			scroller = new JScrollPane(display);

 			// set up the menu items
			title = new JLabel(" MENU:");
 			title.setForeground(Color.blue);
			two = new JLabel("Add a player");
			three = new JLabel("Search for a player by name");
			four = new JLabel("Display roster");
			five = new JLabel("Remove a player");
			six = new JLabel("Sort the roster alphabetically by name");
			seven = new JLabel("Sort the roster by number of games played");
			eight = new JLabel("Update the number of games played by player");
			nine = new JLabel("Players who play at a specific position");
			ten = new JLabel("Players who played more than a number of seasons");
			eleven = new JLabel("Total number of goals scored");
			twelve = new JLabel("Total number of games played");





			// set up the menu bar
			menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			save = new JMenuItem("Open");
			open = new JMenuItem("Save");
			quit = new JMenuItem("Quit");
			help = new JMenuItem("Help");
			save.addActionListener(this);
			open.addActionListener(this);
			quit.addActionListener(this);
			help.addActionListener(this);
			menu = new JMenu("File");
			menu.add(save);
			menu.add(open);
			menu.add(quit);
			menu.add(help);
			menuBar.add(menu);

			// Make the panel for the menu and add the menu items to it
			north = new JPanel();
			north.add(title);
			north.add(two);
			north.add(three);
			north.add(four);
			north.add(five);
			north.add(six);
			north.add(seven);
			north.add(eight);
			north.add(nine);
			north.add(ten);
			north.add(eleven);
			north.add(twelve);


			north.setLayout(new GridLayout(4,3)); // the menu has 6 rows and 1 column

			setLayout(new GridLayout(2,1)); // the window has 1 row and two columns
			add(north);
			add(scroller);

			//*** OR

			setLayout(new BorderLayout()); // sets the layout so you can put things in the North, South, East
											// West or Center

			add(north, BorderLayout.NORTH); // add the menu to the left
			add(scroller, BorderLayout.CENTER); // add the text area to center, which takes up any unused space


			// make all of the menu items listen
			two.addMouseListener(this);
			three.addMouseListener(this);
			four.addMouseListener(this);
			five.addMouseListener(this);
			six.addMouseListener(this);
			seven.addMouseListener(this);
			eight.addMouseListener(this);
			nine.addMouseListener(this);
			ten.addMouseListener(this);
			eleven.addMouseListener(this);
			twelve.addMouseListener(this);


			// when the user clicks the X in the top right corner, the program will exit
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setVisible(true); // make it visible LAST
		}
		/**
		 * required by the MouseListener interface.  Invoked when the mouse is clicked.
		 * @param e the MouseEvent that triggered the method
		 */
		public void mouseClicked(MouseEvent e)
		{
			// nothing needed, but method required by interface
		}
		/**
		 * required by the MouseListener interface.  Invoked when the mouse is released.
		 * @param e the MouseEvent that triggered the method
		 */
		public void mouseReleased(MouseEvent e)
		{
			// nothing needed, but method required by interface
		}
		/**
		 * required by the MouseListener interface.  Invoked when no mouse buttons are clicked and
		 * the mouse pointer enters a particular gui element.  In this implementation, if the mouse
		 * pointer enters one of the menu labels, the label is set to red.
		 * @param e the MouseEvent that triggered the method
		 */
		public void mouseEntered(MouseEvent e)
		{
			JLabel label = (JLabel) e.getSource();
			label.setForeground(Color.red);
		}
		/**
		 * required by the MouseListener interface.  Invoked when no mouse buttons are clicked and
		 * the mouse pointer leaves a particular gui element.  In this implementation, if the mouse
		 * pointer leaves one of the menu labels, the label is set to back to black.
		 * @param e the MouseEvent that triggered the method
		 */
		public void mouseExited(MouseEvent e)
		{
			JLabel label = (JLabel) e.getSource();
			label.setForeground(Color.black);
		}
		/**
		 * required by the MouseListener interface.  Invoked when the left mouse button is pressed.
		 * In this implementation, if the mouse is pressed on a particular labels, that menu item
		 * is invoked.
		 * @param e the MouseEvent that triggered the method
		 */
		public void mousePressed(MouseEvent e)
		{
	    	String name;
			int age;
			int seasonsPlayed;
			String position;
			int gamesPlayed;
			int goalsScored;
			JLabel label = (JLabel) e.getSource();

			if (label.equals(two)) // Add an item to the cart
			{
					display.setText(""); // optional.. empties out the text area
					name = JOptionPane.showInputDialog("Enter the name of the player");
					age = Integer.parseInt(JOptionPane.showInputDialog("Enter the age of the player"));
					seasonsPlayed = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of seasons played by the player"));
					position = JOptionPane.showInputDialog("Enter the position the player plays at");
					gamesPlayed = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of games played by the player"));
					goalsScored = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of goals scored by the player"));



					// *** create a new item and add it to the cart
					myRoster.addPlayer(new Player(name, age, seasonsPlayed, position,gamesPlayed,goalsScored));
					showRoster(); // updates the window with the contents of the whole cart
			}
//			else if (label.equals(three)) // Find an item in the cart
//			{
//					itemName = JOptionPane.showInputDialog("Item you are looking for");
//					Item i = myCart.findItem(new Item(itemName));
//					if(i == null)
//					{
//						showCart();
//						JOptionPane.showMessageDialog(null,"You have no " +itemName + " in your cart.");
//					}
//
//					else
//						showItem(i);
//			}
			else if (label.equals(four)) // Display the cart
			{
				showRoster();
			}
//			else if (label.equals(five)) // Remove an item
//			{
//				itemName = JOptionPane.showInputDialog("Item to remove");
//				if (myCart.removeItem(new Item(itemName)))
//					JOptionPane.showMessageDialog(null,itemName + " has been removed from your cart.");
//				else
//					JOptionPane.showMessageDialog(null,"You have no " + itemName + " in your cart to remove.");
//			}
//			else if (label.equals(six))  // Check out
//			{
//				JOptionPane.showMessageDialog(null,String.format("The total cost of your purchase before tax is $%.2f",myCart.getTotalCost()));
//				myCart.emptyCart();
//				showCart();
//			}
		}

		/**
		 * actionPerformed is required by the ActionListener interface.
		 * It is invoked whenever one of the menu items is selected.
		 * The possible menu items that can be activated are quit, save and load.
		 * @param e the ActionEvent that triggered the method
		 */
		public void actionPerformed(ActionEvent e)
		{
			String menuName = e.getActionCommand();

			if (menuName.equals("Quit"))
				System.exit(0);
			else if (menuName.equals("Save"))
			{
				myRoster.saveToFile();
			}
			else if (menuName.equals("Help"))
			{
				display.setText(" \t This program allows the user to maintain a database pertaining to the roster of FC Barcelona." +"It keeps track of each player that is on"
				+"the roster. This allows \nthe user to see how good/experienced a player is." + "\n\n \t Records in the system the name of the player," +
					"the age of the" +" player,number of seasons played by the player,the position the player plays at,number of goals" +"\nscored by the player and the number of games"
				+" played by the player." + " Under the file menu you can see these three option: open,save and quit. Open: loads the roster."
				+"\nSave: saves the roster. Quit: Exits the program." + " Also, there are eleven more options in the program. \n\n\t" + " Add a player: " +
				"Adds a new player to the roster. Remove player: removes a player from the roster. Search for a player by name: searches for a player by name. " +
				"\nDisplay Roster: displays the entire roster.\n\n\tYou can also sort the roster in many different ways and do some calculations."
					+" Sort the roster by number of games played: sorts the roster. Players who have played more"
				+" than a number of\nseasons: lists the players who have played more than a number of seasons. Update the number of games played by player: "
				+"updates the number of games played by a specific player.\nTotal number of goals scored: Adds the number of goals scored by all the" +
				" players on the roster. Search for a player by name: searches for a player by name. \nSort the roster alphabatically by name: "
				+"sorts the roster by name. Players who play at a specific position: list the players who play at a specific position. \n"
				+"Total number of games played: adds up the number of games played by the players on the roster. ");
//				try {
//
//
//			    myRoster.loadFromFile();
//				}
//				catch(IOException a) {}
				//showRoster();
			}

		}

		/**
		 * displays the cart contents in the display pane
		 */
		private void showRoster()
		{
				display.setText("Roster:\n" +
					String.format("%-20s%-12s%-20s%-20s%-20s%-12s\n","Name", "Age", "Seasons Played", "Position", "Games Played", "Goals Scored")+
    				myRoster.toString());
		}
//
//  		private void showPlayer(Item i)
//		{
//			display.setText("Found Item:\n" +
//					String.format("%-20s%-12s%-6s%-15s\n","Name", "Price", "QTY", "Total Price")+
//    				i.toString());
//		}
}



PLAYER.JAVA

[code]

/**
 * Represents an item in a shopping cart.
 */
public class Player
{
    private String name;
    private int age;
    private int seasonsPlayed;
    private String position;
    private int gamesPlayed;
    private int goalsScored;

	/**
	 * Creates an item with a specified name, and a price and quantity of 0.
	 * @param itemName the name of the item
	 */
	public Player(String plName, int plAge, int seaPl, String pos, int gaPl, int goSc)
	{
		name = plName;
		age = plAge;
		seasonsPlayed = seaPl;
		position = pos;
		gamesPlayed = gaPl;
		goalsScored = goSc;

	}

   /**
     * returns a formatted String of the item
     * @return the string version of the item
     */
    public String toString ()
    {
		return String.format("%-20s%7.2f%4s%-6d$%10.2f",name, age," ", seasonsPlayed," ", position, " ", gamesPlayed, " ", goalsScored);
    }

    /**
     * returns the item price
     * @return the item price
     */
    public int getAge()
    {
		return age;
    }

  	/**
     * returns the item name
     * @return the item name
     */
    public String getName()
    {
		return name;
    }

  	/**
     * returns the item quantity
     * @return the item quantity
     */
    public int getSeasonsPlayed()
    {
		return seasonsPlayed;
    }

    public String getPosition()
    {
    	return position;
    }

    public int getGamesPlayed()
    {
    	return gamesPlayed;
    }

    public int getGoalsScored()
    {
    	return goalsScored;
    }

}












ROSTER.JAVA








/****************************************************************
 * ShoppingCart.java
 *
 * A class that models a set of  Item objects.  A shopping cart has
 * an ArrayList of items.
 *
 ****************************************************************/

 import java.util.ArrayList;
 import java.io.*;
 import java.util.Scanner;
 import javax.swing.JFileChooser;
 import javax.swing.*;

/**
 * A class that models a set of  Item objects.  A shopping cart has
 * an ArrayList of items.
 */
public class Roster
{
	// instance field
	private ArrayList <Player> play;

	/**
	 * Constructs an empty shopping cart
	 */
	public Roster()
	{
		play = new ArrayList <Player>();
	}

	public void addPlayer(Player player)
	{
		play.add(player);
	}

	public String toString()
	{
		String s = "";
		for(Player p: play)
		{
			s+= p + "\n";
		//	s += p.toString() + "\n";
		}
			return s;
	}

	public void saveToFile()
	{
			String fileName = JOptionPane.showInputDialog("Enter the file name");
    	try
    	{
    		PrintWriter out = new PrintWriter(fileName);
	    	for(Player p: play)
	    	{
	    		out.println(p.getName());
	    		out.println(p.getAge());
	    		out.println(p.getSeasonsPlayed());
	    		out.println(p.getPosition());
	    		out.println(p.getGamesPlayed());
	    		out.println(p.getGoalsScored());
	    	}
	    	out.close();
	    	JOptionPane.showMessageDialog(null, fileName + " has been saved.");
    	}
    	catch(IOException exception)
    	{
    		System.out.println("File problem - could not save");
    	}

	}

	public void loadFromFile() throws IOException
	{
			JFileChooser chooser = new JFileChooser();
    	chooser.requestFocus();
    	File infile = null;
    	if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
	   	  {
	   	  	infile = chooser.getSelectedFile();

	   	  }
	   	Scanner in = new Scanner(infile);
	   	while(in.hasNext())
	   	{
	   		String name = in.nextLine();
	   		int age = in.nextInt();
	   		int seasonsPlayed = in.nextInt();
	   		String position = in.nextLine();
	   		int gamesPlayed = in.nextInt();
	   		int goalsScored = in.nextInt();
	   		addPlayer(new Player(name, age, seasonsPlayed, position, gamesPlayed, goalsScored));
	   		in.nextLine();

	   	}
	   	in.close();


	}


}





























[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: DATABASE

#2 program94  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-April 14

Re: DATABASE

Posted 18 May 2014 - 02:28 PM

THIS IS THE ERROR I GET WHEN I ENTER ALL THE INFO IN THE JOPTION PANE IN ADD A PLAYER


--------------------Configuration: <Default>--------------------
Exception in thread "AWT-EventQueue-0" java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.util.Formatter.format(Formatter.java:2423)
at java.lang.String.format(String.java:2797)
at Player.toString(Player.java:34)
at java.lang.String.valueOf(String.java:2854)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at Roster.toString(Roster.java:43)
at Player1.showRoster(FCBarcelona.java:300)
at Player1.mousePressed(FCBarcelona.java:218)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.window.dispatchEventImpl(window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Process completed.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,676
  • Joined: 27-December 08

Re: DATABASE

Posted 18 May 2014 - 02:31 PM

The %f formatter is a floating-point number (double or float). Use the %d formatter for a decimal (base-10) integer (ie., an int).
Was This Post Helpful? 0
  • +
  • -

#4 program94  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-April 14

Re: DATABASE

Posted 18 May 2014 - 02:38 PM

#3 macosxnerd101

STILL GIVES ME THE SAME ERROR DID NOT HELP
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,676
  • Joined: 27-December 08

Re: DATABASE

Posted 18 May 2014 - 02:47 PM

My fix will take care of the particular Exception you described. Are you sure you made the change at all necessary places in your code?

Also, just a heads up- typing in all caps is considered screaming or shouting on the forums. I don't think there is a need to scream at anyone here. ;)
Was This Post Helpful? 0
  • +
  • -

#6 program94  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 19-April 14

Re: DATABASE

Posted 18 May 2014 - 03:51 PM

yes I did and didnt help

Please look in the code if i need to make any other changes or try to run it if u cn so that you cn see what exactly is wrong with the code I am working on add a player thank you :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,676
  • Joined: 27-December 08

Re: DATABASE

Posted 18 May 2014 - 04:19 PM

Please post your revised code then.

Edit: By the way- I ran your code with my suggestion of using the %d formatter, and got a new Exception, which is quite self-explanatory:

Quote

Exception in thread "AWT-EventQueue-0" java.util.IllegalFormatPrecisionException: 2

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1