Results 1 to 3 of 3
  1. #1
    ThretZ is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default 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();
    		}
     
       }
     
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Read CSV files and organising the data in Java

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    BinaryDigit09 is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default 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]);
    As far as the output you want, don't necessarily try to do it all in the same read loop. Use the read loop to collect all the data from the file, then after the read loop go analyze the data and build your output.

Similar Threads

  1. Replies: 1
    Last Post: 04-11-2014, 02:39 AM
  2. jar files that can read video and audio meta data
    By ktlim in forum Advanced Java
    Replies: 1
    Last Post: 04-12-2012, 05:33 PM
  3. How Read and Write XMl files using Java
    By tjs in forum SWT / JFace
    Replies: 0
    Last Post: 02-23-2009, 12:19 PM
  4. Replies: 2
    Last Post: 02-20-2009, 07:13 AM
  5. Data files - how to read random line?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 08:28 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
  •