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

Thread: Read CSV File

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Read CSV File

    have to implement a static-public method named "csvToList" in the class "Functionality.java". The method gets a string as input parameter and returns a LinkedList<String[]>.

    Signature: csvToList(String path) : LinkedList<String[]>

    My problem is to implement this : The method reads a CSV file in the path. Each single line is divided into two parts. The two parts are always separated by a comma. The two parts are normalized by removing any spaces at the front and back, and stored in a string array. This two-pair in string[] is added to the list. The list with the two-pairs is to be returned.

    The algorithm must meet the following requirements:

    The first row should not be read in, because it contains only the column information.
    The normalized entries of a line should be in a string array.
    The individual elements of the lines should not contain any spaces in the string array.
    If an IOException is thrown when reading the file, null should be returned. Use try & catch for this.

    My code:

    public static String [] csvToList(String path) {
        String fileName = "c:\\test\\csv\\country.csv";
          String splitBy = ",";
                try {
                BufferedReader br = new BufferedReader(new FileReader(country.csv));
                String line;
                String line1=null;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
     
            }
            return null;
    Last edited by NehaSharma_1997; January 5th, 2021 at 07:37 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read CSV File

    Do you have any specific java programming questions about your assignment?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read CSV File

    Yes how to implement this: The two parts are normalized by removing any spaces at the front and back, and stored in a string array. This two-pair in string[] is added to the list. The list with the two-pairs is to be returned

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read CSV File

    Make a list of the steps the program needs to take to solve the problem. Then take the first item in the list and work on the details of what the program needs to do to implement it.
    When you need help, post the list and the item in the list that you are having problems with. Post what you think the computer should do for that step and ask any questions about the problems you are having with it.

    Can you post a short simple example of what you are trying to do?
    Show a few lines of a CSV file and the desired String array contents.

    A problem with using an array is the size needs to be determined before it can be declared and used.
    How will the program determine the array's size?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    NehaSharma_1997 (January 5th, 2021)

  6. #5
    Junior Member
    Join Date
    Jan 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read CSV File

    This is the CSV File

    Mail,Passwort
    sam@gmail.com, sfkjiwefsdkfl
    totin@airbnb.com, 29sdf@s
    raj@gmail.com, abc
    dev@gmail.com, wild

    --- Update ---

    The algorithm must meet the following requirements:

    The first row should not be read in, because it contains only the column information.
    The normalized entries of a line should be in a string array.
    The individual elements of the lines should not contain any spaces in the string array.
    If an IOException is thrown when reading the file, null should be returned. Use try & catch for this.

    is implemented:

     try {
                BufferedReader br = new BufferedReader(new FileReader(country.csv));
                String line;
                String line1=null;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
     
            }
            return null;

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read CSV File

    What should go into the String array? Can you post an example?

    The normalized entries of a line
    What does it mean to normalize entries of a line?

    The individual elements of the lines should not contain any spaces
    Look at the API doc for the String class. It has methods for removing/replacing characters in a String.
    The Java SE classes API doc: http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  2. [SOLVED] Jar file to read text file from within itself
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2011, 06:34 PM
  3. [SOLVED] Can I have two scanners to the same file? (I want to read through one file 2 times)
    By beginnerprogrammer in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 20th, 2011, 11:23 AM
  4. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  5. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM

Tags for this Thread