Results 1 to 3 of 3
- 04-14-2014, 02:05 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Read CSV files and organising the data in Java
Hi, I was wondering anyone one can help me please?
I am trying to write a program that read from a csv file called matches.csv.
A single football match can end with a win or a draw: in first case the winner team get 3 points and the loser none, in the second case of draw each of the two teams get 1 point.
For example, the first line of the file matches.txt is as follow:
In the file it contains the following data.
17/08/2013 Arsenal Aston Villa 1 3
24/08/2013 Aston Villa Liverpool 0 1
This means that a match has been played on the 17/08/2013 where Arsenal scored 1 goal while Aston Villa 3 goals: thus Arsenal got 0 points while Aston Villa 3 points.
How can I structure my output to make it make it read
Position Team Played Points
1 Aston Villa 2 3
2 Liverpool 1 3
3 Arsenal 1 0
Java Code:import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Teams { public static void main(String[] args) { String fileName = "matches.csv"; File file = new File(fileName); try { Scanner inputStream = new Scanner(file); while (inputStream.hasNext()) { String data = inputStream.next(); System.out.println(data); } } catch(FileNotFoundException e) { e.printStackTrace(); } } }
- 04-14-2014, 02:16 AM #2
Re: Read CSV files and organising the data in Java
Also posted at: Read CSV files and organising the data in Java
If you don't understand my response, don't ignore it, ask a question.
- 04-14-2014, 02:48 PM #3
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
Re: Read CSV files and organising the data in Java
Since nobody mentioned this in the other forum yet,
17/08/2013 Arsenal Aston Villa 1 3
24/08/2013 Aston Villa Liverpool 0 1
This is not CSV because there are no commas. And it's not usable as-is, even if you split on spaces because you'll get 3 teams: "Aston", "Villa", and "Liverpool". The lines in the file need to look like this:
17/08/2013,Arsenal,Aston Villa,1,3
24/08/2013,Aston Villa,Liverpool,0,1
Then split each line on commas:
Java Code:String[] arr = data.split(","); String date = arr[0]; String team1 = arr[1]; String team2 = arr[2]; int team1Score = Integer.parseInt(arr[3]); int team2Score = Integer.parseInt(arr[4]);
Similar Threads
-
Read Specific Gantt Chart View Data & Group Definition Data from MPP Files
By sherazam in forum Java SoftwareReplies: 1Last Post: 04-11-2014, 02:39 AM -
jar files that can read video and audio meta data
By ktlim in forum Advanced JavaReplies: 1Last Post: 04-12-2012, 05:33 PM -
How Read and Write XMl files using Java
By tjs in forum SWT / JFaceReplies: 0Last Post: 02-23-2009, 12:19 PM -
How to read pdf files from the folder using java?
By hnj81 in forum New To JavaReplies: 2Last Post: 02-20-2009, 07:13 AM -
Data files - how to read random line?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 08:28 PM
Bookmarks