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

  • Home
  • SEARCH
  • 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 6370527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:56:29+00:00 2026-05-25T00:56:29+00:00

I have a csv file with unknown amount of columns and row. The only

  • 0

I have a csv file with unknown amount of columns and row. The only thing I know is that each entry is separated by a comma. Can I use the split method to convert each line of the data into an array and then can I store that Array into an Arraylist. One of the things that concerns me is would I be able to rearrange the Arraylist alphabetically or numerically.

  • 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-25T00:56:30+00:00Added an answer on May 25, 2026 at 12:56 am

    There are several questions here so I’ll cover each point individually.

    Can I use the split method convert each line of the data into an array

    This would work as you expect in the naive case. However, it doesn’t know anything about escaping; so if a comma is embedded within a field (and properly escaped, usually by double-quoting the whole field) the simple split won’t do the job here and will chop the field in two.

    If you know you’ll never have to deal with embedded commas, then calling line.split(",") is acceptable. The real solution however is to write a slightly more involved parse method which keeps track of quotes, and possibly backslash escapes etc.


    …into an array than can I store that Array into an Arraylist

    You certainly could have an ArrayList<String[]>, but that doesn’t strike me as particularly useful. A better approach would be to write a simple class for whatever it is the CSV lines are representing, and then create instances of that class when you’re parsing each line. Something like this perhaps:

    public class Order {
        private final int orderId;
        private final String productName;
        private final int quantity;
        private final BigDecimal price;
    
        // Plus constructor, getters etc.
    }
    
    private Order parseCsvLine(String line) {
       String[] fields = line.split(",");
    
       // TODO validation of input/error checking
       final int orderId = Integer.parseInt(fields[0]);
       final String productName = fields[1];
       final int quantity = Integer.parseInt(fields[2]);
       final BigDecimal price = new BigDecimal(fields[3]);
    
       return new Order(orderId, productName, quantity, price);
    }
    

    Then you’d have a list of Orders, which more accurately represents what you have in the file (and in memory) than a list of string-arrays.


    One of the things that concerns me is would I be able to rearrange the Arraylist according alphabetically or numerically?

    Sure – the standard collections support a sort method, into which you can pass an instance of Comparator. This takes two instances of the object in the list, and decides which one comes before the other.

    So following on from the above example, if you have a List<Order> you can pass in whatever comparator you want to sort it, for example:

    final Comparator<Order> quantityAsc = new Comparator<Order>() {
       public int compare(Order o1, Order o2) {
          return o2.quantity - o1.quantity; // smaller order comes before bigger one
       }
    }
    
    final Comparator<Order> productDesc = new Comparator<Order>() {
       public int compare(Order o1, Order o2) {
          if (o2.productName == null) {
             return o1.productName == null ? 0 : -1;
          }
          return o2.productName.compareTo(o1.productName);
       }
    }
    
    final List<Order> orders = ...; // populated by parsing the CSV
    
    final List<Order> ordersByQuantity = Collections.sort(orders, quantityAsc);
    final List<Order> ordersByProductZToA = Collections.sort(orders, productDesc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have .csv file that contain 2 columns delimited with , . file.csv word1,word2
I have a CSV file with several entries, and each entry has 2 unix
I have a csv file where each row is a different type of record.
I have a time series(a csv file) data that looks like below. Each observation
I have a .csv file and I'm only interested in rows with comma delimeted
I have a csv file with traceroutes, the last entry in every row is
I have a comma separated CSV file looks like: customer1,customer2,,customer4, ,customer2,,customer4, custome1,,customer3,, I want
I have a CSV file with 5 columns and about 2*10 4 rows that
I have a csv file that has like 30,000 rows in it. It also
I have a .CSV file with the following entries. The last entry is location.

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.