Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Read CSV files and organising the data in Java

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

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

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    Can anyone help me please? The CSV file contains
    17/08/2013 Arsenal Aston Villa 1 3
    24/08/2013 Aston Villa Liverpool 0 1

    In java eclipse how do I use read the file and arrange it so the output will be look this:
    Position Team Played Points
    1 Aston Villa 2 3
    2 Liverpool 1 3
    3 Arsenal 1 0

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Divide the program up into several steps.
    Start by reading the file into Strings
    and parsing the Strings into the parts with the different values.
    Make a list of the teams.
    Build the totals
    Sort the list by the totals
    print out the list
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    how do i parse the strings into the parts?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    The String class's split() method would be good with a CSV file.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    by the way is this code correct for reading the csv file in string?

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

    }

    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What happens when it is executed? The output will show you.
    I'd read the file line by line and split the line into parts vs token by token.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    When i code run the output displays the data from the csv file.
    But now how do I include the String class's split()?

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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    how do I include the String class's split()?
    After the code has read a line of text from the file into a String,
    use the split() method to separate the tokens on that line into elements in an array.

    The code should use the nextLine() method to read a line from the file, not the next() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. merge 2 csv files
    By sudhi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2014, 05:52 AM
  2. Inventory CSV files
    By mstratmann in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 25th, 2013, 12:26 AM
  3. read csv file in java
    By kishore1981 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2012, 07:52 AM
  4. Trying to read a CSV file !
    By Cruz182 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 2nd, 2011, 01:30 PM
  5. Reading CSV files into a program
    By Wrathgarr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2010, 10:41 AM