Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JAVA: Trying to create World Cup program -- anyone willing to help?

  • 12-05-2006 8:44am
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey folks,

    I posted a thread on this a while ago, but it was mores specific. Basically it's getting more tricky, so while I still wanna do the program, I'm confusing myself, so I need a bit of help!

    It's gonna do a bit more, but this first draft should do the following:

    1. display a JFrame with 3 buttons (only addPlayer has a listener at the moment)
    2. when you click addPlayer, a JOptionPane asks for the name, age, and origin of the player
    3. after it does part 2, it should save the player's information to Players.txt, in the form 'name(age), from origin', and it should then go back to the JFrame
    4. if you click addPlayer again, it should again ask for name, age, and origin, and after it gets this, it should save the information in the same file, Players.txt, but on a different line

    This is just the basics at the moment... I'm still not sure what direction the program will take! But it'd be cool to be able to do this for starters. I'll post any more questions in this thread.

    Cheers!

    Here's the code so far...

    (EDIT: I edited the code and it compiles now! But I get crazy errors when I try to run it :()
    import java.io.FileReader;
    import java.util.Scanner;
    import java.io.PrintWriter;
    import javax.swing.*;          
    import java.awt.*;
    import java.awt.event.*;
    
    class GUI extends JFrame implements ActionListener {
    
    	JButton addPlayer = new JButton("Add player"); 
     	JButton addTeam = new JButton("Add team");
     	JButton addGroup = new JButton("Add group");
    
     	JPanel buttonPanel = new JPanel();
    
     		GUI (){                           // the constructor
       		super("World Cup 2006");      
    
    		addPlayer.addActionListener(this);
    
       		buttonPanel.setLayout(new GridLayout(1,3));      
       		buttonPanel.add(addPlayer);     
       		buttonPanel.add(addTeam);              // add button 
       		buttonPanel.add(addGroup);              // add button 
    
    		Container content = this.getContentPane();
       		content.setLayout(new BorderLayout());
       		content.add(buttonPanel);    // add text area
       		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		}
    
    		 public void actionPerformed(ActionEvent e) {
    
    			if (e.getSource().equals(addPlayer)) {
    
    				Player[] playerArray;
    				playerArray = new Player[1];
    
    				//JOptionPane asks for name, age, origin
    
           				String currName = JOptionPane.showInputDialog(null, "enter name:");
             				String ageString = JOptionPane.showInputDialog(null, "enter age:");
             				int currAge = Integer.parseInt(ageString);
    					String currOrigin = JOptionPane.showInputDialog(null, "enter origin:");
             				playerArray[1] = new Player(currName, currAge, currOrigin);
    
    
    					String myFileName = "Players.txt";
    					try {
    						PrintWriter myOutFile = new PrintWriter (myFileName);
    						myOutFile.println(playerArray[1].getDetails());
    						myOutFile.close();
    					} catch (Exception ex) {
    						JOptionPane.showMessageDialog(null, "Error 1!");
    					}
    
    			}
    			else {
    				JOptionPane.showMessageDialog(null, "Error 2!");
    			}
    		}
    }
    
    
    
    class Player {
    
    	String name;
    	int age;
    	String origin;
    
    	public Player(String n, int a, String o) {
    		name = n;
    		age = a;
    		origin = o;
    	}
    
    
    	public String getDetails(){
    		return name + "(" + age + "), from " + origin;
    	}
    }
    
    public class CupTest {
    	public static void main(String[] args) {
    
        		GUI frame = new GUI();    
        		frame.setSize(300,200);  
        		frame.setLocation(300,330);
        		frame.setVisible(true);
        		frame.pack();
    	}
    }
    
    

    Post any comments you have please, and point me in the right direction! Does the code make sense? How do I change it so that it does what I want it to do?

    Thanks

    ps. this isn't for college or school, just... 'fun' :rolleyes: :o


Comments

  • Closed Accounts Posts: 30 Mr. Magoo


    give me a few mins and ill have a goo at your code. The functionality you've described should be very simple to implement


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    Are you running this on java 1.5?

    I've got 1.4 at present and it don't recognise the scanner import because I think its part of 1.5

    Commented it out and changed the print writer so working ok now.

    This line : playerArray[1] = new Player(currName, currAge, currOrigin);
    And This: myOutFile.println(playerArray[1].getDetails());

    need to be playerArray[0]


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    oooooohhhh, thanks for that both of you! :D

    Yeah it's on 1.5.

    Now how would I get it to print onto different lines? It outputs to the file, but it overwrites the last thing it outputted. I'm sure it's just some parameter I don't know about, but can ya clear that up?

    Thanks again, bloody silly mistake to make :p


  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    PrintWriter myOutFile = new PrintWriter (new FileOutputStream(myFileName,true));

    the true on the end means the file is opened for appending so writting begins at the end of the file. You will need to import java.io.FileOutputStream.

    [edit] This is how I fixed it under 1.4 but should still be fine under 1.5


  • Registered Users, Registered Users 2 Posts: 5,112 ✭✭✭Blowfish


    timeout wrote:
    PrintWriter myOutFile = new PrintWriter (new FileOutputStream(myFileName,true));

    the true on the end means the file is opened for appending so writting begins at the end of the file. You will need to import java.io.FileOutputStream.
    Acctually it's FileWriter you need to use to do that. The constructor is FileWriter(File file, boolean append).
    Just open your output as a File first, (File myOutputFile = new File("pathname"); ) then use it in the FileWriter.

    [edit] never mind, timeouts way will work too.

    Out of curiosity, why are you using an array of one element? It might be an idea to have it as a global variable, then you could store all of the players that the person has added into it. If you were going to do that, a Vector might be a better idea.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 919 ✭✭✭timeout


    I was all set to correct you and you got the edit it ;)

    It should work with alot of different versions once they are of object type OutputStream.

    Think this is only test phease and thats why he's only using one array variable.


Advertisement