Results 1 to 5 of 5
  1. #1
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Extracting and displaying data from a text file

    Hi all

    I was wondering if anyone would be able to give me a little bit of help and advice with something I'm having a bit of trouble with. I'm a Java newbie and what I'm trying to do is perhaps a little advanced for me.

    What I am trying to do is extract data from a text file containing football results (please see attachment) and display information regarding a specific team. I am fine up until the point where I have split the data into its component parts, but I am completely stumped as to actually displaying the data as I would like. I am just trying to keep this as basic as possible at the moment. Please take a look at what I have so far:


    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Results
    {
    
            public static void main (String[] args) throws FileNotFoundException 
    	{
    		new File("team.html").delete();		
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.print("Select a specific team: ");
    		String team = scan.nextLine();
    		
    		System.out.print("Select (H)TML or (P)lain text output: ");
    		String f = scan.nextLine();
    		String format = f.toUpperCase();
    		
    		{
    			Scanner s = new Scanner(new File("results.txt"));
    			
    			String hteam;
    			String ateam;
    			int hgoals;
    			int agoals;
    							
    			String line;
    			
    				while (s.hasNext())
    				{
    					
    					line = s.nextLine();
    					String[] splitupText = line.split(" : ");
    	
    						if (splitupText.length==4)
    						{
    							{
    								
    								hteam = splitupText[0].trim();
    								ateam = splitupText[1].trim();
    								hgoals = Integer.parseInt(splitupText[2].trim());
    								agoals = Integer.parseInt(splitupText[3].trim());
    								
    							}
    							
    								[COLOR="Blue"]if(format.equals("P"))[/COLOR]
    								{
    									
    								
    									
    								}
    							
    						}				
    				}
    		}		
    	}
    }
    In the section I've highlighted in blue, I need to somehow calculate and go on to display the following information for the user's chosen team, but I'm not really sure how to and would greatly appreciate a few pointers. I guess what is confusing me most is the chosen team could appear as either the home team (hteam) or away team (ateam) so the way the results are calculated is dependent on this factor.

    Games played: (this should just be the total number of occurrances of the chosen team?)
    Games won:
    Games drawn:
    Games lost:
    Goals for:
    Goals against:

    Many thanks
    fugazi
    Attached Files Attached Files

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    you can try using Maps. for example to store draw results for home team
    Java Code:
    int hdrawcount=0;
    Map <String, Integer> hdrawResults = new HashMap <String, Integer>();
    .....
    if ( hgoals == agoals) {
       drawResults.put( hteam, hdrawcount++)
    }
    ....
    do the same for your other outputs...

  3. #3
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for your quick reply JavaHater.

    The problem I'm having now is that when I output the results, I get a line for each mach, with either a '0' if the game wasn't a draw and a '1' if the game was a draw. How can I get the program to output the sum total of these numbers on a single line?

    Sorry to be a pain, I'm sure this is something very basic. I'm new to Java programming and just sort of learning as I go along.
    Last edited by fugazi; 01-07-2011 at 04:56 PM. Reason: correction

  4. #4
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Sorry to double post, but just to clarify,

    Here is my code as it currently stands:

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    import java.io.*;
    
    
    public class testresults
    {
    
            public static void main (String[] args) throws FileNotFoundException 
    	{
    		new File("team.html").delete();		
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.print("Select a specific team: ");
    		String team = scan.nextLine();
    		
    		System.out.print("Select (H)TML or (P)lain text output: ");
    		String f = scan.nextLine();
    		String format = f.toUpperCase();
    		
    		{
    			Scanner s = new Scanner(new File("results.txt"));
    			
    			String hteam;
    			String ateam;
    			int hgoals;
    			int agoals;
    							
    			String line;
    			
    				while (s.hasNext())
    				{
    					
    					line = s.nextLine();
    					String[] splitupText = line.split(" : ");
    	
    						if (splitupText.length==4)
    						{
    							{
    								
    								hteam = splitupText[0].trim();
    								ateam = splitupText[1].trim();
    								hgoals = Integer.parseInt(splitupText[2].trim());
    								agoals = Integer.parseInt(splitupText[3].trim());
    								
    							}
    							
    								if(format.equals("P"))
    								{
    									if(hteam.equals(team) || ateam.equals(team))
    									{
    										
    										int wincount=0;
    										Map <String, Integer> winresults = new HashMap <String, Integer>();
    											
    										if ((hteam.equals(team) && hgoals > agoals) || (ateam.equals(team) && agoals > hgoals))
    										{
    											   winresults.put(hteam, wincount++);										   
    										}
    										
    										System.out.println("Games won: " + wincount);
    								
    									}
    								}
    							
    						}				
    				}
    		}		
    	}
    }
    Which outputs:

    Java Code:
    Select a specific team: Everton
    Select (H)TML or (P)lain text output: P
    Games won: 1
    Games won: 1
    Games won: 1
    Games won: 1
    Which is correct as they have won four games, however it is outputting each win on a separate line. I'm looking to somehow get a total of these games so it outputs as follows:

    Java Code:
    Select a specific team: Everton
    Select (H)TML or (P)lain text output: P
    Games won: 4
    If anyone can help with this I'd be eternally grateful!

    Many thanks
    fugazi

  5. #5
    fugazi is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Sorry to TRIPLE post, but I've solved it! Phew. Took my time but I got there.

Similar Threads

  1. Extracting Text from Image
    By kish27 in forum Advanced Java
    Replies: 3
    Last Post: 03-08-2011, 03:42 AM
  2. Extracting the text between two strings
    By vidya in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 11:04 PM
  3. extracting text from jpeg
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 10-05-2008, 11:40 PM
  4. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 10:30 AM
  5. Extracting data from an XML file...
    By techno_brains in forum New To Java
    Replies: 1
    Last Post: 07-15-2007, 05:46 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
  •