Help on a simple example?

Hi,

I am trying to get my head around java (well actually programming in general) and wondered if anyone could help with a fairly simple example. This isn't 'homework' - this is just something I'm interested in and want to learn. The problem I find is so many examples on the net are just so complicated, I try and follow it but get lost.

I am wanting to create a a dead simple football text based game (on the lines of championship manager but a lot simpler!)

For now I just want to get the code started really.

This code is entirely untested as this has been done at work without a compiler, but can anyone help me out with my question below this code?

Player.java

import java.io.*;

import javax.swing.*;

class Player

{

private String name ="David Healy";

privateint speed = 10;

privateint shooting = 15;

//etc etc I understand that if I add more instance variables they would go below as well in the constructor.

public Player ( String name,int speed,int shooting)

this.name = name;

this.speed = speed;

this.shooting = shooting;

}

public Player ()

{

}

publicvoid getName()

{

return name;

}

publicvoid getSpeed()

{

return speed;

}

publicvoid getShooting()

{

return shooting;

}

Game.java

import java.io.*;

import javax.swing.*;

publicclass Game

{

publicstaticvoid main(String[] args)throws IOException

{

Player p1 =new Player();

p1.getName();

p1.getSpeed();

p1.getShooting();

System.exit(1);

}

}

Now my question is - I want to make this so that instead of me actually setting the instance variables to a name, a text file could do it for me. In the form of:

David Healy

10

12

John Smith

12

12

etc.

Can anyone help? All I want at this time is to be able to print each players details. Eventually I want to assign them to teams but will add this in later

Thank you

Andrew

Message was edited by:

andrewsco1

[3627 byte] By [andrewsco1a] at [2007-11-26 20:13:44]
# 1
Hint: don't try to follow examples and alter code. You'll never learn anything. Write programs yourself, from scratch, without looking at an example.
CeciNEstPasUnProgrammeura at 2007-7-9 23:19:59 > top of Java-index,Java Essentials,Java Programming...
# 2

If you want to print player details, then maybe the players should have a method that provides them in a for that others don't have to worry about. Overriding toString() would be a good approach. Or let the player instances print themselves right away when asked.

The trick is about *not* letting anyone access the player's data, but telling the players what to do with their data themselves.

CeciNEstPasUnProgrammeura at 2007-7-9 23:19:59 > top of Java-index,Java Essentials,Java Programming...
# 3

I did a project similar to this but in Perl. what I did was enter all the player's detail in a database - MySQL is free,, but when using text files I would have all the details in there, extract them using a regex, put them in a HaahMap & then use your get/set method to print them or whatever.

adunkey10a at 2007-7-9 23:19:59 > top of Java-index,Java Essentials,Java Programming...