Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6225159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:47:32+00:00 2026-05-24T08:47:32+00:00

I am validating a particular column in a CSV file , I have basically

  • 0

I am validating a particular column in a CSV file , I have basically two approaches i.e

  • Approach 1
    I can Read the CSV file line by line with a BufferedReader and Stop when
    I hit a line that startsWith the thing I’m looking for

    try {   
         BufferedReader br = new BufferedReader(
             new InputStreamReader(item.getInputStream()));
    String strLine = "";
    StringTokenizer st = null;
    String value = "";
    while ((strLine = br.readLine()) != null) {
        st = new StringTokenizer(strLine, ",");
        while (st.hasMoreTokens()) {
            value = st.nextToken();
            if ((value != null) && (("MyString1".equals(value)
                || ("MyString2".equals(value)
                || ("MyString3".equals(value)))  {
                // Send error Message   
            }
        }
    }
    
  • Second Approach is use a String.split as below

    Scanner s = new Scanner(item.getInputStream());  
    while(s.hasNextLine()) {  
        String[] fields = s.nextLine().split("\\s*,\\s*", -1);  
        for(int i = 0;i < fields.length;i++) {  
            if(!"".equals(fields[i]) && ...  
        }  
    }  
    s.close();
    

Please suggest a better approach

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:47:33+00:00Added an answer on May 24, 2026 at 8:47 am

    Don’t write your own CSV parser. CSV is actually more complicated than you might at first guess. (Examples: multi-line values, encapsulated fields, escaped characters, and more. Commas don’t always separate fields!)

    Use opencsv, which is a full-featured pure-Java CSV parser.

    This parser will easily let you loop over a CSV file, and process individual columns and rows. Examples are given in the link above.

    Edit: Also, I forgot to mention, it is Apache2 licensed, so it can safely be embedded in commercial projects!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a shortcut in validating fields in winforms? For example a particular textBox
In my Rails app I have a fairly standard has_many relationship between two entities.
I am using Page.Isvalid function in my web page.I have two buttons in my
I have client side date validation that requires one particular Date to be one
I'm investigating an annotation-based approach to validating Spring beans using spring modules . In
Can anyone offer some advice on validating a X509Certificate. I am already checking to
I have a validation attribute that inherits from ValidationAttribute. However, the particular usage of
I'm using ASP.NET MVC and I have a partial control that needs a particular
I'm using JQuery Validation to validate a particular textbox in my form. The textbox
Validating Primitive Arguments and Complex Data Validating Arguments When writing a method, arguments should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.