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: saving and accessing information?

  • 09-05-2006 4:10pm
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey folks,

    I'm messing around trying to create a Java program that replicates the World Cup, and I'm just wondering is it possible to save information in say a text file, and then access that at a later stage.

    Here's the code to my program so far (I just started it today):
    class Player {
    
    	String name;
    	int age;
    	String origin;
    
    	public  Player(String n, int a, String o) {
    
    		name = n;
    		age = a;
    		origin = o;
    
    	}
    }
    
    class Team {
    
    	Player[] TeamPlayers;
    	String TeamName;
    	
    	public Team(Player[] tp, String tn) {
    
    		tp = TeamPlayers;
    		tn = TeamName;
    
    	}
    }
    
    class Group {
    
    	Team[] GroupTeams;
    	String GroupName;
    
    	public Group(Team[] gt, String gn) {
    
    		gt = GroupTeams;
    		gn = GroupName;
    
    	}
    }
    
    (this compiles okay btw -- dunno how much use it is though, or will be)

    I'm not gonna go through every player and team in the tournament, but I'd like to have the framework that permits me to do that.

    I want to have a file that saves all the players that I create and their information (name, age, origin), and then another that saves all the teams and their information (players, team name), and then another that saves all the groups with their information (teams in the group, group name).

    I presume that this is possible since it's necessary for any decent program to work (otherwise the user would have to keep populating arrays and so on each time it runs). How would I do it in Java?

    FYI: I'm a first year Computer Science student, but this isn't a college project, it's just something I started to do to better understand objects and classes, and it's getting interesting :D I'm actually finished my course for the year, and had my exam today, which went fine thank you :D

    Thanks for any help!


Comments

  • Registered Users, Registered Users 2 Posts: 527 ✭✭✭Sean^DCT4


    import java.io.FileReader;
    import java.io.FileWriter;

    These are what you are looking for, do a search on the java.sun.com site and or have a look at the API.

    Here is a sample of code I have written recently for a GPS Bluetooth program for reading from a text file...
    	public void read() {
    		try {
    			String str = null;
    			 
    			FileReader inFile = new FileReader(fileName);			
    
    			char[] cBuf = new char[1000];
    	
    			while (inFile.read(cBuf, 0, 1000) != -1) {
    				str = new String(cBuf);
    				
    				int sol = str.indexOf("GPGGA");											
    				while (sol != -1) {
    					str = str.substring(sol);
    									
    					int eol = str.indexOf("$");
    					if (eol != -1) {						
    						list.add(str.substring(0, eol).trim());						
    						str = str.substring(eol);
    					}
    					else {
    						str = str.substring(1);
    					}
    					
    					sol = str.indexOf("GPGGA");
    				}					
    			}
    
    			inFile.close();
    		}
    		catch (Exception e) {
    			e.printStackTrace();		
    		}		
    	}
    
    


  • Closed Accounts Posts: 261 ✭✭bishopLEN


    Hi,
    Have a look into XML for storing the data. Define some DTD's.
    Makes things easier to manage then.
    There was an xml thread here recently that I posted to with the code to read from an xml file.


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    If it's a learning project then go the whole hog and dump some SQL in there too.

    Forget about files, use hypersonic a 100% java database that can be run straight from your code (no server needed!)

    This means you can store the data in tables and kill 2 birds with one stone.


Advertisement