Results 1 to 5 of 5
- 01-07-2011, 02:50 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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:
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.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] { } } } } } }
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
- 01-07-2011, 03:20 PM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
you can try using Maps. for example to store draw results for home team
do the same for your other outputs...Java Code:int hdrawcount=0; Map <String, Integer> hdrawResults = new HashMap <String, Integer>(); ..... if ( hgoals == agoals) { drawResults.put( hteam, hdrawcount++) } ....
- 01-07-2011, 03:49 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-07-2011, 05:27 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Sorry to double post, but just to clarify,
Here is my code as it currently stands:
Which outputs: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 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: 1 Games won: 1 Games won: 1 Games won: 1
If anyone can help with this I'd be eternally grateful!Java Code:Select a specific team: Everton Select (H)TML or (P)lain text output: P Games won: 4
Many thanks
fugazi
- 01-07-2011, 06:37 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Extracting Text from Image
By kish27 in forum Advanced JavaReplies: 3Last Post: 03-08-2011, 03:42 AM -
Extracting the text between two strings
By vidya in forum New To JavaReplies: 7Last Post: 02-11-2010, 11:04 PM -
extracting text from jpeg
By Nicholas Jordan in forum Advanced JavaReplies: 0Last Post: 10-05-2008, 11:40 PM -
Displaying data into text area
By abhiN in forum New To JavaReplies: 1Last Post: 01-22-2008, 10:30 AM -
Extracting data from an XML file...
By techno_brains in forum New To JavaReplies: 1Last Post: 07-15-2007, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks