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 5 of 5

Thread: Reading CSV File

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reading CSV File

    I have a CSV file with 16K entries of a data table.

    Does Java work well with CSV file? Sorry I am a complete noob

    --- Update ---



    Okay, so I found this code. And it seems its quite easy to read in the data I need. Say for example if I wanted a loop to randomly pick the first field of a specific line in the CSV data table. How would i go about coding that??????

    package cvs.test;
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class CVSTest {
     
     
        public static void main(String[] args) throws IOException {
     
            String lines = "";
            String unparsedFile = "";
            String myArray[];
     
            FileReader fr = new FileReader("c://Data1.csv");
            BufferedReader br = new BufferedReader(fr);
     
            while((lines = br.readLine()) != null) {
                unparsedFile += lines;
            }
            br.close();
     
            myArray = unparsedFile.split(",");
     
            for(String item : myArray){
                System.out.println(item);
            }
     
            }   
        }
    }


    --- Update ---

    Hand	Rank	Percentile	SB	BB	UG	MP	CO	BN
    [AJ][AJ]	1	0.01%	T	T	T	T	T	T
    [AT][AT]	2	0.01%	F	T	T	T	T	T
    [AQ][AQ]	3	0.02%	F	F	T	T	T	T
    [AJ][AT]	4	0.02%	F	F	F	T	T	T
    [A5][A5]	5	0.03%	F	F	F	F	T	T
    [A9][A9]	6	0.04%	F	F	F	F	F	T
    [AK][AK]	7	0.04%	F	F	F	F	F	T

    The CSV looks like the above. and I basically would like to read in the Hand to get it to show in a text box and then randomly have the program ask me to correctly identify the True/False return for one of the SB/BB/UG/MP/CO/BN columns. As you can prob tel i am not very good with Java... so i am stumbling my way through this. I know what I would like to do it, its just a case of how I get there!!!

    Thanks for any replies


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reading CSV File

    You use the word "randomly," but what you mean by it isn't clear.

    What you describe (I think) could be done relatively simply by someone who has a good understanding of OOP with Java and is beginning to learn GUI programming using Swing. I don't know where you are on the knowledge and experience scale, but here are the Swing tutorials to get you started on the GUI using Swing part. Ignore the biased advice to use the Netbeans GUIBuilder and learn to code the GUI "by hand."

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading CSV File

    Thanks for the reply. That link is useful and i will have to read it quite a few times. As I am a complete beginner with limited free time to read up on Java, but the time I do have spare I spend trying to learn as much as I can.

    Sorry if I dont explain things well, English isn't my first language. I wish to have some kind of loop or iteration? that selects the "hand" as above, say for example [A9][A9] and displays this within a text box, then at the same time, another text box requres me to enter the correct T or F figure for any one of the T or F fields for that hand.

    And then the loop to work through the list of rows. Above is a sample - there are actually 16432 rows in the .CSV that i am using.

    Hope this makes sense

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reading CSV File

    I recommend you create a Hand class that contains the attributes shown in each line. Read a line from the file, parse as needed, send the parsed data to the Hand constructor, and add the new Hand instance to a collection of some kind. Create methods in the Hand class as needed to provide the rest of the functionality you describe.

    As for the GUI interface part, you'll have to add that as you learn how. If you're in a hurry to get your idea working, you might get the functionality working from the command line first and then add the GUI when you've learned how.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading CSV File

    I'll give that a go and then post what i come up with in here

Similar Threads

  1. csv or text file to Multiple csv or text file
    By robert1994 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 19th, 2013, 01:35 PM
  2. reading csv by scanner
    By takamine in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 22nd, 2013, 10:41 AM
  3. 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
  4. Reading CSV file to take min, max, average of various stats
    By Annorax in forum Collections and Generics
    Replies: 1
    Last Post: March 3rd, 2011, 08:57 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